Example usage for com.google.common.base Equivalence equivalentTo

List of usage examples for com.google.common.base Equivalence equivalentTo

Introduction

In this page you can find the example usage for com.google.common.base Equivalence equivalentTo.

Prototype

@Beta
public final Predicate<T> equivalentTo(@Nullable T target) 

Source Link

Document

Returns a predicate that evaluates to true if and only if the input is equivalent to target according to this equivalence relation.

Usage

From source file:org.apache.abdera2.common.selector.Selectors.java

/**
 * Returns a Selector<X> for the given Equivalence
 *//*from  w w  w.j  a  v  a2s.c o  m*/
public static <X> Selector<X> equivalentTo(final Equivalence<X> equivalence, final X item) {
    return forPredicate(equivalence.equivalentTo(item));
}

From source file:de.flapdoodle.guava.Merger.java

public static <T> ImmutableList<T> merge(Iterable<? extends T> left, Iterable<? extends T> right,
        Equivalence<? super T> matcher, Foldleft<? super T, T> fold) {
    ImmutableList.Builder<T> builder = ImmutableList.builder();
    Iterable<? extends T> notMerged = right;
    for (T l : left) {
        Iterable<? extends T> matching = Iterables.filter(notMerged, matcher.equivalentTo(l));
        notMerged = Iterables.filter(notMerged, Predicates.not(matcher.equivalentTo(l)));

        boolean noMatching = true;
        for (T r : matching) {
            builder.add(fold.apply(l, r));
            noMatching = false;/*from  w  ww .  j a va2s .c  o  m*/
        }
        if (noMatching) {
            builder.add(l);
        }
    }
    builder.addAll(notMerged);
    return builder.build();
}

From source file:org.apache.abdera2.common.misc.MapRed.java

public static <K, V> Reducer<K, V, K, V> noEquivalentsReducer(final Equivalence<V> equivalence) {
    return new Reducer<K, V, K, V>() {
        final Set<V> set = new LinkedHashSet<V>();

        public void reduce(K key, Iterator<V> vals, Collector<K, V> context) {
            while (vals.hasNext()) {
                V v = vals.next();/* w w  w  .j  a va2  s .  c  om*/
                Predicate<V> ve = equivalence.equivalentTo(v);
                try {
                    Iterables.find(set, ve);
                } catch (NoSuchElementException e) {
                    context.collect(key, v);
                    set.add(v);
                }
            }
        }
    };
}

From source file:com.github.buzztaiki.jenova.InterfaceMethod.java

public List<JCTree.JCExpression> getParamTypes(List<JCTree.JCExpression> typeArgs) {
    Preconditions.checkArgument(typeArgs.size() == clazz.getTypeParameters().size(),
            "typeArgs length for %s should be %s but takes %s", getClazz(), clazz.getTypeParameters().size(),
            typeArgs.size());// w ww  .  ja va  2s.  c  o m

    Iterable<Type> typeParams = types(clazz.getTypeParameters());
    Equivalence<Type> sameType = sameType(types);
    ListBuffer<JCTree.JCExpression> res = new ListBuffer<JCTree.JCExpression>();
    for (Type type : types(method.getParameters())) {
        int i = Iterables.indexOf(typeParams, sameType.equivalentTo(type));
        res.append(i >= 0 ? typeArgs.get(i) : maker.Type(type));
    }
    return res.toList();
}

From source file:com.vmware.appfactory.taskqueue.tasks.state.tasks.FeedScanTask.java

/**
 * Update an existing list of applications or recipes with the contents from
 * a new list. Items no longer in the new list are removed from the old list.
 * Items in the new list not present in the old list are added to it. Items
 * in both are updated, performing a deep copy from the new item into the old
 * item./* www .  j a v a 2  s  . c o  m*/
 *
 *
 * @param oldItems
 * @param newItems
 * @param now
 * @param fnEquals
 * an equivalence relation by which two apps or recipes
 * are considered "the same".  We use this instead of
 * the .equals() method, as we need looser semantics.
 *
 * @return A list of changes that were made, or null if the task looks
 * aborted.
 */
private <T extends AbstractRecord> List<Delta> updateListInPlace(List<T> oldItems, List<T> newItems, long now,
        Equivalence<T> fnEquals) {
    float progressDelta = 100f / (oldItems.size() + newItems.size());
    float progress = 0f;
    List<Delta> deltas = new ArrayList<Delta>(Math.max(oldItems.size(), newItems.size()));

    /* Examine the items we know about already. */
    Iterator<T> it = oldItems.iterator();
    while (it.hasNext()) {
        T oldItem = it.next();

        // note: indexOf uses the object's equals() methods to determine if the
        // old and new object are the same.  When detecting changes from a feed,
        // we have to be more subtle than this, because:
        //  - the user might have changed the app's or recipe's metadata in the meantime
        //  - the feed might have changed the app's or recipe's metadata
        //
        // instead, use the passed-in equivalence function to determine if the
        // old and new record should be considered equivalent or not
        //
        T newItem = Iterables.find(newItems, fnEquals.equivalentTo(oldItem), null);

        if (null == newItem) {
            /* Old item no longer in the list: skip it */
            it.remove();
            deltas.add(Delta.delete);
            _log.debug("{} removed from feed", oldItem);
        } else {
            /* Old item still in the new list: update it */
            int numChanges = oldItem.deepCopy(newItem);
            if (numChanges > 0) {
                deltas.add(Delta.change);
                _log.debug("{} changed in feed", oldItem);
            }
        }

        progress += progressDelta;
        updateProgress(Math.round(progress));
        if (getCurrentTaskState().isAborted()) {
            return null;
        }
    }

    /* Look for new items we don't have yet */
    for (T newItem : newItems) {

        T oldItem = Iterables.find(oldItems, fnEquals.equivalentTo(newItem), null);
        if (null == oldItem) {
            /* A new item we don't have yet */

            // note: if we don't set lastModified, then the value
            //       will be 0 for all Applications in the table.
            //       If the user later adds a second feed without making
            //       any other changes, the second feed will also have
            //       timestamps of 0.  The timestamp here didn't change.
            //       Since the frontend uses the most recent modified timestamp
            //       to tell when it has more AJAX data to load, this means
            //       the user won't see it.
            //
            newItem.setModified(now);

            oldItems.add(newItem);
            deltas.add(Delta.add);
            _log.debug("{} is new in feed", newItem);
        }

        progress += progressDelta;
        updateProgress((int) progress);
        if (getCurrentTaskState().isAborted()) {
            return null;
        }
    }

    return deltas;
}

From source file:org.apache.abdera2.common.date.DateTimes.java

public static Predicate<DateTime> equivalentTo(DateTime dateTime) {
    return Equivalence.equivalentTo(dateTime);
}