Java Code Examples for java.util.Comparator.equals()

The following are Jave code examples for showing how to use equals() of the java.util.Comparator class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: sstore-soft   File: ImmutableSortedMultiset.java   View Source Code Vote up 6 votes
/**
 * Returns an immutable sorted multiset containing the given elements sorted by the given {@code
 * Comparator}. This method iterates over {@code elements} at most once.
 *
 * <p>Despite the method name, this method attempts to avoid actually copying the data when it is
 * safe to do so. The exact circumstances under which a copy will or will not be performed are
 * undocumented and subject to change.
 *
 * @throws NullPointerException if {@code comparator} or any of {@code elements} is null
 */
public static <E> ImmutableSortedMultiset<E> copyOf(
    Comparator<? super E> comparator, Iterable<? extends E> elements) {
  if (elements instanceof ImmutableSortedMultiset) {
    @SuppressWarnings("unchecked") // immutable collections are always safe for covariant casts
    ImmutableSortedMultiset<E> multiset = (ImmutableSortedMultiset<E>) elements;
    if (comparator.equals(multiset.comparator())) {
      if (multiset.isPartialView()) {
        return copyOfSortedEntries(comparator, multiset.entrySet().asList());
      } else {
        return multiset;
      }
    }
  }
  elements = Lists.newArrayList(elements); // defensive copy
  TreeMultiset<E> sortedCopy = TreeMultiset.create(checkNotNull(comparator));
  Iterables.addAll(sortedCopy, elements);
  return copyOfSortedEntries(comparator, sortedCopy.entrySet());
}
 
Example 2
Project: bbst-showdown   File: TreeMapBST.java   View Source Code Vote up 6 votes
/**
 * Copies all of the mappings from the specified map to this map.
 * These mappings replace any mappings that this map had for any
 * of the keys currently in the specified map.
 *
 * @param  map mappings to be stored in this map
 * @throws ClassCastException if the class of a key or value in
 *         the specified map prevents it from being stored in this map
 * @throws NullPointerException if the specified map is null or
 *         the specified map contains a null key and this map does not
 *         permit null keys
 */
public void putAll(Map<? extends K, ? extends V> map) {
    int mapSize = map.size();
    if (size==0 && mapSize!=0 && map instanceof SortedMap) {
        Comparator<?> c = ((SortedMap<?,?>)map).comparator();
        if (c == comparator || (c != null && c.equals(comparator))) {
            ++modCount;
//            try {
//                buildFromSorted(mapSize, map.entrySet().iterator(), null, null);
//            } catch (java.io.IOException cannotHappen) {
//            } catch (ClassNotFoundException cannotHappen) {
//            }
            return;
        }
    }
    super.putAll(map);
}
 
Example 3
Project: guava-mock   File: ImmutableSortedMultiset.java   View Source Code Vote up 6 votes
/**
 * Returns an immutable sorted multiset containing the given elements sorted by the given {@code
 * Comparator}. This method iterates over {@code elements} at most once.
 *
 * <p>Despite the method name, this method attempts to avoid actually copying the data when it is
 * safe to do so. The exact circumstances under which a copy will or will not be performed are
 * undocumented and subject to change.
 *
 * @throws NullPointerException if {@code comparator} or any of {@code elements} is null
 */
public static <E> ImmutableSortedMultiset<E> copyOf(
    Comparator<? super E> comparator, Iterable<? extends E> elements) {
  if (elements instanceof ImmutableSortedMultiset) {
    @SuppressWarnings("unchecked") // immutable collections are always safe for covariant casts
    ImmutableSortedMultiset<E> multiset = (ImmutableSortedMultiset<E>) elements;
    if (comparator.equals(multiset.comparator())) {
      if (multiset.isPartialView()) {
        return copyOfSortedEntries(comparator, multiset.entrySet().asList());
      } else {
        return multiset;
      }
    }
  }
  elements = Lists.newArrayList(elements); // defensive copy
  TreeMultiset<E> sortedCopy = TreeMultiset.create(checkNotNull(comparator));
  Iterables.addAll(sortedCopy, elements);
  return copyOfSortedEntries(comparator, sortedCopy.entrySet());
}
 
Example 4
Project: guava-mock   File: ImmutableSortedMap.java   View Source Code Vote up 6 votes
private static <K, V> ImmutableSortedMap<K, V> copyOfInternal(
    Map<? extends K, ? extends V> map, Comparator<? super K> comparator) {
  boolean sameComparator = false;
  if (map instanceof SortedMap) {
    SortedMap<?, ?> sortedMap = (SortedMap<?, ?>) map;
    Comparator<?> comparator2 = sortedMap.comparator();
    sameComparator =
        (comparator2 == null)
            ? comparator == NATURAL_ORDER
            : comparator.equals(comparator2);
  }

  if (sameComparator && (map instanceof ImmutableSortedMap)) {
    // TODO(kevinb): Prove that this cast is safe, even though
    // Collections.unmodifiableSortedMap requires the same key type.
    @SuppressWarnings("unchecked")
    ImmutableSortedMap<K, V> kvMap = (ImmutableSortedMap<K, V>) map;
    if (!kvMap.isPartialView()) {
      return kvMap;
    }
  }
  return fromEntries(comparator, sameComparator, map.entrySet());
}
 
