Example usage for com.google.common.base Optional equals

List of usage examples for com.google.common.base Optional equals

Introduction

In this page you can find the example usage for com.google.common.base Optional equals.

Prototype

@Override
public abstract boolean equals(@Nullable Object object);

Source Link

Document

Returns true if object is an Optional instance, and either the contained references are Object#equals equal to each other or both are absent.

Usage

From source file:org.geogit.api.plumbing.merge.MergeFeaturesOp.java

@Override
public Feature call() {
    checkNotNull(nodeRefA, "first feature version not specified");
    checkNotNull(nodeRefB, "second feature version not specified");
    checkNotNull(ancestorRef, "ancestor version not specified");
    // String firstPath = removeRef(oldNodeRef.path());
    // String newPath = removeRef(newNodeRef.path());
    checkArgument(nodeRefA.path().equals(nodeRefB.path()),
            "old and new versions do not correspond to the same feature");

    Optional<RevFeature> featureA = command(RevObjectParse.class).setObjectId(nodeRefA.getNode().getObjectId())
            .call(RevFeature.class);
    checkArgument(featureA.isPresent(), "Invalid reference: %s", nodeRefA);

    Optional<RevFeature> featureB = command(RevObjectParse.class).setObjectId(nodeRefB.getNode().getObjectId())
            .call(RevFeature.class);
    checkArgument(featureB.isPresent(), "Invalid reference: %s", nodeRefB);

    Optional<RevFeature> ancestorFeature = command(RevObjectParse.class)
            .setObjectId(ancestorRef.getNode().getObjectId()).call(RevFeature.class);
    checkArgument(ancestorFeature.isPresent(), "Invalid reference: %s", ancestorRef);

    Optional<RevFeatureType> featureTypeA = command(RevObjectParse.class).setObjectId(nodeRefA.getMetadataId())
            .call(RevFeatureType.class);
    checkArgument(featureTypeA.isPresent(), "Invalid reference: %s", nodeRefA);

    Optional<RevFeatureType> featureTypeB = command(RevObjectParse.class).setObjectId(nodeRefB.getMetadataId())
            .call(RevFeatureType.class);
    checkArgument(featureTypeB.isPresent(), "Invalid reference: %s", nodeRefB);

    Optional<RevFeatureType> ancestorFeatureType = command(RevObjectParse.class)
            .setObjectId(ancestorRef.getMetadataId()).call(RevFeatureType.class);
    checkArgument(ancestorFeatureType.isPresent(), "Invalid reference: %s", ancestorRef);

    Preconditions.checkArgument(featureTypeA.equals(featureTypeB) && featureTypeA.equals(ancestorFeatureType),
            "Non-matching feature types. Cannot merge");

    return merge(featureA.get(), featureB.get(), ancestorFeature.get(), ancestorFeatureType.get());

}

From source file:org.locationtech.geogig.api.plumbing.merge.MergeFeaturesOp.java

