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

The following examples show how to use java.util.Comparator#equals() . These examples are extracted from open source projects. 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 Project: chart-fx   File: IndexedTreeSet.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Adds all of the elements in the specified collection to this set.
 *
 * @param c collection containing elements to be added to this set
 * @return {@code true} if this set changed as a result of the call
 * @throws ClassCastException if the elements provided cannot be compared with the elements currently in the set
 * @throws NullPointerException if the specified collection is null or if any element is null and this set uses
 *         natural ordering, or its comparator does not permit null elements
 */
@Override
public boolean addAll(Collection<? extends E> c) {
    // Use linear-time version if applicable
    if (m.isEmpty() && !c.isEmpty() && c instanceof SortedSet && m instanceof IndexedTreeMap) {
        final SortedSet<? extends E> set = (SortedSet<? extends E>) c;
        final IndexedTreeMap<E, Object> map = (IndexedTreeMap<E, Object>) m;
        final Comparator<? super E> cc = (Comparator<? super E>) set.comparator();
        final Comparator<? super E> mc = map.comparator();
        if (cc == mc || cc != null && cc.equals(mc)) {
            map.addAllForTreeSet(set, IndexedTreeSet.PRESENT);
            return true;
        }
    }
    return super.addAll(c);
}
 
Example 2
Source Project: chart-fx   File: IndexedTreeMap.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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
 */
@Override
public void putAll(Map<? extends K, ? extends V> map) {
    final int mapSize = map.size();
    if (size == 0 && mapSize != 0 && map instanceof SortedMap) {
        final Comparator<?> c = ((SortedMap<? extends K, ? extends V>) map).comparator();
        if (c == comparator || c != null && c.equals(comparator)) {
            ++modCount;
            try {
                buildFromSorted(mapSize, map.entrySet().iterator(), null, null);
            } catch (java.io.IOException | ClassNotFoundException cannotHappen) {
                // cannot happen
            }
            return;
        }
    }
    super.putAll(map);
}
 
Example 3
/**
 * Adds all of the elements in the specified collection to this set.
 *
 * @param c collection containing elements to be added to this set
 * @return {@code true} if this set changed as a result of the call
 * @throws ClassCastException   if the elements provided cannot be compared
 *                              with the elements currently in the set
 * @throws NullPointerException if the specified collection is null or
 *                              if any element is null and this set uses natural ordering, or
 *                              its comparator does not permit null elements
 */
public boolean addAll(Collection<? extends E> c) {
    // Use linear-time version if applicable
    if (m.size() == 0 && c.size() > 0 &&
            c instanceof SortedSet &&
            m instanceof IndexedTreeMap) {
        SortedSet<? extends E> set = (SortedSet<? extends E>) c;
        IndexedTreeMap<E, Object> map = (IndexedTreeMap<E, Object>) m;
        Comparator<? super E> cc = (Comparator<? super E>) set.comparator();
        Comparator<? super E> mc = map.comparator();
        if (cc == mc || (cc != null && cc.equals(mc))) {
            map.addAllForTreeSet(set, PRESENT);
            return true;
        }
    }
    return super.addAll(c);
}
 
Example 4
/**
 * 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 5
/**
 * 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 6
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 7
/**
 * 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 8
/**
 * 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
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
/**
 * 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 11
/**
 * 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 12
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 13
/**
 * 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
/**
 * 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 15
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 16
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 17
/**
 * Get the segment for a child node with the least value for the field
 * corresponding to the comparator's field.
 *
 * @param index
 *            The index of the child node
 * @param order
 *            The comparator with which to sort segments
 * @return A segment whose value for the field that correspond to the
 *         comparator is the least value of the child node
 */
public ISegment getIndex(int index, Comparator<E> order) {
    if (order.equals(SegmentComparators.INTERVAL_START_COMPARATOR)) {
        return new BasicSegment(getChildStart(index), getChildStart(index));
    } else if (order.equals(SegmentComparators.INTERVAL_START_COMPARATOR.reversed())) {
        return new BasicSegment(fChildMaxStart[index], fChildMaxStart[index]);
    } else if (order.equals(SegmentComparators.INTERVAL_END_COMPARATOR)) {
        return new BasicSegment(fChildMinEnd[index], fChildMinEnd[index]);
    } else if (order.equals(SegmentComparators.INTERVAL_END_COMPARATOR.reversed())) {
        return new BasicSegment(getChildEnd(index), getChildEnd(index));
    } else if (order.equals(SegmentComparators.INTERVAL_LENGTH_COMPARATOR)) {
        return new BasicSegment(0, fMinLength[index]);
    } else if (order.equals(SegmentComparators.INTERVAL_LENGTH_COMPARATOR.reversed())) {
        return new BasicSegment(0, fMaxLength[index]);
    }
    // TODO: Don't know what to do with other comparators yet
    return new BasicSegment(getChild(index), getChild(index));
}
 
Example 18
/**
 * Retreives topic statistics while determining whether to retrieve them from the cache or by invoking getTopicStatistics()
 */
public List getGradeStatisticsForStatsListByTopic()
{
	// Determine if something has changed that warrants the cached gradeStatistics to be refreshed
	// Has the selected gradebook assignment or the selected group changed since the gradeStatistics were cached?
	boolean refreshCachedStatistics = !StringUtils.equals(m_gradeStatisticsAssign, selectedAssign) || !StringUtils.equals(m_gradeStatisticsGroup, selectedGroup);
	if (!refreshCachedStatistics)
	{
		// Are we sorting on a different column?
		Comparator comparator = determineComparator();
		if (comparator != null && !comparator.equals(m_comparator))
		{
			refreshCachedStatistics = true;
		}
	}

	if (refreshCachedStatistics)
	{
		// The selected gradebook assignment or group has changed; our gradeStatistics need to be updated
		getTopicStatistics();
	}
	return gradeStatistics;
}
 
Example 19
/**
 * 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 20
Source Project: bazel   File: ImmutableMapCodec.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(
    SerializationContext context, ImmutableMap<?, V> map, CodedOutputStream codedOut)
    throws SerializationException, IOException {
  codedOut.writeInt32NoTag(map.size());
  boolean serializeAsSortedMap = false;
  if (map instanceof ImmutableSortedMap) {
    Comparator<?> comparator = ((ImmutableSortedMap<?, ?>) map).comparator();
    // In practice the comparator seems to always be Ordering.natural(), but be flexible.
    serializeAsSortedMap =
        comparator.equals(Ordering.natural()) || comparator.equals(Comparator.naturalOrder());
  }
  codedOut.writeBoolNoTag(serializeAsSortedMap);
  serializeEntries(context, map.entrySet(), codedOut);
}