Example 5
Project: guava-mock   File: ImmutableSortedMap.java   View Source Code Vote up 6 votes
private static <K, V> ImmutableSortedMap<K, V> copyOfInternal(
    Map<? extends K, ? extends V> map, Comparator<? super K> comparator) {

  if (map instanceof ImmutableSortedMap) {
    // TODO: Prove that this cast is safe, even though
    // Collections.unmodifiableSortedMap requires the same key type.
    @SuppressWarnings("unchecked")
    ImmutableSortedMap<K, V> kvMap = (ImmutableSortedMap<K, V>) map;
    Comparator<?> comparator2 = kvMap.comparator();
    boolean sameComparator = (comparator2 == null)
        ? comparator == NATURAL_ORDER
        : comparator.equals(comparator2);
    if (sameComparator) {
      return kvMap;
    }
  }

  SortedMap<K, V> delegate = newModifiableDelegate(comparator);
  for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
    putEntryWithChecks(delegate, entry);
  }
  return newView(unmodifiableSortedMap(delegate), comparator);
}
 
Example 6
Project: ciguan   File: TreeList.java   View Source Code Vote up 6 votes
/**
   * Adds all of the elements in the specified collection to this list.
   *
   * @param c elements to be added
   * @return <tt>true</tt> if this list changed as a result of the call.
   */
  @SuppressWarnings("unchecked")
public boolean addAll(Collection<? extends E> c)
  {
    if (m.size() == 0 && c.size() > 0 && c instanceof SortedList &&
      m instanceof IndexedTreeMap)
    {
      SortedList<Map.Entry<E,Object>> list = (SortedList<Map.Entry<E,Object>>)c;
      IndexedTreeMap<E,Object> map = (IndexedTreeMap<E,Object>)m;
      Comparator<? super E> cc = (Comparator<E>)list.comparator();
      Comparator<? super E> mc = map.comparator();
      if (cc == mc || (cc != null && cc.equals(mc)))
      {
        map.addAllForTreeList(list, PRESENT);
        return true;
      }
    }
    return super.addAll(c);
  }
 
Example 7
Project: s-store   File: ImmutableSortedMultiset.java   View Source Code Vote up 6 votes
/**
 * Returns an immutable sorted multiset containing the given elements sorted by the given {@code
 * Comparator}. This method iterates over {@code elements} at most once.
 *
 * <p>Despite the method name, this method attempts to avoid actually copying the data when it is
 * safe to do so. The exact circumstances under which a copy will or will not be performed are
 * undocumented and subject to change.
 *
 * @throws NullPointerException if {@code comparator} or any of {@code elements} is null
 */
public static <E> ImmutableSortedMultiset<E> copyOf(
    Comparator<? super E> comparator, Iterable<? extends E> elements) {
  if (elements instanceof ImmutableSortedMultiset) {
    @SuppressWarnings("unchecked") // immutable collections are always safe for covariant casts
    ImmutableSortedMultiset<E> multiset = (ImmutableSortedMultiset<E>) elements;
    if (comparator.equals(multiset.comparator())) {
      if (multiset.isPartialView()) {
        return copyOfSortedEntries(comparator, multiset.entrySet().asList());
      } else {
        return multiset;
      }
    }
  }
  elements = Lists.newArrayList(elements); // defensive copy
  TreeMultiset<E> sortedCopy = TreeMultiset.create(checkNotNull(comparator));
  Iterables.addAll(sortedCopy, elements);
  return copyOfSortedEntries(comparator, sortedCopy.entrySet());
}
 
Example 8
Project: googles-monorepo-demo   File: ImmutableSortedMultiset.java   View Source Code Vote up 6 votes
/**
 * Returns an immutable sorted multiset containing the given elements sorted by the given {@code
 * Comparator}. This method iterates over {@code elements} at most once.
 *
 * <p>Despite the method name, this method attempts to avoid actually copying the data when it is
 * safe to do so. The exact circumstances under which a copy will or will not be performed are
 * undocumented and subject to change.
 *
 * @throws NullPointerException if {@code comparator} or any of {@code elements} is null
 */