@Override
protected Feature _call() {
    checkNotNull(nodeRefA, "first feature version not specified");
    checkNotNull(nodeRefB, "second feature version not specified");
    checkNotNull(ancestorRef, "ancestor version not specified");
    // String firstPath = removeRef(oldNodeRef.path());
    // String newPath = removeRef(newNodeRef.path());
    checkArgument(nodeRefA.path().equals(nodeRefB.path()),
            "old and new versions do not correspond to the same feature");

    Optional<RevFeature> featureA = command(RevObjectParse.class).setObjectId(nodeRefA.getNode().getObjectId())
            .call(RevFeature.class);
    checkArgument(featureA.isPresent(), "Invalid reference: %s", nodeRefA);

    Optional<RevFeature> featureB = command(RevObjectParse.class).setObjectId(nodeRefB.getNode().getObjectId())
            .call(RevFeature.class);
    checkArgument(featureB.isPresent(), "Invalid reference: %s", nodeRefB);

    Optional<RevFeature> ancestorFeature = command(RevObjectParse.class)
            .setObjectId(ancestorRef.getNode().getObjectId()).call(RevFeature.class);
    checkArgument(ancestorFeature.isPresent(), "Invalid reference: %s", ancestorRef);

    Optional<RevFeatureType> featureTypeA = command(RevObjectParse.class).setObjectId(nodeRefA.getMetadataId())
            .call(RevFeatureType.class);
    checkArgument(featureTypeA.isPresent(), "Invalid reference: %s", nodeRefA);

    Optional<RevFeatureType> featureTypeB = command(RevObjectParse.class).setObjectId(nodeRefB.getMetadataId())
            .call(RevFeatureType.class);
    checkArgument(featureTypeB.isPresent(), "Invalid reference: %s", nodeRefB);

    Optional<RevFeatureType> ancestorFeatureType = command(RevObjectParse.class)
            .setObjectId(ancestorRef.getMetadataId()).call(RevFeatureType.class);
    checkArgument(ancestorFeatureType.isPresent(), "Invalid reference: %s", ancestorRef);

    Preconditions.checkArgument(featureTypeA.equals(featureTypeB) && featureTypeA.equals(ancestorFeatureType),
            "Non-matching feature types. Cannot merge");

    return merge(featureA.get(), featureB.get(), ancestorFeature.get(), ancestorFeatureType.get());

}

From source file:com.twitter.aurora.scheduler.storage.log.LogStorage.java

@Timed("scheduler_save_host_attribute")
@Override/*w  w w.  java 2s .c  o  m*/
public void saveHostAttributes(final HostAttributes attrs) {
    write(new MutateWork.NoResult.Quiet() {
        @Override
        protected void execute(MutableStoreProvider unused) {
            // Pass the updated attributes upstream, and then check if the stored value changes.
            // We do this since different parts of the system write partial HostAttributes objects
            // and they are merged together internally.
            // TODO(William Farner): Split out a separate method
            //                       saveAttributes(String host, Iterable<Attributes>) to simplify this.
            Optional<HostAttributes> saved = LogStorage.super.getHostAttributes(attrs.getHost());
            LogStorage.super.saveHostAttributes(attrs);
            Optional<HostAttributes> updated = LogStorage.super.getHostAttributes(attrs.getHost());
            if (!saved.equals(updated)) {
                log(Op.saveHostAttributes(new SaveHostAttributes(updated.get())));
            }
        }
    });
}

From source file:com.github.filosganga.geogson.gson.PositionsAdapter.java

private Positions parsePositions(JsonReader in) throws IOException {

    Optional<Positions> parsed = Optional.absent();

    if (in.peek() != JsonToken.BEGIN_ARRAY) {
        throw new IllegalArgumentException("The given json is not a valid positions");
    }/*from w  w w . jav a  2  s  .co m*/

    in.beginArray();
    if (in.peek() == JsonToken.NUMBER) {
        parsed = Optional.of(parseSinglePosition(in));
    } else if (in.peek() == JsonToken.BEGIN_ARRAY) {
        while (in.hasNext()) {
            Positions thisPositions = parsePositions(in);
            // fix bug #30: according to the recursion (i.e. the array structure;
            // recognize that we came from a recursion because no parsed has no
            // value yet): convert the already parsed Positions to the
            // LinearPositions/AreaPositions matching the recursion level
            if (parsed.equals(Optional.absent()) && thisPositions instanceof LinearPositions) {
                AreaPositions areaPositions = new AreaPositions(
                        ImmutableList.of((LinearPositions) thisPositions));
                parsed = Optional.of((Positions) areaPositions);
            } else if (parsed.equals(Optional.absent()) && thisPositions instanceof AreaPositions) {
                MultiDimensionalPositions multiPositions = new MultiDimensionalPositions(
                        ImmutableList.of((AreaPositions) thisPositions));
                parsed = Optional.of((Positions) multiPositions);
            } else {
                // mergeFn() does all the rest, if parsed has a value
                parsed = parsed.transform(mergeFn(thisPositions)).or(Optional.of(thisPositions));
            }

        }
    }

    in.endArray();

    return parsed.orNull();
}

