Java Code Examples for com.google.common.base.Equivalence#Wrapper

The following examples show how to use com.google.common.base.Equivalence#Wrapper . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MethodSignature.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
static MethodSignature fromExecutableType(String methodName, ExecutableType methodType) {
  checkNotNull(methodType);
  ImmutableList.Builder<Equivalence.Wrapper<TypeMirror>> parameters = ImmutableList.builder();
  ImmutableList.Builder<Equivalence.Wrapper<TypeMirror>> thrownTypes = ImmutableList.builder();
  for (TypeMirror parameter : methodType.getParameterTypes()) {
    parameters.add(MoreTypes.equivalence().wrap(parameter));
  }
  for (TypeMirror thrownType : methodType.getThrownTypes()) {
    thrownTypes.add(MoreTypes.equivalence().wrap(thrownType));
  }
  return new AutoValue_MethodSignature(
      methodName,
      parameters.build(),
      thrownTypes.build());
}
 
Example 2
Source File: TreeDiffer.java    From compile-testing with Apache License 2.0 5 votes vote down vote up
static MethodSignature create(
    CompilationUnitTree compilationUnitTree, MethodTree tree, Trees trees) {
  ImmutableList.Builder<Equivalence.Wrapper<TypeMirror>> parameterTypes =
      ImmutableList.builder();
  for (VariableTree parameter : tree.getParameters()) {
    parameterTypes.add(
        MoreTypes.equivalence()
            .wrap(trees.getTypeMirror(trees.getPath(compilationUnitTree, parameter))));
  }
  return new AutoValue_TreeDiffer_MethodSignature(
      tree.getName().toString(), parameterTypes.build());
}
 
Example 3
Source File: BindingGraph.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
private Optional<RequestResolver> getOwningResolver(ProvisionBinding provisionBinding) {
  Optional<Equivalence.Wrapper<AnnotationMirror>> bindingScope =
      provisionBinding.wrappedScope();
  for (RequestResolver requestResolver : getResolverLineage()) {
    if (bindingScope.equals(requestResolver.targetScope)
        || requestResolver.explicitProvisionBindings.containsValue(provisionBinding)) {
      return Optional.of(requestResolver);
    }
  }
  return Optional.absent();
}
 
Example 4
Source File: Mirrors.java    From auto with Apache License 2.0 5 votes vote down vote up
/**
 * Unwraps an {@link Optional} of a {@link Equivalence.Wrapper} into an {@code Optional} of the
 * underlying type.
 */
static <T> Optional<T> unwrapOptionalEquivalence(
    Optional<Equivalence.Wrapper<T>> wrappedOptional) {
  return wrappedOptional.isPresent()
      ? Optional.of(wrappedOptional.get().get())
      : Optional.<T>absent();
}
 
Example 5
Source File: TypeVariables.java    From auto with Apache License 2.0 5 votes vote down vote up
/**
 * Tests whether a given parameter can be given to a static method like
 * {@code ImmutableMap.copyOf} to produce a value that can be assigned to the given target type.
 *
 * <p>For example, suppose we have this method in {@code ImmutableMap}:<br>
 * {@code static <K, V> ImmutableMap<K, V> copyOf(Map<? extends K, ? extends V>)}<br>
 * and we want to know if we can do this:
 *
 * <pre>
 * {@code ImmutableMap<String, Integer> actualParameter = ...;}
 * {@code ImmutableMap<String, Number> target = ImmutableMap.copyOf(actualParameter);}
 * </pre>
 *
 * We will infer {@code K=String}, {@code V=Number} based on the target type, and then rewrite the
 * formal parameter type from<br>
 * {@code Map<? extends K, ? extends V>} to<br>
 * {@code Map<? extends String, ? extends Number>}. Then we can check whether
 * {@code actualParameter} is assignable to that.
 *
 * <p>The logic makes some simplifying assumptions, which are met for the {@code copyOf} and
 * {@code of} methods that we use this for. The method must be static, it must have exactly one
 * parameter, and it must have type parameters without bounds that are the same as the type
 * parameters of its return type. We can see that these assumptions are met for the
 * {@code ImmutableMap.copyOf} example above.
 */
static boolean canAssignStaticMethodResult(
    ExecutableElement method,
    TypeMirror actualParameterType,
    TypeMirror targetType,
    Types typeUtils) {
  if (!targetType.getKind().equals(TypeKind.DECLARED)
      || !method.getModifiers().contains(Modifier.STATIC)
      || method.getParameters().size() != 1) {
    return false;
  }
  List<? extends TypeParameterElement> typeParameters = method.getTypeParameters();
  List<? extends TypeMirror> targetTypeArguments =
      MoreTypes.asDeclared(targetType).getTypeArguments();
  if (typeParameters.size() != targetTypeArguments.size()) {
    return false;
  }
  Map<Equivalence.Wrapper<TypeVariable>, TypeMirror> typeVariables = new LinkedHashMap<>();
  for (int i = 0; i < typeParameters.size(); i++) {
    TypeVariable v = MoreTypes.asTypeVariable(typeParameters.get(i).asType());
    typeVariables.put(MoreTypes.equivalence().wrap(v), targetTypeArguments.get(i));
  }
  TypeMirror formalParameterType = method.getParameters().get(0).asType();
  SubstitutionVisitor substitutionVisitor = new SubstitutionVisitor(typeVariables, typeUtils);
  TypeMirror substitutedParameterType = substitutionVisitor.visit(formalParameterType, null);
  if (substitutedParameterType.getKind().equals(TypeKind.WILDCARD)) {
    // If the target type is Optional<? extends Foo> then <T> T Optional.of(T) will give us
    // ? extends Foo here, and typeUtils.isAssignable will return false. But we can in fact
    // give a Foo as an argument, so we just replace ? extends Foo with Foo.
    WildcardType wildcard = MoreTypes.asWildcard(substitutedParameterType);
    if (wildcard.getExtendsBound() != null) {
      substitutedParameterType = wildcard.getExtendsBound();
    }
  }
  return typeUtils.isAssignable(actualParameterType, substitutedParameterType);
}
 
