public class Collections extends java.lang.Object implements Rollbackable
All methods which take a collection throw a {
Collection
,
Set
,
List
,
Map
,
Arrays
Modifier and Type | Field and Description |
---|---|
protected Checkpoint |
$CHECKPOINT |
protected CheckpointRecord |
$RECORD$$CHECKPOINT |
static List |
EMPTY_LIST
An immutable, serializable, empty List, which implements RandomAccess.
|
static Map |
EMPTY_MAP
An immutable, serializable, empty Map.
|
static Set |
EMPTY_SET
An immutable, serializable, empty Set.
|
Modifier and Type | Method and Description |
---|---|
void |
$COMMIT(long timestamp)
Commit changes up to the given timestamp, but not including changes
made at timestamp and afterward.
|
Checkpoint |
$GET$CHECKPOINT()
Get the checkpoint object that monitors this rollbackable object.
|
void |
$RESTORE(long timestamp,
boolean trim)
Restore a previous state to all the private fields of this rollbackable
object.
|
java.lang.Object |
$SET$CHECKPOINT(Checkpoint checkpoint)
Set the checkpoint object of this rollbackable object.
|
static int |
binarySearch(List l,
java.lang.Object key)
Perform a binary search of a List for a key, using the natural ordering of
the elements.
|
static int |
binarySearch(List l,
java.lang.Object key,
java.util.Comparator c)
Perform a binary search of a List for a key, using a supplied Comparator.
|
static void |
copy(List dest,
List source)
Copy one list to another.
|
static List |
emptyList()
Returns the empty list (immutable).
|
static Map |
emptyMap()
Returns the empty list (immutable).
|
static java.util.Enumeration |
enumeration(Collection c)
Returns an Enumeration over a collection.
|
static void |
fill(List l,
java.lang.Object val)
Replace every element of a list with a given value.
|
static int |
indexOfSubList(List source,
List target)
Returns the starting index where the specified sublist first occurs
in a larger list, or -1 if there is no matching position.
|
static int |
lastIndexOfSubList(List source,
List target)
Returns the starting index where the specified sublist last occurs
in a larger list, or -1 if there is no matching position.
|
static ArrayList |
list(java.util.Enumeration e)
Returns an ArrayList holding the elements visited by a given
Enumeration.
|
static java.lang.Object |
max(Collection c)
Find the maximum element in a Collection, according to the natural
ordering of the elements.
|
static java.lang.Object |
max(Collection c,
java.util.Comparator order)
Find the maximum element in a Collection, according to a specified
Comparator.
|
static java.lang.Object |
min(Collection c)
Find the minimum element in a Collection, according to the natural
ordering of the elements.
|
static java.lang.Object |
min(Collection c,
java.util.Comparator order)
Find the minimum element in a Collection, according to a specified
Comparator.
|
static List |
nCopies(int n,
java.lang.Object o)
Creates an immutable list consisting of the same object repeated n times.
|
static boolean |
replaceAll(List list,
java.lang.Object oldval,
java.lang.Object newval)
Replace all instances of one object with another in the specified list.
|
static void |
reverse(List l)
Reverse a given list.
|
static java.util.Comparator |
reverseOrder()
Get a comparator that implements the reverse of natural ordering.
|
static void |
rotate(List list,
int distance)
Rotate the elements in a list by a specified distance.
|
static void |
shuffle(List l)
Shuffle a list according to a default source of randomness.
|
static void |
shuffle(List l,
Random r)
Shuffle a list according to a given source of randomness.
|
static Set |
singleton(java.lang.Object o)
Obtain an immutable Set consisting of a single element.
|
static List |
singletonList(java.lang.Object o)
Obtain an immutable List consisting of a single element.
|
static Map |
singletonMap(java.lang.Object key,
java.lang.Object value)
Obtain an immutable Map consisting of a single key-value pair.
|
static void |
sort(List l)
Sort a list according to the natural ordering of its elements.
|
static void |
sort(List l,
java.util.Comparator c)
Sort a list according to a specified Comparator.
|
static void |
swap(List l,
int i,
int j)
Swaps the elements at the specified positions within the list.
|
static Collection |
synchronizedCollection(Collection c)
Returns a synchronized (thread-safe) collection wrapper backed by the
given collection.
|
static List |
synchronizedList(List l)
Returns a synchronized (thread-safe) list wrapper backed by the
given list.
|
static Map |
synchronizedMap(Map m)
Returns a synchronized (thread-safe) map wrapper backed by the given
map.
|
static Set |
synchronizedSet(Set s)
Returns a synchronized (thread-safe) set wrapper backed by the given
set.
|
static SortedMap |
synchronizedSortedMap(SortedMap m)
Returns a synchronized (thread-safe) sorted map wrapper backed by the
given map.
|
static SortedSet |
synchronizedSortedSet(SortedSet s)
Returns a synchronized (thread-safe) sorted set wrapper backed by the
given set.
|
static Collection |
unmodifiableCollection(Collection c)
Returns an unmodifiable view of the given collection.
|
static List |
unmodifiableList(List l)
Returns an unmodifiable view of the given list.
|
static Map |
unmodifiableMap(Map m)
Returns an unmodifiable view of the given map.
|
static Set |
unmodifiableSet(Set s)
Returns an unmodifiable view of the given set.
|
static SortedMap |
unmodifiableSortedMap(SortedMap m)
Returns an unmodifiable view of the given sorted map.
|
static SortedSet |
unmodifiableSortedSet(SortedSet s)
Returns an unmodifiable view of the given sorted set.
|
protected transient Checkpoint $CHECKPOINT
public static final Set EMPTY_SET
Serializable
public static final List EMPTY_LIST
Serializable
,
RandomAccess
public static final Map EMPTY_MAP
Serializable
protected transient CheckpointRecord $RECORD$$CHECKPOINT
public static final List emptyList()
public static final Map emptyMap()
public static int binarySearch(List l, java.lang.Object key)
This algorithm behaves in log(n) time for {
l
- the list to search (must be sorted)key
- the value to search forjava.lang.ClassCastException
- if key could not be compared with one of the
elements of ljava.lang.NullPointerException
- if a null element has compareTo calledsort(List)
public static int binarySearch(List l, java.lang.Object key, java.util.Comparator c)
This algorithm behaves in log(n) time for {
l
- the list to search (must be sorted)key
- the value to search forc
- the comparator by which the list is sortedjava.lang.ClassCastException
- if key could not be compared with one of the
elements of ljava.lang.NullPointerException
- if a null element is compared with natural
ordering (only possible when c is null)sort(List, Comparator)
public static void copy(List dest, List source)
dest
- the destination listsource
- the source listjava.lang.IndexOutOfBoundsException
- if the destination list is shorter
than the source list (the destination will be unmodified)java.lang.UnsupportedOperationException
- if dest.listIterator() does not
support the set operationpublic static java.util.Enumeration enumeration(Collection c)
c
- the Collection to iterate overpublic static void fill(List l, java.lang.Object val)
l
- the list to fill.val
- the object to vill the list with.java.lang.UnsupportedOperationException
- if l.listIterator() does not
support the set operation.public static int indexOfSubList(List source, List target)
target.size() > source.size()
, this returns -1,
otherwise this implementation uses brute force, checking for
source.sublist(i, i + target.size()).equals(target)
for all possible i.source
- the list to searchtarget
- the sublist to search forpublic static int lastIndexOfSubList(List source, List target)
target.size() > source.size()
, this returns -1,
otherwise this implementation uses brute force, checking for
source.sublist(i, i + target.size()).equals(target)
for all possible i.source
- the list to searchtarget
- the sublist to search forpublic static ArrayList list(java.util.Enumeration e)
e
- the enumeration to put in a listArrayList
public static java.lang.Object max(Collection c)
c
- the Collection to find the maximum element ofjava.util.NoSuchElementException
- if c is emptyjava.lang.ClassCastException
- if elements in c are not mutually comparablejava.lang.NullPointerException
- if null.compareTo is calledpublic static java.lang.Object max(Collection c, java.util.Comparator order)
c
- the Collection to find the maximum element oforder
- the Comparator to order the elements by, or null for natural
orderingjava.util.NoSuchElementException
- if c is emptyjava.lang.ClassCastException
- if elements in c are not mutually comparablejava.lang.NullPointerException
- if null is compared by natural ordering
(only possible when order is null)public static java.lang.Object min(Collection c)
c
- the Collection to find the minimum element ofjava.util.NoSuchElementException
- if c is emptyjava.lang.ClassCastException
- if elements in c are not mutually comparablejava.lang.NullPointerException
- if null.compareTo is calledpublic static java.lang.Object min(Collection c, java.util.Comparator order)
c
- the Collection to find the minimum element oforder
- the Comparator to order the elements by, or null for natural
orderingjava.util.NoSuchElementException
- if c is emptyjava.lang.ClassCastException
- if elements in c are not mutually comparablejava.lang.NullPointerException
- if null is compared by natural ordering
(only possible when order is null)public static List nCopies(int n, java.lang.Object o)
n
- the number of times to repeat the objecto
- the object to repeatjava.lang.IllegalArgumentException
- if n < 0List.addAll(Collection)
,
Serializable
,
RandomAccess
public static boolean replaceAll(List list, java.lang.Object oldval, java.lang.Object newval)
oldval == null ? e == null : oldval.equals(e)
.list
- the list to iterate overoldval
- the element to replacenewval
- the new value for the elementtrue
if a replacement occurred.java.lang.UnsupportedOperationException
- if the list iterator does not allow
for the set operationjava.lang.ClassCastException
- if newval is of a type which cannot be added
to the listjava.lang.IllegalArgumentException
- if some other aspect of newval stops
it being added to the listpublic static void reverse(List l)
l
- the list to reversejava.lang.UnsupportedOperationException
- if l.listIterator() does not
support the set operationpublic static java.util.Comparator reverseOrder()
Comparable
,
Serializable
public static void rotate(List list, int distance)
i
was formerly at index
(i - distance) mod list.size()
. The list size is unchanged.
For example, suppose a list contains [t, a, n, k, s]
. After
either Collections.rotate(l, 4)
or
Collections.rotate(l, -1)
, the new contents are
[s, t, a, n, k]
. This can be applied to sublists to rotate
just a portion of the list. For example, to move element a
forward two positions in the original example, use
Collections.rotate(l.subList(1, 3+1), -1)
, which will
result in [t, n, k, a, s]
.
If the list is small or implements {
list
- the list to rotatedistance
- the distance to rotate by; unrestricted in valuejava.lang.UnsupportedOperationException
- if the list does not support setpublic static void shuffle(List l)
This algorithm would result in a perfectly fair shuffle (that is, each element would have an equal chance of ending up in any position) if r were a perfect source of randomness. In practice the results are merely very close to perfect.
This method operates in linear time. To do this on large lists which do not implement {
l
- the list to shufflejava.lang.UnsupportedOperationException
- if l.listIterator() does not
support the set operationpublic static void shuffle(List l, Random r)
This algorithm would result in a perfectly fair shuffle (that is, each element would have an equal chance of ending up in any position) if r were a perfect source of randomness. In practise (eg if r = new Random()) the results are merely very close to perfect.
This method operates in linear time. To do this on large lists which do not implement {
l
- the list to shuffler
- the source of randomness to use for the shufflejava.lang.UnsupportedOperationException
- if l.listIterator() does not
support the set operationpublic static Set singleton(java.lang.Object o)
o
- the single elementSerializable
public static List singletonList(java.lang.Object o)
o
- the single elementSerializable
,
RandomAccess
public static Map singletonMap(java.lang.Object key, java.lang.Object value)
key
- the single keyvalue
- the single valueSerializable
public static void sort(List l)
l
- the List to sortjava.lang.ClassCastException
- if some items are not mutually comparablejava.lang.UnsupportedOperationException
- if the List is not modifiablejava.lang.NullPointerException
- if some element is nullArrays.sort(Object[])
public static void sort(List l, java.util.Comparator c)
l
- the List to sortc
- the Comparator specifying the ordering for the elements, or
null for natural orderingjava.lang.ClassCastException
- if c will not compare some pair of itemsjava.lang.UnsupportedOperationException
- if the List is not modifiablejava.lang.NullPointerException
- if null is compared by natural ordering
(only possible when c is null)Arrays.sort(Object[], Comparator)
public static void swap(List l, int i, int j)
l
- the list to work oni
- the first index to swapj
- the second indexjava.lang.UnsupportedOperationException
- if list.set is not supportedjava.lang.IndexOutOfBoundsException
- if either i or j is < 0 or >=
list.size()public static Collection synchronizedCollection(Collection c)
Collection c = Collections.synchronizedCollection(new Collection(...)); ... synchronized (c) { Iterator i = c.iterator(); while (i.hasNext()) foo(i.next()); }
Since the collection might be a List or a Set, and those have incompatible equals and hashCode requirements, this relies on Object's implementation rather than passing those calls on to the wrapped collection. The returned Collection implements Serializable, but can only be serialized if the collection it wraps is likewise Serializable.
c
- the collection to wrapSerializable
public static List synchronizedList(List l)
List l = Collections.synchronizedList(new List(...)); ... synchronized (l) { Iterator i = l.iterator(); while (i.hasNext()) foo(i.next()); }
The returned List implements Serializable, but can only be serialized if the list it wraps is likewise Serializable. In addition, if the wrapped list implements RandomAccess, this does too.
l
- the list to wrapSerializable
,
RandomAccess
public static Map synchronizedMap(Map m)
Map m = Collections.synchronizedMap(new Map(...)); ... Set s = m.keySet(); // safe outside a synchronized block synchronized (m) // synch on m, not s { Iterator i = s.iterator(); while (i.hasNext()) foo(i.next()); }
The returned Map implements Serializable, but can only be serialized if the map it wraps is likewise Serializable.
m
- the map to wrapSerializable
public static Set synchronizedSet(Set s)
Set s = Collections.synchronizedSet(new Set(...)); ... synchronized (s) { Iterator i = s.iterator(); while (i.hasNext()) foo(i.next()); }
The returned Set implements Serializable, but can only be serialized if the set it wraps is likewise Serializable.
s
- the set to wrapSerializable
public static SortedMap synchronizedSortedMap(SortedMap m)
SortedMap m = Collections.synchronizedSortedMap(new SortedMap(...)); ... Set s = m.keySet(); // safe outside a synchronized block SortedMap m2 = m.headMap(foo); // safe outside a synchronized block Set s2 = m2.keySet(); // safe outside a synchronized block synchronized (m) // synch on m, not m2, s or s2 { Iterator i = s.iterator(); while (i.hasNext()) foo(i.next()); i = s2.iterator(); while (i.hasNext()) bar(i.next()); }
The returned SortedMap implements Serializable, but can only be serialized if the map it wraps is likewise Serializable.
m
- the sorted map to wrapSerializable
public static SortedSet synchronizedSortedSet(SortedSet s)
SortedSet s = Collections.synchronizedSortedSet(new SortedSet(...)); ... SortedSet s2 = s.headSet(foo); // safe outside a synchronized block synchronized (s) // synch on s, not s2 { Iterator i = s2.iterator(); while (i.hasNext()) foo(i.next()); }
The returned SortedSet implements Serializable, but can only be serialized if the set it wraps is likewise Serializable.
s
- the sorted set to wrapSerializable
public static Collection unmodifiableCollection(Collection c)
c
- the collection to wrapSerializable
public static List unmodifiableList(List l)
l
- the list to wrapSerializable
,
RandomAccess
public static Map unmodifiableMap(Map m)
m
- the map to wrapSerializable
public static Set unmodifiableSet(Set s)
s
- the set to wrapSerializable
public static SortedMap unmodifiableSortedMap(SortedMap m)
m
- the map to wrapSerializable
public static SortedSet unmodifiableSortedSet(SortedSet s)
s
- the set to wrapSerializable
public void $COMMIT(long timestamp)
Rollbackable
$COMMIT
in interface Rollbackable
timestamp
- The timestamp.public void $RESTORE(long timestamp, boolean trim)
Rollbackable
$RESTORE
in interface Rollbackable
timestamp
- The timestamp taken at the time when the previous
state was recorded.trim
- Whether to delete the records used for the rollback.Checkpoint.rollback(long, boolean)
public final Checkpoint $GET$CHECKPOINT()
Rollbackable
$GET$CHECKPOINT
in interface Rollbackable
public final java.lang.Object $SET$CHECKPOINT(Checkpoint checkpoint)
Rollbackable
$SET$CHECKPOINT
in interface Rollbackable
checkpoint
- The new checkpoint object.Checkpoint.setCheckpoint(Checkpoint)