public static <E> ImmutableSortedMultiset<E> copyOf(
    Comparator<? super E> comparator, Iterable<? extends E> elements) {
  if (elements instanceof ImmutableSortedMultiset) {
    @SuppressWarnings("unchecked") // immutable collections are always safe for covariant casts
    ImmutableSortedMultiset<E> multiset = (ImmutableSortedMultiset<E>) elements;
    if (comparator.equals(multiset.comparator())) {
      if (multiset.isPartialView()) {
        return copyOfSortedEntries(comparator, multiset.entrySet().asList());
      } else {
        return multiset;
      }
    }
  }
  elements = Lists.newArrayList(elements); // defensive copy
  TreeMultiset<E> sortedCopy = TreeMultiset.create(checkNotNull(comparator));
  Iterables.addAll(sortedCopy, elements);
  return copyOfSortedEntries(comparator, sortedCopy.entrySet());
}
 
Example 9
Project: googles-monorepo-demo   File: ImmutableSortedMap.java   View Source Code Vote up 6 votes
private static <K, V> ImmutableSortedMap<K, V> copyOfInternal(
    Map<? extends K, ? extends V> map, Comparator<? super K> comparator) {
  boolean sameComparator = false;
  if (map instanceof SortedMap) {
    SortedMap<?, ?> sortedMap = (SortedMap<?, ?>) map;
    Comparator<?> comparator2 = sortedMap.comparator();
    sameComparator =
        (comparator2 == null)
            ? comparator == NATURAL_ORDER
            : comparator.equals(comparator2);
  }

  if (sameComparator && (map instanceof ImmutableSortedMap)) {
    // TODO(kevinb): Prove that this cast is safe, even though
    // Collections.unmodifiableSortedMap requires the same key type.
    @SuppressWarnings("unchecked")
    ImmutableSortedMap<K, V> kvMap = (ImmutableSortedMap<K, V>) map;
    if (!kvMap.isPartialView()) {
      return kvMap;
    }
  }
  return fromEntries(comparator, sameComparator, map.entrySet());
}
 
Example 10
Project: googles-monorepo-demo   File: ImmutableSortedMap.java   View Source Code Vote up 6 votes
private static <K, V> ImmutableSortedMap<K, V> copyOfInternal(
    Map<? extends K, ? extends V> map, Comparator<? super K> comparator) {

  if (map instanceof ImmutableSortedMap) {
    // TODO: Prove that this cast is safe, even though
    // Collections.unmodifiableSortedMap requires the same key type.
    @SuppressWarnings("unchecked")
    ImmutableSortedMap<K, V> kvMap = (ImmutableSortedMap<K, V>) map;
    Comparator<?> comparator2 = kvMap.comparator();
    boolean sameComparator = (comparator2 == null)
        ? comparator == NATURAL_ORDER
        : comparator.equals(comparator2);
    if (sameComparator) {
      return kvMap;
    }
  }

  SortedMap<K, V> delegate = newModifiableDelegate(comparator);
  for (Entry<? extends K, ? extends V> entry : map.entrySet()) {
    putEntryWithChecks(delegate, entry);
  }
  return newView(unmodifiableSortedMap(delegate), comparator);
}
 
Example 11
Project: sstore-soft   File: SortedIterables.java   View Source Code Vote up 5 votes
/**
 * Returns {@code true} if {@code elements} is a sorted collection using an ordering equivalent
 * to {@code comparator}.
 */
public static boolean hasSameComparator(Comparator<?> comparator, Iterable<?> elements) {
  checkNotNull(comparator);
  checkNotNull(elements);
  Comparator<?> comparator2;
  if (elements instanceof SortedSet) {
    comparator2 = comparator((SortedSet<?>) elements);
  } else if (elements instanceof SortedIterable) {
    comparator2 = ((SortedIterable<?>) elements).comparator();
  } else {
    return false;
  }
  return comparator.equals(comparator2);
}
 
Example 12
Project: sstore-soft   File: ImmutableSortedMap.java   View Source Code Vote up 5 votes
private static <K, V> ImmutableSortedMap<K, V> copyOfInternal(
    Map<? extends K, ? extends V> map, Comparator<? super K> comparator) {
  boolean sameComparator = false;
  if (map instanceof SortedMap) {
    SortedMap<?, ?> sortedMap = (SortedMap<?, ?>) map;
    Comparator<?> comparator2 = sortedMap.comparator();
    sameComparator = (comparator2 == null)
        ? comparator == NATURAL_ORDER
        : comparator.equals(comparator2);
  }

  if (sameComparator && (map instanceof ImmutableSortedMap)) {
    // TODO(kevinb): Prove that this cast is safe, even though
    // Collections.unmodifiableSortedMap requires the same key type.
    @SuppressWarnings("unchecked")
    ImmutableSortedMap<K, V> kvMap = (ImmutableSortedMap<K, V>) map;
    if (!kvMap.isPartialView()) {
      return kvMap;
    }
  }

  // "adding" type params to an array of a raw type should be safe as
  // long as no one can ever cast that same array instance back to a
  // raw type.
  @SuppressWarnings("unchecked")
  Entry<K, V>[] entries = map.entrySet().toArray(new Entry[0]);

  return fromEntries(comparator, sameComparator, entries.length, entries);
}
 
