Package aQute.lib.collections
Class SortedList<T>
- java.lang.Object
-
- aQute.lib.collections.SortedList<T>
-
- Type Parameters:
T-
- All Implemented Interfaces:
java.lang.Iterable<T>,java.util.Collection<T>,java.util.List<T>,java.util.Set<T>,java.util.SortedSet<T>
public class SortedList<T> extends java.lang.Object implements java.util.SortedSet<T>, java.util.List<T>An immutable list that sorts objects by their natural order or through a comparator. It has convenient methods/constructors to create it from collections and iterators. Why not maintain the lists in their sorted form? Well, TreeMaps are quite expensive ... I once profiled bnd and was shocked how much memory the Jar class took due to the TreeMaps. I could not easily change it unfortunately. The other reason is that Parameters uses a LinkedHashMap because the preferred order should be the declaration order. However, sometimes you need to sort the keys by name. Last, and most important reason, is that sometimes you do not know what collection you have or it is not available in a sort ordering (MultiMap for example) ... I found myself sorting these things over and over again and decided to just make an immutable SortedList that is easy to slice and dice
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classSortedList.It
-
Field Summary
Fields Modifier and Type Field Description private java.util.Comparator<? super T>comparatorprivate static SortedList<?>EMPTYprivate intendprivate T[]listprivate intstartprivate java.lang.Class<?>type
-
Constructor Summary
Constructors Modifier Constructor Description privateSortedList()privateSortedList(SortedList<T> other, int start, int end)SortedList(C... x)SortedList(java.util.Collection<? extends java.lang.Comparable<? super T>> x)SortedList(java.util.Collection<? extends T> x, int start, int end, java.util.Comparator<? super T> cmp)SortedList(java.util.Collection<? extends T> x, java.util.Comparator<? super T> cmp)SortedList(java.util.Comparator<? super T> cmp, T... x)SortedList(T[] x, int start, int end, java.util.Comparator<? super T> cmp)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidadd(int index, T element)Deprecated.booleanadd(T e)booleanaddAll(int index, java.util.Collection<? extends T> c)Deprecated.booleanaddAll(java.util.Collection<? extends T> c)voidclear()java.util.Comparator<? super T>comparator()private intcompare(T o1, T o2)booleancontains(java.lang.Object o)booleancontainsAll(java.util.Collection<?> c)static <T> java.util.SortedSet<T>empty()booleanequals(java.lang.Object other)Deprecated.private intfind(java.lang.Object toElement)Find the first element that is equal or bigger than the given elementTfirst()static <T extends java.lang.Comparable<? super T>>
SortedList<T>fromIterator(java.util.Iterator<? extends T> it)static <T> SortedList<T>fromIterator(java.util.Iterator<? extends T> it, java.util.Comparator<? super T> cmp)Tget(int index)java.lang.Class<?>getType()booleanhasDuplicates()inthashCode()Deprecated.SortedList<T>headSet(T toElement)intindexOf(java.lang.Object o)booleanisEmpty()booleanisEqual(SortedList<T> list)booleanisSubSet()java.util.Iterator<T>iterator()Tlast()intlastIndexOf(java.lang.Object o)java.util.ListIterator<T>listIterator()java.util.ListIterator<T>listIterator(int index)Tremove(int index)Deprecated.booleanremove(java.lang.Object o)booleanremoveAll(java.util.Collection<?> c)booleanretainAll(java.util.Collection<?> c)Tset(int index, T element)Deprecated.voidsetType(java.lang.Class<?> type)intsize()java.util.Spliterator<T>spliterator()SortedList<T>subList(int fromIndex, int toIndex)SortedList<T>subSet(T fromElement, T toElement)java.util.SortedSet<T>tailSet(T fromElement)java.lang.Object[]toArray()<X> X[]toArray(X[] a)java.lang.StringtoString()
-
-
-
Field Detail
-
EMPTY
private static final SortedList<?> EMPTY
-
list
private final T[] list
-
start
private final int start
-
end
private final int end
-
comparator
private final java.util.Comparator<? super T> comparator
-
type
private java.lang.Class<?> type
-
-
Constructor Detail
-
SortedList
public SortedList(java.util.Collection<? extends java.lang.Comparable<? super T>> x)
-
SortedList
public SortedList(java.util.Collection<? extends T> x, java.util.Comparator<? super T> cmp)
-
SortedList
@SafeVarargs public SortedList(C... x)
-
SortedList
private SortedList(SortedList<T> other, int start, int end)
-
SortedList
public SortedList(java.util.Collection<? extends T> x, int start, int end, java.util.Comparator<? super T> cmp)
-
SortedList
private SortedList()
-
-
Method Detail
-
size
public int size()
-
isEmpty
public boolean isEmpty()
-
contains
public boolean contains(java.lang.Object o)
-
iterator
public java.util.Iterator<T> iterator()
-
toArray
public java.lang.Object[] toArray()
-
toArray
public <X> X[] toArray(X[] a)
-
add
public boolean add(T e)
-
remove
public boolean remove(java.lang.Object o)
-
containsAll
public boolean containsAll(java.util.Collection<?> c)
-
addAll
public boolean addAll(java.util.Collection<? extends T> c)
-
retainAll
public boolean retainAll(java.util.Collection<?> c)
-
removeAll
public boolean removeAll(java.util.Collection<?> c)
-
clear
public void clear()
-
comparator
public java.util.Comparator<? super T> comparator()
- Specified by:
comparatorin interfacejava.util.SortedSet<T>
-
isSubSet
public boolean isSubSet()
-
subSet
public SortedList<T> subSet(T fromElement, T toElement)
- Specified by:
subSetin interfacejava.util.SortedSet<T>
-
indexOf
public int indexOf(java.lang.Object o)
- Specified by:
indexOfin interfacejava.util.List<T>
-
lastIndexOf
public int lastIndexOf(java.lang.Object o)
- Specified by:
lastIndexOfin interfacejava.util.List<T>
-
find
private int find(java.lang.Object toElement)
Find the first element that is equal or bigger than the given element- Parameters:
toElement-- Returns:
- absolute index (not relative!), returns end if not found
-
tailSet
public java.util.SortedSet<T> tailSet(T fromElement)
- Specified by:
tailSetin interfacejava.util.SortedSet<T>
-
headSet
public SortedList<T> headSet(T toElement)
- Specified by:
headSetin interfacejava.util.SortedSet<T>
-
addAll
@Deprecated public boolean addAll(int index, java.util.Collection<? extends T> c)Deprecated.- Specified by:
addAllin interfacejava.util.List<T>
-
set
@Deprecated public T set(int index, T element)
Deprecated.- Specified by:
setin interfacejava.util.List<T>
-
add
@Deprecated public void add(int index, T element)Deprecated.- Specified by:
addin interfacejava.util.List<T>
-
remove
@Deprecated public T remove(int index)
Deprecated.- Specified by:
removein interfacejava.util.List<T>
-
listIterator
public java.util.ListIterator<T> listIterator()
- Specified by:
listIteratorin interfacejava.util.List<T>
-
listIterator
public java.util.ListIterator<T> listIterator(int index)
- Specified by:
listIteratorin interfacejava.util.List<T>
-
subList
public SortedList<T> subList(int fromIndex, int toIndex)
- Specified by:
subListin interfacejava.util.List<T>
-
equals
@Deprecated public boolean equals(java.lang.Object other)
Deprecated.
-
hashCode
@Deprecated public int hashCode()
Deprecated.
-
isEqual
public boolean isEqual(SortedList<T> list)
-
getType
public java.lang.Class<?> getType()
-
setType
public void setType(java.lang.Class<?> type)
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
hasDuplicates
public boolean hasDuplicates()
-
fromIterator
public static <T extends java.lang.Comparable<? super T>> SortedList<T> fromIterator(java.util.Iterator<? extends T> it)
-
fromIterator
public static <T> SortedList<T> fromIterator(java.util.Iterator<? extends T> it, java.util.Comparator<? super T> cmp)
-
empty
public static <T> java.util.SortedSet<T> empty()
-
spliterator
public java.util.Spliterator<T> spliterator()
-
-