Example 6
Source File: Util.java    From dagger2-sample with Apache License 2.0 5 votes vote down vote up
/**
 * Wraps an {@link Optional} of a type in an {@code Optional} of a {@link Wrapper} for that type.
 */
static <T> Optional<Equivalence.Wrapper<T>> wrapOptionalInEquivalence(
    Equivalence<T> equivalence, Optional<T> optional) {
  return optional.isPresent()
      ? Optional.of(equivalence.wrap(optional.get()))
      : Optional.<Equivalence.Wrapper<T>>absent();
}
 
Example 7
Source File: SerializableAutoValueExtension.java    From auto with Apache License 2.0 5 votes vote down vote up
ProxyGenerator(
    TypeName outerClassTypeName,
    ImmutableList<TypeVariableName> typeVariableNames,
    ImmutableList<PropertyMirror> propertyMirrors,
    ImmutableMap<Equivalence.Wrapper<TypeMirror>, Serializer> serializersMap) {
  this.outerClassTypeName = outerClassTypeName;
  this.typeVariableNames = typeVariableNames;
  this.propertyMirrors = propertyMirrors;
  this.serializersMap = serializersMap;
}
 
Example 8
Source File: ReasonerUtils.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
private static <B, S extends B> Map<Equivalence.Wrapper<B>, Integer> getCardinalityMap(Collection<S> coll, Equivalence<B> equiv) {
    Map<Equivalence.Wrapper<B>, Integer> count = new HashMap<>();
    for (S obj : coll) count.merge(equiv.wrap(obj), 1, Integer::sum);
    return count;
}
 
Example 9
Source File: TypeMirrorSet.java    From RetroFacebook with Apache License 2.0 4 votes vote down vote up
private Equivalence.Wrapper<TypeMirror> wrap(TypeMirror typeMirror) {
  return MoreTypes.equivalence().wrap(typeMirror);
}
 
Example 10
Source File: QuerySet.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
public static QuerySet create(Collection<Equivalence.Wrapper<ReasonerQueryImpl>> queries){
    return new QuerySet(queries.stream().map(Equivalence.Wrapper::get).collect(Collectors.toSet()));
}
 
Example 11
Source File: QueryCollection.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
public boolean contains(Equivalence.Wrapper<ReasonerQueryImpl> q){
    return wrappedCollection.contains(q);
}
 
Example 12
Source File: MultilevelSemanticCache.java    From grakn with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
Equivalence.Wrapper<ReasonerAtomicQuery> queryToKey(ReasonerAtomicQuery query) {
    return unifierType().equivalence().wrap(query);
}
 
Example 13
Source File: Key.java    From dagger2-sample with Apache License 2.0 2 votes vote down vote up
/**
 * The type represented by this key.
 *
 * As documented in {@link TypeMirror}, equals and hashCode aren't implemented to represent
 * logical equality, so {@link MoreTypes#equivalence()} wraps this type.
 */
abstract Equivalence.Wrapper<TypeMirror> wrappedType();
 
Example 14
Source File: ComponentDescriptor.java    From dagger2-sample with Apache License 2.0 2 votes vote down vote up
/**
 * An optional annotation constraining the scope of this component wrapped in an
 * {@link com.google.common.base.Equivalence.Wrapper} to preserve comparison semantics of
 * {@link AnnotationMirror}.
 */
abstract Optional<Equivalence.Wrapper<AnnotationMirror>> wrappedScope();
 
Example 15
Source File: ProvisionBinding.java    From dagger2-sample with Apache License 2.0 2 votes vote down vote up
/**
 * An optional annotation constraining the scope of this component wrapped in an
 * {@link com.google.common.base.Equivalence.Wrapper} to preserve comparison semantics of
 * {@link AnnotationMirror}.
 */
abstract Optional<Equivalence.Wrapper<AnnotationMirror>> wrappedScope();
 
Example 16
Source File: MethodSignature.java    From dagger2-sample with Apache License 2.0 votes vote down vote up
abstract ImmutableList<Equivalence.Wrapper<TypeMirror>> parameterTypes(); 
Example 17
Source File: MethodSignature.java    From dagger2-sample with Apache License 2.0 votes vote down vote up
abstract ImmutableList<Equivalence.Wrapper<TypeMirror>> thrownTypes(); 
Example 18
Source File: Key.java    From auto with Apache License 2.0 votes vote down vote up
abstract Equivalence.Wrapper<TypeMirror> type(); 
Example 19
Source File: TreeDiffer.java    From compile-testing with Apache License 2.0 votes vote down vote up
abstract ImmutableList<Equivalence.Wrapper<TypeMirror>> parameterTypes(); 
Example 20
Source File: Key.java    From auto with Apache License 2.0 votes vote down vote up
abstract Optional<Equivalence.Wrapper<AnnotationMirror>> qualifierWrapper();