Example 13
Project: guava-mock   File: SortedIterables.java   View Source Code Vote up 5 votes
/**
 * Returns {@code true} if {@code elements} is a sorted collection using an ordering equivalent
 * to {@code comparator}.
 */
public static boolean hasSameComparator(Comparator<?> comparator, Iterable<?> elements) {
  checkNotNull(comparator);
  checkNotNull(elements);
  Comparator<?> comparator2;
  if (elements instanceof SortedSet) {
    comparator2 = comparator((SortedSet<?>) elements);
  } else if (elements instanceof SortedIterable) {
    comparator2 = ((SortedIterable<?>) elements).comparator();
  } else {
    return false;
  }
  return comparator.equals(comparator2);
}
 
Example 14
Project: guava-mock   File: ImmutableSortedSet.java   View Source Code Vote up 5 votes
private static boolean hasSameComparator(
    Iterable<?> elements, Comparator<?> comparator) {
  if (elements instanceof SortedSet) {
    SortedSet<?> sortedSet = (SortedSet<?>) elements;
    Comparator<?> comparator2 = sortedSet.comparator();
    return (comparator2 == null)
        ? comparator == Ordering.natural()
        : comparator.equals(comparator2);
  }
  return false;
}
 
Example 15
Project: s-store   File: SortedIterables.java   View Source Code Vote up 5 votes
/**
 * Returns {@code true} if {@code elements} is a sorted collection using an ordering equivalent
 * to {@code comparator}.
 */
public static boolean hasSameComparator(Comparator<?> comparator, Iterable<?> elements) {
  checkNotNull(comparator);
  checkNotNull(elements);
  Comparator<?> comparator2;
  if (elements instanceof SortedSet) {
    comparator2 = comparator((SortedSet<?>) elements);
  } else if (elements instanceof SortedIterable) {
    comparator2 = ((SortedIterable<?>) elements).comparator();
  } else {
    return false;
  }
  return comparator.equals(comparator2);
}
 
Example 16
Project: s-store   File: ImmutableSortedMap.java   View Source Code Vote up 5 votes
private static <K, V> ImmutableSortedMap<K, V> copyOfInternal(
    Map<? extends K, ? extends V> map, Comparator<? super K> comparator) {
  boolean sameComparator = false;
  if (map instanceof SortedMap) {
    SortedMap<?, ?> sortedMap = (SortedMap<?, ?>) map;
    Comparator<?> comparator2 = sortedMap.comparator();
    sameComparator = (comparator2 == null)
        ? comparator == NATURAL_ORDER
        : comparator.equals(comparator2);
  }

  if (sameComparator && (map instanceof ImmutableSortedMap)) {
    // TODO(kevinb): Prove that this cast is safe, even though
    // Collections.unmodifiableSortedMap requires the same key type.
    @SuppressWarnings("unchecked")
    ImmutableSortedMap<K, V> kvMap = (ImmutableSortedMap<K, V>) map;
    if (!kvMap.isPartialView()) {
      return kvMap;
    }
  }

  // "adding" type params to an array of a raw type should be safe as
  // long as no one can ever cast that same array instance back to a
  // raw type.
  @SuppressWarnings("unchecked")
  Entry<K, V>[] entries = map.entrySet().toArray(new Entry[0]);

  return fromEntries(comparator, sameComparator, entries.length, entries);
}
 
Example 17
Project: googles-monorepo-demo   File: SortedIterables.java   View Source Code Vote up 5 votes
/**
 * Returns {@code true} if {@code elements} is a sorted collection using an ordering equivalent
 * to {@code comparator}.
 */
public static boolean hasSameComparator(Comparator<?> comparator, Iterable<?> elements) {
  checkNotNull(comparator);
  checkNotNull(elements);
  Comparator<?> comparator2;
  if (elements instanceof SortedSet) {
    comparator2 = comparator((SortedSet<?>) elements);
  } else if (elements instanceof SortedIterable) {
    comparator2 = ((SortedIterable<?>) elements).comparator();
  } else {
    return false;
  }
  return comparator.equals(comparator2);
}
 
Example 18
Project: googles-monorepo-demo   File: ImmutableSortedSet.java   View Source Code Vote up 5 votes
private static boolean hasSameComparator(
    Iterable<?> elements, Comparator<?> comparator) {
  if (elements instanceof SortedSet) {
    SortedSet<?> sortedSet = (SortedSet<?>) elements;
    Comparator<?> comparator2 = sortedSet.comparator();
    return (comparator2 == null)
        ? comparator == Ordering.natural()
        : comparator.equals(comparator2);
  }
  return false;
}