From source file:dagger2.internal.codegen.BindingGraphValidator.java

/**
 * Validates that the scope (if any) of this component are compatible with the scopes of the
 * bindings available in this component/*from  ww w  .  ja v  a  2s.c o  m*/
 */
void validateComponentScope(final BindingGraph subject,
        final ValidationReport.Builder<BindingGraph> reportBuilder,
        ImmutableMap<BindingKey, ResolvedBindings> resolvedBindings) {
    Optional<Equivalence.Wrapper<AnnotationMirror>> componentScope = subject.componentDescriptor()
            .wrappedScope();
    ImmutableSet.Builder<String> incompatiblyScopedMethodsBuilder = ImmutableSet.builder();
    for (ResolvedBindings bindings : resolvedBindings.values()) {
        if (bindings.bindingKey().kind().equals(BindingKey.Kind.CONTRIBUTION)) {
            for (ContributionBinding contributionBinding : bindings.ownedContributionBindings()) {
                if (contributionBinding instanceof ProvisionBinding) {
                    ProvisionBinding provisionBinding = (ProvisionBinding) contributionBinding;
                    if (provisionBinding.scope().isPresent()
                            && !componentScope.equals(provisionBinding.wrappedScope())) {
                        // Scoped components cannot reference bindings to @Provides methods or @Inject
                        // types decorated by a different scope annotation. Unscoped components cannot
                        // reference to scoped @Provides methods or @Inject types decorated by any
                        // scope annotation.
                        switch (provisionBinding.bindingKind()) {
                        case PROVISION:
                            ExecutableElement provisionMethod = MoreElements
                                    .asExecutable(provisionBinding.bindingElement());
                            incompatiblyScopedMethodsBuilder
                                    .add(methodSignatureFormatter.format(provisionMethod));
                            break;
                        case INJECTION:
                            incompatiblyScopedMethodsBuilder
                                    .add(stripCommonTypePrefixes(provisionBinding.scope().get().toString())
                                            + " class "
                                            + provisionBinding.bindingTypeElement().getQualifiedName());
                            break;
                        default:
                            throw new IllegalStateException();
                        }
                    }
                }
            }
        }
    }
    ImmutableSet<String> incompatiblyScopedMethods = incompatiblyScopedMethodsBuilder.build();
    if (!incompatiblyScopedMethods.isEmpty()) {
        TypeElement componentType = subject.componentDescriptor().componentDefinitionType();
        StringBuilder message = new StringBuilder(componentType.getQualifiedName());
        if (componentScope.isPresent()) {
            message.append(" scoped with ");
            message.append(stripCommonTypePrefixes(ErrorMessages.format(componentScope.get().get())));
            message.append(" may not reference bindings with different scopes:\n");
        } else {
            message.append(" (unscoped) may not reference scoped bindings:\n");
        }
        for (String method : incompatiblyScopedMethods) {
            message.append(ErrorMessages.INDENT).append(method).append("\n");
        }
        reportBuilder.addItem(message.toString(), componentType,
                subject.componentDescriptor().componentAnnotation());
    }
}

From source file:org.locationtech.geogig.plumbing.diff.FeatureDiff.java

/**
 * // ww  w .  j  a  v a 2  s  .  c om
 * @param path the full path to the feature, including its name
 * @param newRevFeature the new version of the feature
 * @param oldRevFeature the old version of the feature
 * @param newRevFeatureType the new version of the feature type
 * @param oldRevFeatureType the old version of the feature type
 * @param all - true if all attributes should be added regardless of change
 */
