Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (C) 2011 The Guava Authors
   *
   * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
   * in compliance with the License. You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software distributed under the
  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  * express or implied. See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.google.common.collect;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
 
 import java.util.List;
 import java.util.Set;
Utilities for dealing with sorted collections of all types.

Author(s):
Louis Wasserman
 
 final class SortedIterables {
   private SortedIterables() {}

  
Returns true if elements is a sorted collection using an ordering equivalent to comparator.
 
   public static boolean hasSameComparator(Comparator<?> comparatorIterable<?> elements) {
     checkNotNull(comparator);
     checkNotNull(elements);
     Comparator<?> comparator2;
     if (elements instanceof SortedSet) {
       SortedSet<?> sortedSet = (SortedSet<?>) elements;
       comparator2 = sortedSet.comparator();
       if (comparator2 == null) {
         comparator2 = (Comparator) Ordering.natural();
       }
     } else if (elements instanceof SortedIterable) {
       comparator2 = ((SortedIterable<?>) elements).comparator();
     } else {
       comparator2 = null;
     }
     return comparator.equals(comparator2);
   }

  
Returns a sorted collection of the unique elements according to the specified comparator. Does not check for null.
 
   @SuppressWarnings("unchecked")
   public static <E> Collection<E> sortedUnique(
       Comparator<? super E> comparatorIterator<E> elements) {
     SortedSet<E> sortedSet = Sets.newTreeSet(comparator);
     Iterators.addAll(sortedSetelements);
     return sortedSet;
   }

  
Returns a sorted collection of the unique elements according to the specified comparator. Does not check for null.
 
   @SuppressWarnings("unchecked")
   public static <E> Collection<E> sortedUnique(
       Comparator<? super E> comparatorIterable<E> elements) {
     if (elements instanceof Multiset) {
       elements = ((Multiset<E>) elements).elementSet();
     }
     if (elements instanceof Set) {
       if (hasSameComparator(comparatorelements)) {
         return (Set<E>) elements;
       }
       List<E> list = Lists.newArrayList(elements);
       Collections.sort(listcomparator);
       return list;
     }
     E[] array = (E[]) Iterables.toArray(elements);
     if (!hasSameComparator(comparatorelements)) {
       Arrays.sort(arraycomparator);
     }
     return uniquifySortedArray(comparatorarray);
   }
 
  private static <E> Collection<E> uniquifySortedArray(
      Comparator<? super E> comparator, E[] array) {
    if (array.length == 0) {
      return Collections.emptySet();
    }
    int length = 1;
    for (int i = 1; i < array.lengthi++) {
      int cmp = comparator.compare(array[i], array[length - 1]);
      if (cmp != 0) {
        array[length++] = array[i];
      }
    }
    if (length < array.length) {
      array = ObjectArrays.arraysCopyOf(arraylength);
    }
    return Arrays.asList(array);
  }

  
Returns a collection of multiset entries representing the counts of the distinct elements, in sorted order. Does not check for null.
  public static <E> Collection<Multiset.Entry<E>> sortedCounts(
      Comparator<? super E> comparatorIterator<E> elements) {
    TreeMultiset<E> multiset = TreeMultiset.create(comparator);
    Iterators.addAll(multisetelements);
    return multiset.entrySet();
  }
  
  
Returns a collection of multiset entries representing the counts of the distinct elements, in sorted order. Does not check for null.
  public static <E> Collection<Multiset.Entry<E>> sortedCounts(
      Comparator<? super E> comparatorIterable<E> elements) {
    if (elements instanceof Multiset) {
      Multiset<E> multiset = (Multiset<E>) elements;
      if (hasSameComparator(comparatorelements)) {
        return multiset.entrySet();
      }
      List<Multiset.Entry<E>> entries = Lists.newArrayList(multiset.entrySet());
      Collections.sort(
          entries, Ordering.from(comparator).onResultOf(new Function<Multiset.Entry<E>, E>() {
            @Override
            public E apply(Entry<E> entry) {
              return entry.getElement();
            }
          }));
      return entries;
    } else if (elements instanceof Set) { // known distinct
      Collection<E> sortedElements;
      if (hasSameComparator(comparatorelements)) {
        sortedElements = (Collection<E>) elements;
      } else {
        List<E> list = Lists.newArrayList(elements);
        Collections.sort(listcomparator);
        sortedElements = list;
      }
      return singletonEntries(sortedElements);
    } else if (hasSameComparator(comparatorelements)) {
      E current = null;
      int currentCount = 0;
      List<Multiset.Entry<E>> sortedEntries = Lists.newArrayList();
      for (E e : elements) {
        if (currentCount > 0) {
          if (comparator.compare(currente) == 0) {
            currentCount++;
          } else {
            sortedEntries.add(Multisets.immutableEntry(currentcurrentCount));
            current = e;
            currentCount = 1;
          }
        } else {
          current = e;
          currentCount = 1;
        }
      }
      if (currentCount > 0) {
        sortedEntries.add(Multisets.immutableEntry(currentcurrentCount));
      }
      return sortedEntries;
    }
    TreeMultiset<E> multiset = TreeMultiset.create(comparator);
    Iterables.addAll(multisetelements);
    return multiset.entrySet();
  }
  static <E> Collection<Multiset.Entry<E>> singletonEntries(Collection<E> set) {
    return Collections2.transform(setnew Function<E, Multiset.Entry<E>>() {
      @Override
      public Entry<E> apply(E elem) {
        return Multisets.immutableEntry(elem, 1);
      }
    });
  }
New to GrepCode? Check out our FAQ X