public FeatureDiff(String path, @Nullable RevFeature newRevFeature, @Nullable RevFeature oldRevFeature,
        @Nullable RevFeatureType newRevFeatureType, @Nullable RevFeatureType oldRevFeatureType, boolean all) {

    this.path = path;
    this.newRevFeature = newRevFeature;
    this.oldRevFeature = oldRevFeature;
    this.newFeatureType = newRevFeatureType;
    this.oldFeatureType = oldRevFeatureType;
    diffs = new HashMap<PropertyDescriptor, AttributeDiff>();

    if (newRevFeature == null) {
        Preconditions.checkArgument(oldRevFeature != null, "A feature must be provided");
        Preconditions.checkArgument(oldRevFeatureType != null, "Old feature type must be provided.");

        ImmutableList<PropertyDescriptor> oldAttributes = oldRevFeatureType.descriptors();

        for (int i = 0; i < oldAttributes.size(); i++) {
            Optional<Object> oldValue = oldRevFeature.get(i);
            PropertyDescriptor descriptor = oldAttributes.get(i);
            if (Geometry.class.isAssignableFrom(descriptor.getType().getBinding())) {
                diffs.put(descriptor, new GeometryAttributeDiff((Geometry) oldValue.orNull(), (Geometry) null));
            } else {
                diffs.put(oldAttributes.get(i), new GenericAttributeDiffImpl(oldValue.orNull(), null));
            }
        }
    } else if (oldRevFeature == null) {
        Preconditions.checkArgument(newRevFeatureType != null, "New feature type must be provided.");

        ImmutableList<PropertyDescriptor> newAttributes = newRevFeatureType.descriptors();

        for (int i = 0; i < newAttributes.size(); i++) {
            Optional<Object> newValue = newRevFeature.get(i);
            PropertyDescriptor descriptor = newAttributes.get(i);
            if (Geometry.class.isAssignableFrom(descriptor.getType().getBinding())) {
                diffs.put(descriptor, new GeometryAttributeDiff((Geometry) null, (Geometry) newValue.orNull()));
            } else {
                diffs.put(newAttributes.get(i), new GenericAttributeDiffImpl(null, newValue.orNull()));
            }
        }
    } else {
        ImmutableList<PropertyDescriptor> oldAttributes = oldRevFeatureType.descriptors();
        ImmutableList<PropertyDescriptor> newAttributes = newRevFeatureType.descriptors();
        BitSet updatedAttributes = new BitSet(newRevFeature.size());
        for (int i = 0; i < oldAttributes.size(); i++) {
            Optional<Object> oldValue = oldRevFeature.get(i);
            int idx = newAttributes.indexOf(oldAttributes.get(i));
            if (idx != -1) {
                Optional<Object> newValue = newRevFeature.get(idx);
                if (!oldValue.equals(newValue) || all) {
                    if (Geometry.class.isAssignableFrom(oldAttributes.get(i).getType().getBinding())) {
                        diffs.put(oldAttributes.get(i), new GeometryAttributeDiff((Geometry) oldValue.orNull(),
                                (Geometry) newValue.orNull()));
                    } else {
                        diffs.put(oldAttributes.get(i),
                                new GenericAttributeDiffImpl(oldValue.orNull(), newValue.orNull()));
                    }
                }
                updatedAttributes.set(idx);
            } else {
                if (Geometry.class.isAssignableFrom(oldAttributes.get(i).getType().getBinding())) {
                    diffs.put(oldAttributes.get(i),
                            new GeometryAttributeDiff((Geometry) oldValue.orNull(), (Geometry) null));
                } else {
                    diffs.put(oldAttributes.get(i), new GenericAttributeDiffImpl(oldValue.orNull(), null));
                }
            }
        }
        updatedAttributes.flip(0, newRevFeature.size());
        for (int i = updatedAttributes.nextSetBit(0); i >= 0; i = updatedAttributes.nextSetBit(i + 1)) {
            PropertyDescriptor descriptor = newAttributes.get(i);
            if (Geometry.class.isAssignableFrom(descriptor.getType().getBinding())) {
                diffs.put(descriptor,
                        new GeometryAttributeDiff((Geometry) null, (Geometry) newRevFeature.get(i).orNull()));
            } else {
                diffs.put(descriptor, new GenericAttributeDiffImpl(null, newRevFeature.get(i).orNull()));
            }
        }
    }

}