delete jdk source code
This commit is contained in:
parent
03803a65f5
commit
4c3f9a57a5
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
|
@ -1,931 +0,0 @@
|
|||
package java.util;
|
||||
import java.io.*;
|
||||
|
||||
public class HashMap<K,V>
|
||||
extends AbstractMap<K,V>
|
||||
implements Map<K,V>, Cloneable, Serializable
|
||||
{
|
||||
|
||||
/**
|
||||
* The default initial capacity - MUST be a power of two.
|
||||
*/
|
||||
static final int DEFAULT_INITIAL_CAPACITY = 16;
|
||||
|
||||
/**
|
||||
* The maximum capacity, used if a higher value is implicitly specified
|
||||
* by either of the constructors with arguments.
|
||||
* MUST be a power of two <= 1<<30.
|
||||
*/
|
||||
static final int MAXIMUM_CAPACITY = 1 << 30;
|
||||
|
||||
/**
|
||||
* The load factor used when none specified in constructor.
|
||||
*/
|
||||
static final float DEFAULT_LOAD_FACTOR = 0.75f;
|
||||
|
||||
/**
|
||||
* The table, resized as necessary. Length MUST Always be a power of two.
|
||||
*/
|
||||
transient Entry[] table;
|
||||
|
||||
/**
|
||||
* The number of key-value mappings contained in this map.
|
||||
*/
|
||||
transient int size;
|
||||
|
||||
/**
|
||||
* The next size value at which to resize (capacity * load factor).
|
||||
* @serial
|
||||
*/
|
||||
int threshold;
|
||||
|
||||
/**
|
||||
* The load factor for the hash table.
|
||||
*
|
||||
* @serial
|
||||
*/
|
||||
final float loadFactor;
|
||||
|
||||
/**
|
||||
* The number of times this HashMap has been structurally modified
|
||||
* Structural modifications are those that change the number of mappings in
|
||||
* the HashMap or otherwise modify its internal structure (e.g.,
|
||||
* rehash). This field is used to make iterators on Collection-views of
|
||||
* the HashMap fail-fast. (See ConcurrentModificationException).
|
||||
*/
|
||||
transient int modCount;
|
||||
|
||||
/**
|
||||
* Constructs an empty <tt>HashMap</tt> with the specified initial
|
||||
* capacity and load factor.
|
||||
*
|
||||
* @param initialCapacity the initial capacity
|
||||
* @param loadFactor the load factor
|
||||
* @throws IllegalArgumentException if the initial capacity is negative
|
||||
* or the load factor is nonpositive
|
||||
*/
|
||||
public HashMap(int initialCapacity, float loadFactor) {
|
||||
if (initialCapacity < 0)
|
||||
throw new IllegalArgumentException("Illegal initial capacity: " +
|
||||
initialCapacity);
|
||||
if (initialCapacity > MAXIMUM_CAPACITY)
|
||||
initialCapacity = MAXIMUM_CAPACITY;
|
||||
if (loadFactor <= 0 || Float.isNaN(loadFactor))
|
||||
throw new IllegalArgumentException("Illegal load factor: " +
|
||||
loadFactor);
|
||||
|
||||
// Find a power of 2 >= initialCapacity
|
||||
int capacity = 1;
|
||||
while (capacity < initialCapacity)
|
||||
capacity <<= 1;
|
||||
|
||||
this.loadFactor = loadFactor;
|
||||
threshold = (int)(capacity * loadFactor);
|
||||
table = new Entry[capacity];
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an empty <tt>HashMap</tt> with the specified initial
|
||||
* capacity and the default load factor (0.75).
|
||||
*
|
||||
* @param initialCapacity the initial capacity.
|
||||
* @throws IllegalArgumentException if the initial capacity is negative.
|
||||
*/
|
||||
public HashMap(int initialCapacity) {
|
||||
this(initialCapacity, DEFAULT_LOAD_FACTOR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an empty <tt>HashMap</tt> with the default initial capacity
|
||||
* (16) and the default load factor (0.75).
|
||||
*/
|
||||
public HashMap() {
|
||||
this.loadFactor = DEFAULT_LOAD_FACTOR;
|
||||
threshold = (int)(DEFAULT_INITIAL_CAPACITY * DEFAULT_LOAD_FACTOR);
|
||||
table = new Entry[DEFAULT_INITIAL_CAPACITY];
|
||||
init();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new <tt>HashMap</tt> with the same mappings as the
|
||||
* specified <tt>Map</tt>. The <tt>HashMap</tt> is created with
|
||||
* default load factor (0.75) and an initial capacity sufficient to
|
||||
* hold the mappings in the specified <tt>Map</tt>.
|
||||
*
|
||||
* @param m the map whose mappings are to be placed in this map
|
||||
* @throws NullPointerException if the specified map is null
|
||||
*/
|
||||
public HashMap(Map<? extends K, ? extends V> m) {
|
||||
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
|
||||
DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
|
||||
putAllForCreate(m);
|
||||
}
|
||||
|
||||
// internal utilities
|
||||
|
||||
/**
|
||||
* Initialization hook for subclasses. This method is called
|
||||
* in all constructors and pseudo-constructors (clone, readObject)
|
||||
* after HashMap has been initialized but before any entries have
|
||||
* been inserted. (In the absence of this method, readObject would
|
||||
* require explicit knowledge of subclasses.)
|
||||
*/
|
||||
void init() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies a supplemental hash function to a given hashCode, which
|
||||
* defends against poor quality hash functions. This is critical
|
||||
* because HashMap uses power-of-two length hash tables, that
|
||||
* otherwise encounter collisions for hashCodes that do not differ
|
||||
* in lower bits. Note: Null keys always map to hash 0, thus index 0.
|
||||
*/
|
||||
static int hash(int h) {
|
||||
// This function ensures that hashCodes that differ only by
|
||||
// constant multiples at each bit position have a bounded
|
||||
// number of collisions (approximately 8 at default load factor).
|
||||
h ^= (h >>> 20) ^ (h >>> 12);
|
||||
return h ^ (h >>> 7) ^ (h >>> 4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index for hash code h.
|
||||
*/
|
||||
static int indexFor(int h, int length) {
|
||||
return h & (length-1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of key-value mappings in this map.
|
||||
*
|
||||
* @return the number of key-value mappings in this map
|
||||
*/
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this map contains no key-value mappings.
|
||||
*
|
||||
* @return <tt>true</tt> if this map contains no key-value mappings
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return size == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped,
|
||||
* or {@code null} if this map contains no mapping for the key.
|
||||
*
|
||||
* <p>More formally, if this map contains a mapping from a key
|
||||
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
|
||||
* key.equals(k))}, then this method returns {@code v}; otherwise
|
||||
* it returns {@code null}. (There can be at most one such mapping.)
|
||||
*
|
||||
* <p>A return value of {@code null} does not <i>necessarily</i>
|
||||
* indicate that the map contains no mapping for the key; it's also
|
||||
* possible that the map explicitly maps the key to {@code null}.
|
||||
* The {@link #containsKey containsKey} operation may be used to
|
||||
* distinguish these two cases.
|
||||
*
|
||||
* @see #put(Object, Object)
|
||||
*/
|
||||
public V get(Object key) {
|
||||
if (key == null)
|
||||
return getForNullKey();
|
||||
int hash = hash(key.hashCode());
|
||||
for (Entry<K,V> e = table[indexFor(hash, table.length)];
|
||||
e != null;
|
||||
e = e.next) {
|
||||
Object k;
|
||||
if (e.hash == hash && ((k = e.key) == key || key.equals(k)))
|
||||
return e.value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Offloaded version of get() to look up null keys. Null keys map
|
||||
* to index 0. This null case is split out into separate methods
|
||||
* for the sake of performance in the two most commonly used
|
||||
* operations (get and put), but incorporated with conditionals in
|
||||
* others.
|
||||
*/
|
||||
private V getForNullKey() {
|
||||
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
|
||||
if (e.key == null)
|
||||
return e.value;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this map contains a mapping for the
|
||||
* specified key.
|
||||
*
|
||||
* @param key The key whose presence in this map is to be tested
|
||||
* @return <tt>true</tt> if this map contains a mapping for the specified
|
||||
* key.
|
||||
*/
|
||||
public boolean containsKey(Object key) {
|
||||
return getEntry(key) != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entry associated with the specified key in the
|
||||
* HashMap. Returns null if the HashMap contains no mapping
|
||||
* for the key.
|
||||
*/
|
||||
final Entry<K,V> getEntry(Object key) {
|
||||
int hash = (key == null) ? 0 : hash(key.hashCode());
|
||||
for (Entry<K,V> e = table[indexFor(hash, table.length)];
|
||||
e != null;
|
||||
e = e.next) {
|
||||
Object k;
|
||||
if (e.hash == hash &&
|
||||
((k = e.key) == key || (key != null && key.equals(k))))
|
||||
return e;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Associates the specified value with the specified key in this map.
|
||||
* If the map previously contained a mapping for the key, the old
|
||||
* value is replaced.
|
||||
*
|
||||
* @param key key with which the specified value is to be associated
|
||||
* @param value value to be associated with the specified key
|
||||
* @return the previous value associated with <tt>key</tt>, or
|
||||
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
|
||||
* (A <tt>null</tt> return can also indicate that the map
|
||||
* previously associated <tt>null</tt> with <tt>key</tt>.)
|
||||
*/
|
||||
public V put(K key, V value) {
|
||||
if (key == null)
|
||||
return putForNullKey(value);
|
||||
int hash = hash(key.hashCode());
|
||||
int i = indexFor(hash, table.length);
|
||||
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
|
||||
Object k;
|
||||
if (e.hash == hash && ((k = e.key) == key || key.equals(k))) {
|
||||
V oldValue = e.value;
|
||||
e.value = value;
|
||||
e.recordAccess(this);
|
||||
return oldValue;
|
||||
}
|
||||
}
|
||||
|
||||
modCount++;
|
||||
addEntry(hash, key, value, i);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Offloaded version of put for null keys
|
||||
*/
|
||||
private V putForNullKey(V value) {
|
||||
for (Entry<K,V> e = table[0]; e != null; e = e.next) {
|
||||
if (e.key == null) {
|
||||
V oldValue = e.value;
|
||||
e.value = value;
|
||||
e.recordAccess(this);
|
||||
return oldValue;
|
||||
}
|
||||
}
|
||||
modCount++;
|
||||
addEntry(0, null, value, 0);
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is used instead of put by constructors and
|
||||
* pseudoconstructors (clone, readObject). It does not resize the table,
|
||||
* check for comodification, etc. It calls createEntry rather than
|
||||
* addEntry.
|
||||
*/
|
||||
private void putForCreate(K key, V value) {
|
||||
int hash = (key == null) ? 0 : hash(key.hashCode());
|
||||
int i = indexFor(hash, table.length);
|
||||
|
||||
/**
|
||||
* Look for preexisting entry for key. This will never happen for
|
||||
* clone or deserialize. It will only happen for construction if the
|
||||
* input Map is a sorted map whose ordering is inconsistent w/ equals.
|
||||
*/
|
||||
for (Entry<K,V> e = table[i]; e != null; e = e.next) {
|
||||
Object k;
|
||||
if (e.hash == hash &&
|
||||
((k = e.key) == key || (key != null && key.equals(k)))) {
|
||||
e.value = value;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
createEntry(hash, key, value, i);
|
||||
}
|
||||
|
||||
private void putAllForCreate(Map<? extends K, ? extends V> m) {
|
||||
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
|
||||
putForCreate(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Rehashes the contents of this map into a new array with a
|
||||
* larger capacity. This method is called automatically when the
|
||||
* number of keys in this map reaches its threshold.
|
||||
*
|
||||
* If current capacity is MAXIMUM_CAPACITY, this method does not
|
||||
* resize the map, but sets threshold to Integer.MAX_VALUE.
|
||||
* This has the effect of preventing future calls.
|
||||
*
|
||||
* @param newCapacity the new capacity, MUST be a power of two;
|
||||
* must be greater than current capacity unless current
|
||||
* capacity is MAXIMUM_CAPACITY (in which case value
|
||||
* is irrelevant).
|
||||
*/
|
||||
void resize(int newCapacity) {
|
||||
Entry[] oldTable = table;
|
||||
int oldCapacity = oldTable.length;
|
||||
if (oldCapacity == MAXIMUM_CAPACITY) {
|
||||
threshold = Integer.MAX_VALUE;
|
||||
return;
|
||||
}
|
||||
|
||||
Entry[] newTable = new Entry[newCapacity];
|
||||
transfer(newTable);
|
||||
table = newTable;
|
||||
threshold = (int)(newCapacity * loadFactor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfers all entries from current table to newTable.
|
||||
*/
|
||||
void transfer(Entry[] newTable) {
|
||||
Entry[] src = table;
|
||||
int newCapacity = newTable.length;
|
||||
for (int j = 0; j < src.length; j++) {
|
||||
Entry<K,V> e = src[j];
|
||||
if (e != null) {
|
||||
src[j] = null;
|
||||
do {
|
||||
Entry<K,V> next = e.next;
|
||||
int i = indexFor(e.hash, newCapacity);
|
||||
e.next = newTable[i];
|
||||
newTable[i] = e;
|
||||
e = next;
|
||||
} while (e != null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Copies all of the mappings from the specified map to this map.
|
||||
* These mappings will replace any mappings that this map had for
|
||||
* any of the keys currently in the specified map.
|
||||
*
|
||||
* @param m mappings to be stored in this map
|
||||
* @throws NullPointerException if the specified map is null
|
||||
*/
|
||||
public void putAll(Map<? extends K, ? extends V> m) {
|
||||
int numKeysToBeAdded = m.size();
|
||||
if (numKeysToBeAdded == 0)
|
||||
return;
|
||||
|
||||
/*
|
||||
* Expand the map if the map if the number of mappings to be added
|
||||
* is greater than or equal to threshold. This is conservative; the
|
||||
* obvious condition is (m.size() + size) >= threshold, but this
|
||||
* condition could result in a map with twice the appropriate capacity,
|
||||
* if the keys to be added overlap with the keys already in this map.
|
||||
* By using the conservative calculation, we subject ourself
|
||||
* to at most one extra resize.
|
||||
*/
|
||||
if (numKeysToBeAdded > threshold) {
|
||||
int targetCapacity = (int)(numKeysToBeAdded / loadFactor + 1);
|
||||
if (targetCapacity > MAXIMUM_CAPACITY)
|
||||
targetCapacity = MAXIMUM_CAPACITY;
|
||||
int newCapacity = table.length;
|
||||
while (newCapacity < targetCapacity)
|
||||
newCapacity <<= 1;
|
||||
if (newCapacity > table.length)
|
||||
resize(newCapacity);
|
||||
}
|
||||
|
||||
for (Map.Entry<? extends K, ? extends V> e : m.entrySet())
|
||||
put(e.getKey(), e.getValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the mapping for the specified key from this map if present.
|
||||
*
|
||||
* @param key key whose mapping is to be removed from the map
|
||||
* @return the previous value associated with <tt>key</tt>, or
|
||||
* <tt>null</tt> if there was no mapping for <tt>key</tt>.
|
||||
* (A <tt>null</tt> return can also indicate that the map
|
||||
* previously associated <tt>null</tt> with <tt>key</tt>.)
|
||||
*/
|
||||
public V remove(Object key) {
|
||||
Entry<K,V> e = removeEntryForKey(key);
|
||||
return (e == null ? null : e.value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes and returns the entry associated with the specified key
|
||||
* in the HashMap. Returns null if the HashMap contains no mapping
|
||||
* for this key.
|
||||
*/
|
||||
final Entry<K,V> removeEntryForKey(Object key) {
|
||||
int hash = (key == null) ? 0 : hash(key.hashCode());
|
||||
int i = indexFor(hash, table.length);
|
||||
Entry<K,V> prev = table[i];
|
||||
Entry<K,V> e = prev;
|
||||
|
||||
while (e != null) {
|
||||
Entry<K,V> next = e.next;
|
||||
Object k;
|
||||
if (e.hash == hash &&
|
||||
((k = e.key) == key || (key != null && key.equals(k)))) {
|
||||
modCount++;
|
||||
size--;
|
||||
if (prev == e)
|
||||
table[i] = next;
|
||||
else
|
||||
prev.next = next;
|
||||
e.recordRemoval(this);
|
||||
return e;
|
||||
}
|
||||
prev = e;
|
||||
e = next;
|
||||
}
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
/**
|
||||
* Special version of remove for EntrySet.
|
||||
*/
|
||||
final Entry<K,V> removeMapping(Object o) {
|
||||
if (!(o instanceof Map.Entry))
|
||||
return null;
|
||||
|
||||
Map.Entry<K,V> entry = (Map.Entry<K,V>) o;
|
||||
Object key = entry.getKey();
|
||||
int hash = (key == null) ? 0 : hash(key.hashCode());
|
||||
int i = indexFor(hash, table.length);
|
||||
Entry<K,V> prev = table[i];
|
||||
Entry<K,V> e = prev;
|
||||
|
||||
while (e != null) {
|
||||
Entry<K,V> next = e.next;
|
||||
if (e.hash == hash && e.equals(entry)) {
|
||||
modCount++;
|
||||
size--;
|
||||
if (prev == e)
|
||||
table[i] = next;
|
||||
else
|
||||
prev.next = next;
|
||||
e.recordRemoval(this);
|
||||
return e;
|
||||
}
|
||||
prev = e;
|
||||
e = next;
|
||||
}
|
||||
|
||||
return e;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all of the mappings from this map.
|
||||
* The map will be empty after this call returns.
|
||||
*/
|
||||
public void clear() {
|
||||
modCount++;
|
||||
Entry[] tab = table;
|
||||
for (int i = 0; i < tab.length; i++)
|
||||
tab[i] = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this map maps one or more keys to the
|
||||
* specified value.
|
||||
*
|
||||
* @param value value whose presence in this map is to be tested
|
||||
* @return <tt>true</tt> if this map maps one or more keys to the
|
||||
* specified value
|
||||
*/
|
||||
public boolean containsValue(Object value) {
|
||||
if (value == null)
|
||||
return containsNullValue();
|
||||
|
||||
Entry[] tab = table;
|
||||
for (int i = 0; i < tab.length ; i++)
|
||||
for (Entry e = tab[i] ; e != null ; e = e.next)
|
||||
if (value.equals(e.value))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Special-case code for containsValue with null argument
|
||||
*/
|
||||
private boolean containsNullValue() {
|
||||
Entry[] tab = table;
|
||||
for (int i = 0; i < tab.length ; i++)
|
||||
for (Entry e = tab[i] ; e != null ; e = e.next)
|
||||
if (e.value == null)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a shallow copy of this <tt>HashMap</tt> instance: the keys and
|
||||
* values themselves are not cloned.
|
||||
*
|
||||
* @return a shallow copy of this map
|
||||
*/
|
||||
public Object clone() {
|
||||
HashMap<K,V> result = null;
|
||||
try {
|
||||
result = (HashMap<K,V>)super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
// assert false;
|
||||
}
|
||||
result.table = new Entry[table.length];
|
||||
result.entrySet = null;
|
||||
result.modCount = 0;
|
||||
result.size = 0;
|
||||
result.init();
|
||||
result.putAllForCreate(this);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
static class Entry<K,V> implements Map.Entry<K,V> {
|
||||
final K key;
|
||||
V value;
|
||||
Entry<K,V> next;
|
||||
final int hash;
|
||||
|
||||
/**
|
||||
* Creates new entry.
|
||||
*/
|
||||
Entry(int h, K k, V v, Entry<K,V> n) {
|
||||
value = v;
|
||||
next = n;
|
||||
key = k;
|
||||
hash = h;
|
||||
}
|
||||
|
||||
public final K getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public final V getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public final V setValue(V newValue) {
|
||||
V oldValue = value;
|
||||
value = newValue;
|
||||
return oldValue;
|
||||
}
|
||||
|
||||
public final boolean equals(Object o) {
|
||||
if (!(o instanceof Map.Entry))
|
||||
return false;
|
||||
Map.Entry e = (Map.Entry)o;
|
||||
Object k1 = getKey();
|
||||
Object k2 = e.getKey();
|
||||
if (k1 == k2 || (k1 != null && k1.equals(k2))) {
|
||||
Object v1 = getValue();
|
||||
Object v2 = e.getValue();
|
||||
if (v1 == v2 || (v1 != null && v1.equals(v2)))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public final int hashCode() {
|
||||
return (key==null ? 0 : key.hashCode()) ^
|
||||
(value==null ? 0 : value.hashCode());
|
||||
}
|
||||
|
||||
public final String toString() {
|
||||
return getKey() + "=" + getValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is invoked whenever the value in an entry is
|
||||
* overwritten by an invocation of put(k,v) for a key k that's already
|
||||
* in the HashMap.
|
||||
*/
|
||||
void recordAccess(HashMap<K,V> m) {
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is invoked whenever the entry is
|
||||
* removed from the table.
|
||||
*/
|
||||
void recordRemoval(HashMap<K,V> m) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new entry with the specified key, value and hash code to
|
||||
* the specified bucket. It is the responsibility of this
|
||||
* method to resize the table if appropriate.
|
||||
*
|
||||
* Subclass overrides this to alter the behavior of put method.
|
||||
*/
|
||||
void addEntry(int hash, K key, V value, int bucketIndex) {
|
||||
Entry<K,V> e = table[bucketIndex];
|
||||
table[bucketIndex] = new Entry<>(hash, key, value, e);
|
||||
if (size++ >= threshold)
|
||||
resize(2 * table.length);
|
||||
}
|
||||
|
||||
/**
|
||||
* Like addEntry except that this version is used when creating entries
|
||||
* as part of Map construction or "pseudo-construction" (cloning,
|
||||
* deserialization). This version needn't worry about resizing the table.
|
||||
*
|
||||
* Subclass overrides this to alter the behavior of HashMap(Map),
|
||||
* clone, and readObject.
|
||||
*/
|
||||
void createEntry(int hash, K key, V value, int bucketIndex) {
|
||||
Entry<K,V> e = table[bucketIndex];
|
||||
table[bucketIndex] = new Entry<>(hash, key, value, e);
|
||||
size++;
|
||||
}
|
||||
|
||||
private abstract class HashIterator<E> implements Iterator<E> {
|
||||
Entry<K,V> next; // next entry to return
|
||||
int expectedModCount; // For fast-fail
|
||||
int index; // current slot
|
||||
Entry<K,V> current; // current entry
|
||||
|
||||
HashIterator() {
|
||||
expectedModCount = modCount;
|
||||
if (size > 0) { // advance to first entry
|
||||
Entry[] t = table;
|
||||
while (index < t.length && (next = t[index++]) == null)
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
public final boolean hasNext() {
|
||||
return next != null;
|
||||
}
|
||||
|
||||
final Entry<K,V> nextEntry() {
|
||||
if (modCount != expectedModCount)
|
||||
throw new ConcurrentModificationException();
|
||||
Entry<K,V> e = next;
|
||||
if (e == null)
|
||||
throw new NoSuchElementException();
|
||||
|
||||
if ((next = e.next) == null) {
|
||||
Entry[] t = table;
|
||||
while (index < t.length && (next = t[index++]) == null)
|
||||
;
|
||||
}
|
||||
current = e;
|
||||
return e;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
if (current == null)
|
||||
throw new IllegalStateException();
|
||||
if (modCount != expectedModCount)
|
||||
throw new ConcurrentModificationException();
|
||||
Object k = current.key;
|
||||
current = null;
|
||||
HashMap.this.removeEntryForKey(k);
|
||||
expectedModCount = modCount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private final class ValueIterator extends HashIterator<V> {
|
||||
public V next() {
|
||||
return nextEntry().value;
|
||||
}
|
||||
}
|
||||
|
||||
private final class KeyIterator extends HashIterator<K> {
|
||||
public K next() {
|
||||
return nextEntry().getKey();
|
||||
}
|
||||
}
|
||||
|
||||
private final class EntryIterator extends HashIterator<Map.Entry<K,V>> {
|
||||
public Map.Entry<K,V> next() {
|
||||
return nextEntry();
|
||||
}
|
||||
}
|
||||
|
||||
// Subclass overrides these to alter behavior of views' iterator() method
|
||||
Iterator<K> newKeyIterator() {
|
||||
return new KeyIterator();
|
||||
}
|
||||
Iterator<V> newValueIterator() {
|
||||
return new ValueIterator();
|
||||
}
|
||||
Iterator<Map.Entry<K,V>> newEntryIterator() {
|
||||
return new EntryIterator();
|
||||
}
|
||||
|
||||
|
||||
// Views
|
||||
|
||||
private transient Set<Map.Entry<K,V>> entrySet = null;
|
||||
|
||||
/**
|
||||
* Returns a {@link Set} view of the keys contained in this map.
|
||||
* The set is backed by the map, so changes to the map are
|
||||
* reflected in the set, and vice-versa. If the map is modified
|
||||
* while an iteration over the set is in progress (except through
|
||||
* the iterator's own <tt>remove</tt> operation), the results of
|
||||
* the iteration are undefined. The set supports element removal,
|
||||
* which removes the corresponding mapping from the map, via the
|
||||
* <tt>Iterator.remove</tt>, <tt>Set.remove</tt>,
|
||||
* <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt>
|
||||
* operations. It does not support the <tt>add</tt> or <tt>addAll</tt>
|
||||
* operations.
|
||||
*/
|
||||
public Set<K> keySet() {
|
||||
Set<K> ks = keySet;
|
||||
return (ks != null ? ks : (keySet = new KeySet()));
|
||||
}
|
||||
|
||||
private final class KeySet extends AbstractSet<K> {
|
||||
public Iterator<K> iterator() {
|
||||
return newKeyIterator();
|
||||
}
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
public boolean contains(Object o) {
|
||||
return containsKey(o);
|
||||
}
|
||||
public boolean remove(Object o) {
|
||||
return HashMap.this.removeEntryForKey(o) != null;
|
||||
}
|
||||
public void clear() {
|
||||
HashMap.this.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Collection} view of the values contained in this map.
|
||||
* The collection is backed by the map, so changes to the map are
|
||||
* reflected in the collection, and vice-versa. If the map is
|
||||
* modified while an iteration over the collection is in progress
|
||||
* (except through the iterator's own <tt>remove</tt> operation),
|
||||
* the results of the iteration are undefined. The collection
|
||||
* supports element removal, which removes the corresponding
|
||||
* mapping from the map, via the <tt>Iterator.remove</tt>,
|
||||
* <tt>Collection.remove</tt>, <tt>removeAll</tt>,
|
||||
* <tt>retainAll</tt> and <tt>clear</tt> operations. It does not
|
||||
* support the <tt>add</tt> or <tt>addAll</tt> operations.
|
||||
*/
|
||||
public Collection<V> values() {
|
||||
Collection<V> vs = values;
|
||||
return (vs != null ? vs : (values = new Values()));
|
||||
}
|
||||
|
||||
private final class Values extends AbstractCollection<V> {
|
||||
public Iterator<V> iterator() {
|
||||
return newValueIterator();
|
||||
}
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
public boolean contains(Object o) {
|
||||
return containsValue(o);
|
||||
}
|
||||
public void clear() {
|
||||
HashMap.this.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a {@link Set} view of the mappings contained in this map.
|
||||
* The set is backed by the map, so changes to the map are
|
||||
* reflected in the set, and vice-versa. If the map is modified
|
||||
* while an iteration over the set is in progress (except through
|
||||
* the iterator's own <tt>remove</tt> operation, or through the
|
||||
* <tt>setValue</tt> operation on a map entry returned by the
|
||||
* iterator) the results of the iteration are undefined. The set
|
||||
* supports element removal, which removes the corresponding
|
||||
* mapping from the map, via the <tt>Iterator.remove</tt>,
|
||||
* <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt> and
|
||||
* <tt>clear</tt> operations. It does not support the
|
||||
* <tt>add</tt> or <tt>addAll</tt> operations.
|
||||
*
|
||||
* @return a set view of the mappings contained in this map
|
||||
*/
|
||||
public Set<Map.Entry<K,V>> entrySet() {
|
||||
return entrySet0();
|
||||
}
|
||||
|
||||
private Set<Map.Entry<K,V>> entrySet0() {
|
||||
Set<Map.Entry<K,V>> es = entrySet;
|
||||
return es != null ? es : (entrySet = new EntrySet());
|
||||
}
|
||||
|
||||
private final class EntrySet extends AbstractSet<Map.Entry<K,V>> {
|
||||
public Iterator<Map.Entry<K,V>> iterator() {
|
||||
return newEntryIterator();
|
||||
}
|
||||
public boolean contains(Object o) {
|
||||
if (!(o instanceof Map.Entry))
|
||||
return false;
|
||||
Map.Entry<K,V> e = (Map.Entry<K,V>) o;
|
||||
Entry<K,V> candidate = getEntry(e.getKey());
|
||||
return candidate != null && candidate.equals(e);
|
||||
}
|
||||
public boolean remove(Object o) {
|
||||
return removeMapping(o) != null;
|
||||
}
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
public void clear() {
|
||||
HashMap.this.clear();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the state of the <tt>HashMap</tt> instance to a stream (i.e.,
|
||||
* serialize it).
|
||||
*
|
||||
* @serialData The <i>capacity</i> of the HashMap (the length of the
|
||||
* bucket array) is emitted (int), followed by the
|
||||
* <i>size</i> (an int, the number of key-value
|
||||
* mappings), followed by the key (Object) and value (Object)
|
||||
* for each key-value mapping. The key-value mappings are
|
||||
* emitted in no particular order.
|
||||
*/
|
||||
private void writeObject(java.io.ObjectOutputStream s)
|
||||
throws IOException
|
||||
{
|
||||
Iterator<Map.Entry<K,V>> i =
|
||||
(size > 0) ? entrySet0().iterator() : null;
|
||||
|
||||
// Write out the threshold, loadfactor, and any hidden stuff
|
||||
s.defaultWriteObject();
|
||||
|
||||
// Write out number of buckets
|
||||
s.writeInt(table.length);
|
||||
|
||||
// Write out size (number of Mappings)
|
||||
s.writeInt(size);
|
||||
|
||||
// Write out keys and values (alternating)
|
||||
if (i != null) {
|
||||
while (i.hasNext()) {
|
||||
Map.Entry<K,V> e = i.next();
|
||||
s.writeObject(e.getKey());
|
||||
s.writeObject(e.getValue());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = 362498820763181265L;
|
||||
|
||||
/**
|
||||
* Reconstitute the <tt>HashMap</tt> instance from a stream (i.e.,
|
||||
* deserialize it).
|
||||
*/
|
||||
private void readObject(java.io.ObjectInputStream s)
|
||||
throws IOException, ClassNotFoundException
|
||||
{
|
||||
// Read in the threshold, loadfactor, and any hidden stuff
|
||||
s.defaultReadObject();
|
||||
|
||||
// Read in number of buckets and allocate the bucket array;
|
||||
int numBuckets = s.readInt();
|
||||
table = new Entry[numBuckets];
|
||||
|
||||
init(); // Give subclass a chance to do its thing.
|
||||
|
||||
// Read in size (number of Mappings)
|
||||
int size = s.readInt();
|
||||
|
||||
// Read the keys and values, and put the mappings in the HashMap
|
||||
for (int i=0; i<size; i++) {
|
||||
K key = (K) s.readObject();
|
||||
V value = (V) s.readObject();
|
||||
putForCreate(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
// These methods are used when serializing HashSets
|
||||
int capacity() { return table.length; }
|
||||
float loadFactor() { return loadFactor; }
|
||||
}
|
|
@ -1,228 +0,0 @@
|
|||
package java.util;
|
||||
|
||||
public class HashSet<E>
|
||||
extends AbstractSet<E>
|
||||
implements Set<E>, Cloneable, java.io.Serializable
|
||||
{
|
||||
static final long serialVersionUID = -5024744406713321676L;
|
||||
|
||||
private transient HashMap<E,Object> map;
|
||||
|
||||
// Dummy value to associate with an Object in the backing Map
|
||||
private static final Object PRESENT = new Object();
|
||||
|
||||
/**
|
||||
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
|
||||
* default initial capacity (16) and load factor (0.75).
|
||||
*/
|
||||
public HashSet() {
|
||||
map = new HashMap<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new set containing the elements in the specified
|
||||
* collection. The <tt>HashMap</tt> is created with default load factor
|
||||
* (0.75) and an initial capacity sufficient to contain the elements in
|
||||
* the specified collection.
|
||||
*
|
||||
* @param c the collection whose elements are to be placed into this set
|
||||
* @throws NullPointerException if the specified collection is null
|
||||
*/
|
||||
public HashSet(Collection<? extends E> c) {
|
||||
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
|
||||
addAll(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
|
||||
* the specified initial capacity and the specified load factor.
|
||||
*
|
||||
* @param initialCapacity the initial capacity of the hash map
|
||||
* @param loadFactor the load factor of the hash map
|
||||
* @throws IllegalArgumentException if the initial capacity is less
|
||||
* than zero, or if the load factor is nonpositive
|
||||
*/
|
||||
public HashSet(int initialCapacity, float loadFactor) {
|
||||
map = new HashMap<>(initialCapacity, loadFactor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
|
||||
* the specified initial capacity and default load factor (0.75).
|
||||
*
|
||||
* @param initialCapacity the initial capacity of the hash table
|
||||
* @throws IllegalArgumentException if the initial capacity is less
|
||||
* than zero
|
||||
*/
|
||||
public HashSet(int initialCapacity) {
|
||||
map = new HashMap<>(initialCapacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new, empty linked hash set. (This package private
|
||||
* constructor is only used by LinkedHashSet.) The backing
|
||||
* HashMap instance is a LinkedHashMap with the specified initial
|
||||
* capacity and the specified load factor.
|
||||
*
|
||||
* @param initialCapacity the initial capacity of the hash map
|
||||
* @param loadFactor the load factor of the hash map
|
||||
* @param dummy ignored (distinguishes this
|
||||
* constructor from other int, float constructor.)
|
||||
* @throws IllegalArgumentException if the initial capacity is less
|
||||
* than zero, or if the load factor is nonpositive
|
||||
*/
|
||||
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
|
||||
map = new LinkedHashMap<>(initialCapacity, loadFactor);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over the elements in this set. The elements
|
||||
* are returned in no particular order.
|
||||
*
|
||||
* @return an Iterator over the elements in this set
|
||||
* @see ConcurrentModificationException
|
||||
*/
|
||||
public Iterator<E> iterator() {
|
||||
return map.keySet().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements in this set (its cardinality).
|
||||
*
|
||||
* @return the number of elements in this set (its cardinality)
|
||||
*/
|
||||
public int size() {
|
||||
return map.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this set contains no elements.
|
||||
*
|
||||
* @return <tt>true</tt> if this set contains no elements
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return map.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this set contains the specified element.
|
||||
* More formally, returns <tt>true</tt> if and only if this set
|
||||
* contains an element <tt>e</tt> such that
|
||||
* <tt>(o==null ? e==null : o.equals(e))</tt>.
|
||||
*
|
||||
* @param o element whose presence in this set is to be tested
|
||||
* @return <tt>true</tt> if this set contains the specified element
|
||||
*/
|
||||
public boolean contains(Object o) {
|
||||
return map.containsKey(o);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified element to this set if it is not already present.
|
||||
* More formally, adds the specified element <tt>e</tt> to this set if
|
||||
* this set contains no element <tt>e2</tt> such that
|
||||
* <tt>(e==null ? e2==null : e.equals(e2))</tt>.
|
||||
* If this set already contains the element, the call leaves the set
|
||||
* unchanged and returns <tt>false</tt>.
|
||||
*
|
||||
* @param e element to be added to this set
|
||||
* @return <tt>true</tt> if this set did not already contain the specified
|
||||
* element
|
||||
*/
|
||||
public boolean add(E e) {
|
||||
return map.put(e, PRESENT)==null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the specified element from this set if it is present.
|
||||
* More formally, removes an element <tt>e</tt> such that
|
||||
* <tt>(o==null ? e==null : o.equals(e))</tt>,
|
||||
* if this set contains such an element. Returns <tt>true</tt> if
|
||||
* this set contained the element (or equivalently, if this set
|
||||
* changed as a result of the call). (This set will not contain the
|
||||
* element once the call returns.)
|
||||
*
|
||||
* @param o object to be removed from this set, if present
|
||||
* @return <tt>true</tt> if the set contained the specified element
|
||||
*/
|
||||
public boolean remove(Object o) {
|
||||
return map.remove(o)==PRESENT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all of the elements from this set.
|
||||
* The set will be empty after this call returns.
|
||||
*/
|
||||
public void clear() {
|
||||
map.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a shallow copy of this <tt>HashSet</tt> instance: the elements
|
||||
* themselves are not cloned.
|
||||
*
|
||||
* @return a shallow copy of this set
|
||||
*/
|
||||
public Object clone() {
|
||||
try {
|
||||
HashSet<E> newSet = (HashSet<E>) super.clone();
|
||||
newSet.map = (HashMap<E, Object>) map.clone();
|
||||
return newSet;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new InternalError();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the state of this <tt>HashSet</tt> instance to a stream (that is,
|
||||
* serialize it).
|
||||
*
|
||||
* @serialData The capacity of the backing <tt>HashMap</tt> instance
|
||||
* (int), and its load factor (float) are emitted, followed by
|
||||
* the size of the set (the number of elements it contains)
|
||||
* (int), followed by all of its elements (each an Object) in
|
||||
* no particular order.
|
||||
*/
|
||||
private void writeObject(java.io.ObjectOutputStream s)
|
||||
throws java.io.IOException {
|
||||
// Write out any hidden serialization magic
|
||||
s.defaultWriteObject();
|
||||
|
||||
// Write out HashMap capacity and load factor
|
||||
s.writeInt(map.capacity());
|
||||
s.writeFloat(map.loadFactor());
|
||||
|
||||
// Write out size
|
||||
s.writeInt(map.size());
|
||||
|
||||
// Write out all elements in the proper order.
|
||||
for (E e : map.keySet())
|
||||
s.writeObject(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconstitute the <tt>HashSet</tt> instance from a stream (that is,
|
||||
* deserialize it).
|
||||
*/
|
||||
private void readObject(java.io.ObjectInputStream s)
|
||||
throws java.io.IOException, ClassNotFoundException {
|
||||
// Read in any hidden serialization magic
|
||||
s.defaultReadObject();
|
||||
|
||||
// Read in HashMap capacity and load factor and create backing HashMap
|
||||
int capacity = s.readInt();
|
||||
float loadFactor = s.readFloat();
|
||||
map = (((HashSet)this) instanceof LinkedHashSet ?
|
||||
new LinkedHashMap<E,Object>(capacity, loadFactor) :
|
||||
new HashMap<E,Object>(capacity, loadFactor));
|
||||
|
||||
// Read in size
|
||||
int size = s.readInt();
|
||||
|
||||
// Read in all elements in the proper order.
|
||||
for (int i=0; i<size; i++) {
|
||||
E e = (E) s.readObject();
|
||||
map.put(e, PRESENT);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,349 +0,0 @@
|
|||
|
||||
package java.util;
|
||||
import java.io.*;
|
||||
|
||||
public class LinkedHashMap<K,V>
|
||||
extends HashMap<K,V>
|
||||
implements Map<K,V>
|
||||
{
|
||||
|
||||
private static final long serialVersionUID = 3801124242820219131L;
|
||||
|
||||
/**
|
||||
* The head of the doubly linked list.
|
||||
*/
|
||||
private transient Entry<K,V> header;
|
||||
|
||||
/**
|
||||
* The iteration ordering method for this linked hash map: <tt>true</tt>
|
||||
* for access-order, <tt>false</tt> for insertion-order.
|
||||
*
|
||||
* @serial
|
||||
*/
|
||||
private final boolean accessOrder;
|
||||
|
||||
/**
|
||||
* Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
|
||||
* with the specified initial capacity and load factor.
|
||||
*
|
||||
* @param initialCapacity the initial capacity
|
||||
* @param loadFactor the load factor
|
||||
* @throws IllegalArgumentException if the initial capacity is negative
|
||||
* or the load factor is nonpositive
|
||||
*/
|
||||
public LinkedHashMap(int initialCapacity, float loadFactor) {
|
||||
super(initialCapacity, loadFactor);
|
||||
accessOrder = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
|
||||
* with the specified initial capacity and a default load factor (0.75).
|
||||
*
|
||||
* @param initialCapacity the initial capacity
|
||||
* @throws IllegalArgumentException if the initial capacity is negative
|
||||
*/
|
||||
public LinkedHashMap(int initialCapacity) {
|
||||
super(initialCapacity);
|
||||
accessOrder = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an empty insertion-ordered <tt>LinkedHashMap</tt> instance
|
||||
* with the default initial capacity (16) and load factor (0.75).
|
||||
*/
|
||||
public LinkedHashMap() {
|
||||
super();
|
||||
accessOrder = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an insertion-ordered <tt>LinkedHashMap</tt> instance with
|
||||
* the same mappings as the specified map. The <tt>LinkedHashMap</tt>
|
||||
* instance is created with a default load factor (0.75) and an initial
|
||||
* capacity sufficient to hold the mappings in the specified map.
|
||||
*
|
||||
* @param m the map whose mappings are to be placed in this map
|
||||
* @throws NullPointerException if the specified map is null
|
||||
*/
|
||||
public LinkedHashMap(Map<? extends K, ? extends V> m) {
|
||||
super(m);
|
||||
accessOrder = false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs an empty <tt>LinkedHashMap</tt> instance with the
|
||||
* specified initial capacity, load factor and ordering mode.
|
||||
*
|
||||
* @param initialCapacity the initial capacity
|
||||
* @param loadFactor the load factor
|
||||
* @param accessOrder the ordering mode - <tt>true</tt> for
|
||||
* access-order, <tt>false</tt> for insertion-order
|
||||
* @throws IllegalArgumentException if the initial capacity is negative
|
||||
* or the load factor is nonpositive
|
||||
*/
|
||||
public LinkedHashMap(int initialCapacity,
|
||||
float loadFactor,
|
||||
boolean accessOrder) {
|
||||
super(initialCapacity, loadFactor);
|
||||
this.accessOrder = accessOrder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by superclass constructors and pseudoconstructors (clone,
|
||||
* readObject) before any entries are inserted into the map. Initializes
|
||||
* the chain.
|
||||
*/
|
||||
void init() {
|
||||
header = new Entry<>(-1, null, null, null);
|
||||
header.before = header.after = header;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transfers all entries to new table array. This method is called
|
||||
* by superclass resize. It is overridden for performance, as it is
|
||||
* faster to iterate using our linked list.
|
||||
*/
|
||||
void transfer(HashMap.Entry[] newTable) {
|
||||
int newCapacity = newTable.length;
|
||||
for (Entry<K,V> e = header.after; e != header; e = e.after) {
|
||||
int index = indexFor(e.hash, newCapacity);
|
||||
e.next = newTable[index];
|
||||
newTable[index] = e;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this map maps one or more keys to the
|
||||
* specified value.
|
||||
*
|
||||
* @param value value whose presence in this map is to be tested
|
||||
* @return <tt>true</tt> if this map maps one or more keys to the
|
||||
* specified value
|
||||
*/
|
||||
public boolean containsValue(Object value) {
|
||||
// Overridden to take advantage of faster iterator
|
||||
if (value==null) {
|
||||
for (Entry e = header.after; e != header; e = e.after)
|
||||
if (e.value==null)
|
||||
return true;
|
||||
} else {
|
||||
for (Entry e = header.after; e != header; e = e.after)
|
||||
if (value.equals(e.value))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the value to which the specified key is mapped,
|
||||
* or {@code null} if this map contains no mapping for the key.
|
||||
*
|
||||
* <p>More formally, if this map contains a mapping from a key
|
||||
* {@code k} to a value {@code v} such that {@code (key==null ? k==null :
|
||||
* key.equals(k))}, then this method returns {@code v}; otherwise
|
||||
* it returns {@code null}. (There can be at most one such mapping.)
|
||||
*
|
||||
* <p>A return value of {@code null} does not <i>necessarily</i>
|
||||
* indicate that the map contains no mapping for the key; it's also
|
||||
* possible that the map explicitly maps the key to {@code null}.
|
||||
* The {@link #containsKey containsKey} operation may be used to
|
||||
* distinguish these two cases.
|
||||
*/
|
||||
public V get(Object key) {
|
||||
Entry<K,V> e = (Entry<K,V>)getEntry(key);
|
||||
if (e == null)
|
||||
return null;
|
||||
e.recordAccess(this);
|
||||
return e.value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all of the mappings from this map.
|
||||
* The map will be empty after this call returns.
|
||||
*/
|
||||
public void clear() {
|
||||
super.clear();
|
||||
header.before = header.after = header;
|
||||
}
|
||||
|
||||
/**
|
||||
* LinkedHashMap entry.
|
||||
*/
|
||||
private static class Entry<K,V> extends HashMap.Entry<K,V> {
|
||||
// These fields comprise the doubly linked list used for iteration.
|
||||
Entry<K,V> before, after;
|
||||
|
||||
Entry(int hash, K key, V value, HashMap.Entry<K,V> next) {
|
||||
super(hash, key, value, next);
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes this entry from the linked list.
|
||||
*/
|
||||
private void remove() {
|
||||
before.after = after;
|
||||
after.before = before;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts this entry before the specified existing entry in the list.
|
||||
*/
|
||||
private void addBefore(Entry<K,V> existingEntry) {
|
||||
after = existingEntry;
|
||||
before = existingEntry.before;
|
||||
before.after = this;
|
||||
after.before = this;
|
||||
}
|
||||
|
||||
/**
|
||||
* This method is invoked by the superclass whenever the value
|
||||
* of a pre-existing entry is read by Map.get or modified by Map.set.
|
||||
* If the enclosing Map is access-ordered, it moves the entry
|
||||
* to the end of the list; otherwise, it does nothing.
|
||||
*/
|
||||
void recordAccess(HashMap<K,V> m) {
|
||||
LinkedHashMap<K,V> lm = (LinkedHashMap<K,V>)m;
|
||||
if (lm.accessOrder) {
|
||||
lm.modCount++;
|
||||
remove();
|
||||
addBefore(lm.header);
|
||||
}
|
||||
}
|
||||
|
||||
void recordRemoval(HashMap<K,V> m) {
|
||||
remove();
|
||||
}
|
||||
}
|
||||
|
||||
private abstract class LinkedHashIterator<T> implements Iterator<T> {
|
||||
Entry<K,V> nextEntry = header.after;
|
||||
Entry<K,V> lastReturned = null;
|
||||
|
||||
/**
|
||||
* The modCount value that the iterator believes that the backing
|
||||
* List should have. If this expectation is violated, the iterator
|
||||
* has detected concurrent modification.
|
||||
*/
|
||||
int expectedModCount = modCount;
|
||||
|
||||
public boolean hasNext() {
|
||||
return nextEntry != header;
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
if (lastReturned == null)
|
||||
throw new IllegalStateException();
|
||||
if (modCount != expectedModCount)
|
||||
throw new ConcurrentModificationException();
|
||||
|
||||
LinkedHashMap.this.remove(lastReturned.key);
|
||||
lastReturned = null;
|
||||
expectedModCount = modCount;
|
||||
}
|
||||
|
||||
Entry<K,V> nextEntry() {
|
||||
if (modCount != expectedModCount)
|
||||
throw new ConcurrentModificationException();
|
||||
if (nextEntry == header)
|
||||
throw new NoSuchElementException();
|
||||
|
||||
Entry<K,V> e = lastReturned = nextEntry;
|
||||
nextEntry = e.after;
|
||||
return e;
|
||||
}
|
||||
}
|
||||
|
||||
private class KeyIterator extends LinkedHashIterator<K> {
|
||||
public K next() { return nextEntry().getKey(); }
|
||||
}
|
||||
|
||||
private class ValueIterator extends LinkedHashIterator<V> {
|
||||
public V next() { return nextEntry().value; }
|
||||
}
|
||||
|
||||
private class EntryIterator extends LinkedHashIterator<Map.Entry<K,V>> {
|
||||
public Map.Entry<K,V> next() { return nextEntry(); }
|
||||
}
|
||||
|
||||
// These Overrides alter the behavior of superclass view iterator() methods
|
||||
Iterator<K> newKeyIterator() { return new KeyIterator(); }
|
||||
Iterator<V> newValueIterator() { return new ValueIterator(); }
|
||||
Iterator<Map.Entry<K,V>> newEntryIterator() { return new EntryIterator(); }
|
||||
|
||||
/**
|
||||
* This override alters behavior of superclass put method. It causes newly
|
||||
* allocated entry to get inserted at the end of the linked list and
|
||||
* removes the eldest entry if appropriate.
|
||||
*/
|
||||
void addEntry(int hash, K key, V value, int bucketIndex) {
|
||||
createEntry(hash, key, value, bucketIndex);
|
||||
|
||||
// Remove eldest entry if instructed, else grow capacity if appropriate
|
||||
Entry<K,V> eldest = header.after;
|
||||
if (removeEldestEntry(eldest)) {
|
||||
removeEntryForKey(eldest.key);
|
||||
} else {
|
||||
if (size >= threshold)
|
||||
resize(2 * table.length);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This override differs from addEntry in that it doesn't resize the
|
||||
* table or remove the eldest entry.
|
||||
*/
|
||||
void createEntry(int hash, K key, V value, int bucketIndex) {
|
||||
HashMap.Entry<K,V> old = table[bucketIndex];
|
||||
Entry<K,V> e = new Entry<>(hash, key, value, old);
|
||||
table[bucketIndex] = e;
|
||||
e.addBefore(header);
|
||||
size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns <tt>true</tt> if this map should remove its eldest entry.
|
||||
* This method is invoked by <tt>put</tt> and <tt>putAll</tt> after
|
||||
* inserting a new entry into the map. It provides the implementor
|
||||
* with the opportunity to remove the eldest entry each time a new one
|
||||
* is added. This is useful if the map represents a cache: it allows
|
||||
* the map to reduce memory consumption by deleting stale entries.
|
||||
*
|
||||
* <p>Sample use: this override will allow the map to grow up to 100
|
||||
* entries and then delete the eldest entry each time a new entry is
|
||||
* added, maintaining a steady state of 100 entries.
|
||||
* <pre>
|
||||
* private static final int MAX_ENTRIES = 100;
|
||||
*
|
||||
* protected boolean removeEldestEntry(Map.Entry eldest) {
|
||||
* return size() > MAX_ENTRIES;
|
||||
* }
|
||||
* </pre>
|
||||
*
|
||||
* <p>This method typically does not modify the map in any way,
|
||||
* instead allowing the map to modify itself as directed by its
|
||||
* return value. It <i>is</i> permitted for this method to modify
|
||||
* the map directly, but if it does so, it <i>must</i> return
|
||||
* <tt>false</tt> (indicating that the map should not attempt any
|
||||
* further modification). The effects of returning <tt>true</tt>
|
||||
* after modifying the map from within this method are unspecified.
|
||||
*
|
||||
* <p>This implementation merely returns <tt>false</tt> (so that this
|
||||
* map acts like a normal map - the eldest element is never removed).
|
||||
*
|
||||
* @param eldest The least recently inserted entry in the map, or if
|
||||
* this is an access-ordered map, the least recently accessed
|
||||
* entry. This is the entry that will be removed it this
|
||||
* method returns <tt>true</tt>. If the map was empty prior
|
||||
* to the <tt>put</tt> or <tt>putAll</tt> invocation resulting
|
||||
* in this invocation, this will be the entry that was just
|
||||
* inserted; in other words, if the map contains a single
|
||||
* entry, the eldest entry is also the newest.
|
||||
* @return <tt>true</tt> if the eldest entry should be removed
|
||||
* from the map; <tt>false</tt> if it should be retained.
|
||||
*/
|
||||
protected boolean removeEldestEntry(Map.Entry<K,V> eldest) {
|
||||
return false;
|
||||
}
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
|
||||
package java.util;
|
||||
|
||||
public class LinkedHashSet<E>
|
||||
extends HashSet<E>
|
||||
implements Set<E>, Cloneable, java.io.Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2851667679971038690L;
|
||||
|
||||
/**
|
||||
* Constructs a new, empty linked hash set with the specified initial
|
||||
* capacity and load factor.
|
||||
*
|
||||
* @param initialCapacity the initial capacity of the linked hash set
|
||||
* @param loadFactor the load factor of the linked hash set
|
||||
* @throws IllegalArgumentException if the initial capacity is less
|
||||
* than zero, or if the load factor is nonpositive
|
||||
*/
|
||||
public LinkedHashSet(int initialCapacity, float loadFactor) {
|
||||
super(initialCapacity, loadFactor, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new, empty linked hash set with the specified initial
|
||||
* capacity and the default load factor (0.75).
|
||||
*
|
||||
* @param initialCapacity the initial capacity of the LinkedHashSet
|
||||
* @throws IllegalArgumentException if the initial capacity is less
|
||||
* than zero
|
||||
*/
|
||||
public LinkedHashSet(int initialCapacity) {
|
||||
super(initialCapacity, .75f, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new, empty linked hash set with the default initial
|
||||
* capacity (16) and load factor (0.75).
|
||||
*/
|
||||
public LinkedHashSet() {
|
||||
super(16, .75f, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new linked hash set with the same elements as the
|
||||
* specified collection. The linked hash set is created with an initial
|
||||
* capacity sufficient to hold the elements in the specified collection
|
||||
* and the default load factor (0.75).
|
||||
*
|
||||
* @param c the collection whose elements are to be placed into
|
||||
* this set
|
||||
* @throws NullPointerException if the specified collection is null
|
||||
*/
|
||||
public LinkedHashSet(Collection<? extends E> c) {
|
||||
super(Math.max(2*c.size(), 11), .75f, true);
|
||||
addAll(c);
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -1,698 +0,0 @@
|
|||
package java.util;
|
||||
|
||||
public class PriorityQueue<E> extends AbstractQueue<E>
|
||||
implements java.io.Serializable {
|
||||
|
||||
private static final long serialVersionUID = -7720805057305804111L;
|
||||
|
||||
private static final int DEFAULT_INITIAL_CAPACITY = 11;
|
||||
|
||||
/**
|
||||
* Priority queue represented as a balanced binary heap: the two
|
||||
* children of queue[n] are queue[2*n+1] and queue[2*(n+1)]. The
|
||||
* priority queue is ordered by comparator, or by the elements'
|
||||
* natural ordering, if comparator is null: For each node n in the
|
||||
* heap and each descendant d of n, n <= d. The element with the
|
||||
* lowest value is in queue[0], assuming the queue is nonempty.
|
||||
*/
|
||||
private transient Object[] queue;
|
||||
|
||||
/**
|
||||
* The number of elements in the priority queue.
|
||||
*/
|
||||
private int size = 0;
|
||||
|
||||
/**
|
||||
* The comparator, or null if priority queue uses elements'
|
||||
* natural ordering.
|
||||
*/
|
||||
private final Comparator<? super E> comparator;
|
||||
|
||||
/**
|
||||
* The number of times this priority queue has been
|
||||
* <i>structurally modified</i>. See AbstractList for gory details.
|
||||
*/
|
||||
private transient int modCount = 0;
|
||||
|
||||
/**
|
||||
* Creates a {@code PriorityQueue} with the default initial
|
||||
* capacity (11) that orders its elements according to their
|
||||
* {@linkplain Comparable natural ordering}.
|
||||
*/
|
||||
public PriorityQueue() {
|
||||
this(DEFAULT_INITIAL_CAPACITY, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code PriorityQueue} with the specified initial
|
||||
* capacity that orders its elements according to their
|
||||
* {@linkplain Comparable natural ordering}.
|
||||
*
|
||||
* @param initialCapacity the initial capacity for this priority queue
|
||||
* @throws IllegalArgumentException if {@code initialCapacity} is less
|
||||
* than 1
|
||||
*/
|
||||
public PriorityQueue(int initialCapacity) {
|
||||
this(initialCapacity, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code PriorityQueue} with the specified initial capacity
|
||||
* that orders its elements according to the specified comparator.
|
||||
*
|
||||
* @param initialCapacity the initial capacity for this priority queue
|
||||
* @param comparator the comparator that will be used to order this
|
||||
* priority queue. If {@code null}, the {@linkplain Comparable
|
||||
* natural ordering} of the elements will be used.
|
||||
* @throws IllegalArgumentException if {@code initialCapacity} is
|
||||
* less than 1
|
||||
*/
|
||||
public PriorityQueue(int initialCapacity,
|
||||
Comparator<? super E> comparator) {
|
||||
// Note: This restriction of at least one is not actually needed,
|
||||
// but continues for 1.5 compatibility
|
||||
if (initialCapacity < 1)
|
||||
throw new IllegalArgumentException();
|
||||
this.queue = new Object[initialCapacity];
|
||||
this.comparator = comparator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code PriorityQueue} containing the elements in the
|
||||
* specified collection. If the specified collection is an instance of
|
||||
* a {@link SortedSet} or is another {@code PriorityQueue}, this
|
||||
* priority queue will be ordered according to the same ordering.
|
||||
* Otherwise, this priority queue will be ordered according to the
|
||||
* {@linkplain Comparable natural ordering} of its elements.
|
||||
*
|
||||
* @param c the collection whose elements are to be placed
|
||||
* into this priority queue
|
||||
* @throws ClassCastException if elements of the specified collection
|
||||
* cannot be compared to one another according to the priority
|
||||
* queue's ordering
|
||||
* @throws NullPointerException if the specified collection or any
|
||||
* of its elements are null
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public PriorityQueue(Collection<? extends E> c) {
|
||||
if (c instanceof SortedSet<?>) {
|
||||
SortedSet<? extends E> ss = (SortedSet<? extends E>) c;
|
||||
this.comparator = (Comparator<? super E>) ss.comparator();
|
||||
initElementsFromCollection(ss);
|
||||
}
|
||||
else if (c instanceof PriorityQueue<?>) {
|
||||
PriorityQueue<? extends E> pq = (PriorityQueue<? extends E>) c;
|
||||
this.comparator = (Comparator<? super E>) pq.comparator();
|
||||
initFromPriorityQueue(pq);
|
||||
}
|
||||
else {
|
||||
this.comparator = null;
|
||||
initFromCollection(c);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code PriorityQueue} containing the elements in the
|
||||
* specified priority queue. This priority queue will be
|
||||
* ordered according to the same ordering as the given priority
|
||||
* queue.
|
||||
*
|
||||
* @param c the priority queue whose elements are to be placed
|
||||
* into this priority queue
|
||||
* @throws ClassCastException if elements of {@code c} cannot be
|
||||
* compared to one another according to {@code c}'s
|
||||
* ordering
|
||||
* @throws NullPointerException if the specified priority queue or any
|
||||
* of its elements are null
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public PriorityQueue(PriorityQueue<? extends E> c) {
|
||||
this.comparator = (Comparator<? super E>) c.comparator();
|
||||
initFromPriorityQueue(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a {@code PriorityQueue} containing the elements in the
|
||||
* specified sorted set. This priority queue will be ordered
|
||||
* according to the same ordering as the given sorted set.
|
||||
*
|
||||
* @param c the sorted set whose elements are to be placed
|
||||
* into this priority queue
|
||||
* @throws ClassCastException if elements of the specified sorted
|
||||
* set cannot be compared to one another according to the
|
||||
* sorted set's ordering
|
||||
* @throws NullPointerException if the specified sorted set or any
|
||||
* of its elements are null
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public PriorityQueue(SortedSet<? extends E> c) {
|
||||
this.comparator = (Comparator<? super E>) c.comparator();
|
||||
initElementsFromCollection(c);
|
||||
}
|
||||
|
||||
private void initFromPriorityQueue(PriorityQueue<? extends E> c) {
|
||||
if (c.getClass() == PriorityQueue.class) {
|
||||
this.queue = c.toArray();
|
||||
this.size = c.size();
|
||||
} else {
|
||||
initFromCollection(c);
|
||||
}
|
||||
}
|
||||
|
||||
private void initElementsFromCollection(Collection<? extends E> c) {
|
||||
Object[] a = c.toArray();
|
||||
// If c.toArray incorrectly doesn't return Object[], copy it.
|
||||
if (a.getClass() != Object[].class)
|
||||
a = Arrays.copyOf(a, a.length, Object[].class);
|
||||
int len = a.length;
|
||||
if (len == 1 || this.comparator != null)
|
||||
for (int i = 0; i < len; i++)
|
||||
if (a[i] == null)
|
||||
throw new NullPointerException();
|
||||
this.queue = a;
|
||||
this.size = a.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes queue array with elements from the given Collection.
|
||||
*
|
||||
* @param c the collection
|
||||
*/
|
||||
private void initFromCollection(Collection<? extends E> c) {
|
||||
initElementsFromCollection(c);
|
||||
heapify();
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum size of array to allocate.
|
||||
* Some VMs reserve some header words in an array.
|
||||
* Attempts to allocate larger arrays may result in
|
||||
* OutOfMemoryError: Requested array size exceeds VM limit
|
||||
*/
|
||||
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
|
||||
|
||||
/**
|
||||
* Increases the capacity of the array.
|
||||
*
|
||||
* @param minCapacity the desired minimum capacity
|
||||
*/
|
||||
private void grow(int minCapacity) {
|
||||
int oldCapacity = queue.length;
|
||||
// Double size if small; else grow by 50%
|
||||
int newCapacity = oldCapacity + ((oldCapacity < 64) ?
|
||||
(oldCapacity + 2) :
|
||||
(oldCapacity >> 1));
|
||||
// overflow-conscious code
|
||||
if (newCapacity - MAX_ARRAY_SIZE > 0)
|
||||
newCapacity = hugeCapacity(minCapacity);
|
||||
queue = Arrays.copyOf(queue, newCapacity);
|
||||
}
|
||||
|
||||
private static int hugeCapacity(int minCapacity) {
|
||||
if (minCapacity < 0) // overflow
|
||||
throw new OutOfMemoryError();
|
||||
return (minCapacity > MAX_ARRAY_SIZE) ?
|
||||
Integer.MAX_VALUE :
|
||||
MAX_ARRAY_SIZE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the specified element into this priority queue.
|
||||
*
|
||||
* @return {@code true} (as specified by {@link Collection#add})
|
||||
* @throws ClassCastException if the specified element cannot be
|
||||
* compared with elements currently in this priority queue
|
||||
* according to the priority queue's ordering
|
||||
* @throws NullPointerException if the specified element is null
|
||||
*/
|
||||
public boolean add(E e) {
|
||||
return offer(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts the specified element into this priority queue.
|
||||
*
|
||||
* @return {@code true} (as specified by {@link Queue#offer})
|
||||
* @throws ClassCastException if the specified element cannot be
|
||||
* compared with elements currently in this priority queue
|
||||
* according to the priority queue's ordering
|
||||
* @throws NullPointerException if the specified element is null
|
||||
*/
|
||||
public boolean offer(E e) {
|
||||
if (e == null)
|
||||
throw new NullPointerException();
|
||||
modCount++;
|
||||
int i = size;
|
||||
if (i >= queue.length)
|
||||
grow(i + 1);
|
||||
size = i + 1;
|
||||
if (i == 0)
|
||||
queue[0] = e;
|
||||
else
|
||||
siftUp(i, e);
|
||||
return true;
|
||||
}
|
||||
|
||||
public E peek() {
|
||||
if (size == 0)
|
||||
return null;
|
||||
return (E) queue[0];
|
||||
}
|
||||
|
||||
private int indexOf(Object o) {
|
||||
if (o != null) {
|
||||
for (int i = 0; i < size; i++)
|
||||
if (o.equals(queue[i]))
|
||||
return i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a single instance of the specified element from this queue,
|
||||
* if it is present. More formally, removes an element {@code e} such
|
||||
* that {@code o.equals(e)}, if this queue contains one or more such
|
||||
* elements. Returns {@code true} if and only if this queue contained
|
||||
* the specified element (or equivalently, if this queue changed as a
|
||||
* result of the call).
|
||||
*
|
||||
* @param o element to be removed from this queue, if present
|
||||
* @return {@code true} if this queue changed as a result of the call
|
||||
*/
|
||||
public boolean remove(Object o) {
|
||||
int i = indexOf(o);
|
||||
if (i == -1)
|
||||
return false;
|
||||
else {
|
||||
removeAt(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Version of remove using reference equality, not equals.
|
||||
* Needed by iterator.remove.
|
||||
*
|
||||
* @param o element to be removed from this queue, if present
|
||||
* @return {@code true} if removed
|
||||
*/
|
||||
boolean removeEq(Object o) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
if (o == queue[i]) {
|
||||
removeAt(i);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if this queue contains the specified element.
|
||||
* More formally, returns {@code true} if and only if this queue contains
|
||||
* at least one element {@code e} such that {@code o.equals(e)}.
|
||||
*
|
||||
* @param o object to be checked for containment in this queue
|
||||
* @return {@code true} if this queue contains the specified element
|
||||
*/
|
||||
public boolean contains(Object o) {
|
||||
return indexOf(o) != -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing all of the elements in this queue.
|
||||
* The elements are in no particular order.
|
||||
*
|
||||
* <p>The returned array will be "safe" in that no references to it are
|
||||
* maintained by this queue. (In other words, this method must allocate
|
||||
* a new array). The caller is thus free to modify the returned array.
|
||||
*
|
||||
* <p>This method acts as bridge between array-based and collection-based
|
||||
* APIs.
|
||||
*
|
||||
* @return an array containing all of the elements in this queue
|
||||
*/
|
||||
public Object[] toArray() {
|
||||
return Arrays.copyOf(queue, size);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array containing all of the elements in this queue; the
|
||||
* runtime type of the returned array is that of the specified array.
|
||||
* The returned array elements are in no particular order.
|
||||
* If the queue fits in the specified array, it is returned therein.
|
||||
* Otherwise, a new array is allocated with the runtime type of the
|
||||
* specified array and the size of this queue.
|
||||
*
|
||||
* <p>If the queue fits in the specified array with room to spare
|
||||
* (i.e., the array has more elements than the queue), the element in
|
||||
* the array immediately following the end of the collection is set to
|
||||
* {@code null}.
|
||||
*
|
||||
* <p>Like the {@link #toArray()} method, this method acts as bridge between
|
||||
* array-based and collection-based APIs. Further, this method allows
|
||||
* precise control over the runtime type of the output array, and may,
|
||||
* under certain circumstances, be used to save allocation costs.
|
||||
*
|
||||
* <p>Suppose <tt>x</tt> is a queue known to contain only strings.
|
||||
* The following code can be used to dump the queue into a newly
|
||||
* allocated array of <tt>String</tt>:
|
||||
*
|
||||
* <pre>
|
||||
* String[] y = x.toArray(new String[0]);</pre>
|
||||
*
|
||||
* Note that <tt>toArray(new Object[0])</tt> is identical in function to
|
||||
* <tt>toArray()</tt>.
|
||||
*
|
||||
* @param a the array into which the elements of the queue are to
|
||||
* be stored, if it is big enough; otherwise, a new array of the
|
||||
* same runtime type is allocated for this purpose.
|
||||
* @return an array containing all of the elements in this queue
|
||||
* @throws ArrayStoreException if the runtime type of the specified array
|
||||
* is not a supertype of the runtime type of every element in
|
||||
* this queue
|
||||
* @throws NullPointerException if the specified array is null
|
||||
*/
|
||||
public <T> T[] toArray(T[] a) {
|
||||
if (a.length < size)
|
||||
// Make a new array of a's runtime type, but my contents:
|
||||
return (T[]) Arrays.copyOf(queue, size, a.getClass());
|
||||
System.arraycopy(queue, 0, a, 0, size);
|
||||
if (a.length > size)
|
||||
a[size] = null;
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over the elements in this queue. The iterator
|
||||
* does not return the elements in any particular order.
|
||||
*
|
||||
* @return an iterator over the elements in this queue
|
||||
*/
|
||||
public Iterator<E> iterator() {
|
||||
return new Itr();
|
||||
}
|
||||
|
||||
private final class Itr implements Iterator<E> {
|
||||
/**
|
||||
* Index (into queue array) of element to be returned by
|
||||
* subsequent call to next.
|
||||
*/
|
||||
private int cursor = 0;
|
||||
|
||||
/**
|
||||
* Index of element returned by most recent call to next,
|
||||
* unless that element came from the forgetMeNot list.
|
||||
* Set to -1 if element is deleted by a call to remove.
|
||||
*/
|
||||
private int lastRet = -1;
|
||||
|
||||
/**
|
||||
* A queue of elements that were moved from the unvisited portion of
|
||||
* the heap into the visited portion as a result of "unlucky" element
|
||||
* removals during the iteration. (Unlucky element removals are those
|
||||
* that require a siftup instead of a siftdown.) We must visit all of
|
||||
* the elements in this list to complete the iteration. We do this
|
||||
* after we've completed the "normal" iteration.
|
||||
*
|
||||
* We expect that most iterations, even those involving removals,
|
||||
* will not need to store elements in this field.
|
||||
*/
|
||||
private ArrayDeque<E> forgetMeNot = null;
|
||||
|
||||
/**
|
||||
* Element returned by the most recent call to next iff that
|
||||
* element was drawn from the forgetMeNot list.
|
||||
*/
|
||||
private E lastRetElt = null;
|
||||
|
||||
/**
|
||||
* The modCount value that the iterator believes that the backing
|
||||
* Queue should have. If this expectation is violated, the iterator
|
||||
* has detected concurrent modification.
|
||||
*/
|
||||
private int expectedModCount = modCount;
|
||||
|
||||
public boolean hasNext() {
|
||||
return cursor < size ||
|
||||
(forgetMeNot != null && !forgetMeNot.isEmpty());
|
||||
}
|
||||
|
||||
public E next() {
|
||||
if (expectedModCount != modCount)
|
||||
throw new ConcurrentModificationException();
|
||||
if (cursor < size)
|
||||
return (E) queue[lastRet = cursor++];
|
||||
if (forgetMeNot != null) {
|
||||
lastRet = -1;
|
||||
lastRetElt = forgetMeNot.poll();
|
||||
if (lastRetElt != null)
|
||||
return lastRetElt;
|
||||
}
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
if (expectedModCount != modCount)
|
||||
throw new ConcurrentModificationException();
|
||||
if (lastRet != -1) {
|
||||
E moved = PriorityQueue.this.removeAt(lastRet);
|
||||
lastRet = -1;
|
||||
if (moved == null)
|
||||
cursor--;
|
||||
else {
|
||||
if (forgetMeNot == null)
|
||||
forgetMeNot = new ArrayDeque<>();
|
||||
forgetMeNot.add(moved);
|
||||
}
|
||||
} else if (lastRetElt != null) {
|
||||
PriorityQueue.this.removeEq(lastRetElt);
|
||||
lastRetElt = null;
|
||||
} else {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
expectedModCount = modCount;
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return size;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all of the elements from this priority queue.
|
||||
* The queue will be empty after this call returns.
|
||||
*/
|
||||
public void clear() {
|
||||
modCount++;
|
||||
for (int i = 0; i < size; i++)
|
||||
queue[i] = null;
|
||||
size = 0;
|
||||
}
|
||||
|
||||
public E poll() {
|
||||
if (size == 0)
|
||||
return null;
|
||||
int s = --size;
|
||||
modCount++;
|
||||
E result = (E) queue[0];
|
||||
E x = (E) queue[s];
|
||||
queue[s] = null;
|
||||
if (s != 0)
|
||||
siftDown(0, x);
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the ith element from queue.
|
||||
*
|
||||
* Normally this method leaves the elements at up to i-1,
|
||||
* inclusive, untouched. Under these circumstances, it returns
|
||||
* null. Occasionally, in order to maintain the heap invariant,
|
||||
* it must swap a later element of the list with one earlier than
|
||||
* i. Under these circumstances, this method returns the element
|
||||
* that was previously at the end of the list and is now at some
|
||||
* position before i. This fact is used by iterator.remove so as to
|
||||
* avoid missing traversing elements.
|
||||
*/
|
||||
private E removeAt(int i) {
|
||||
assert i >= 0 && i < size;
|
||||
modCount++;
|
||||
int s = --size;
|
||||
if (s == i) // removed last element
|
||||
queue[i] = null;
|
||||
else {
|
||||
E moved = (E) queue[s];
|
||||
queue[s] = null;
|
||||
siftDown(i, moved);
|
||||
if (queue[i] == moved) {
|
||||
siftUp(i, moved);
|
||||
if (queue[i] != moved)
|
||||
return moved;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts item x at position k, maintaining heap invariant by
|
||||
* promoting x up the tree until it is greater than or equal to
|
||||
* its parent, or is the root.
|
||||
*
|
||||
* To simplify and speed up coercions and comparisons. the
|
||||
* Comparable and Comparator versions are separated into different
|
||||
* methods that are otherwise identical. (Similarly for siftDown.)
|
||||
*
|
||||
* @param k the position to fill
|
||||
* @param x the item to insert
|
||||
*/
|
||||
private void siftUp(int k, E x) {
|
||||
if (comparator != null)
|
||||
siftUpUsingComparator(k, x);
|
||||
else
|
||||
siftUpComparable(k, x);
|
||||
}
|
||||
|
||||
private void siftUpComparable(int k, E x) {
|
||||
Comparable<? super E> key = (Comparable<? super E>) x;
|
||||
while (k > 0) {
|
||||
int parent = (k - 1) >>> 1;
|
||||
Object e = queue[parent];
|
||||
if (key.compareTo((E) e) >= 0)
|
||||
break;
|
||||
queue[k] = e;
|
||||
k = parent;
|
||||
}
|
||||
queue[k] = key;
|
||||
}
|
||||
|
||||
private void siftUpUsingComparator(int k, E x) {
|
||||
while (k > 0) {
|
||||
int parent = (k - 1) >>> 1;
|
||||
Object e = queue[parent];
|
||||
if (comparator.compare(x, (E) e) >= 0)
|
||||
break;
|
||||
queue[k] = e;
|
||||
k = parent;
|
||||
}
|
||||
queue[k] = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Inserts item x at position k, maintaining heap invariant by
|
||||
* demoting x down the tree repeatedly until it is less than or
|
||||
* equal to its children or is a leaf.
|
||||
*
|
||||
* @param k the position to fill
|
||||
* @param x the item to insert
|
||||
*/
|
||||
private void siftDown(int k, E x) {
|
||||
if (comparator != null)
|
||||
siftDownUsingComparator(k, x);
|
||||
else
|
||||
siftDownComparable(k, x);
|
||||
}
|
||||
|
||||
private void siftDownComparable(int k, E x) {
|
||||
Comparable<? super E> key = (Comparable<? super E>)x;
|
||||
int half = size >>> 1; // loop while a non-leaf
|
||||
while (k < half) {
|
||||
int child = (k << 1) + 1; // assume left child is least
|
||||
Object c = queue[child];
|
||||
int right = child + 1;
|
||||
if (right < size &&
|
||||
((Comparable<? super E>) c).compareTo((E) queue[right]) > 0)
|
||||
c = queue[child = right];
|
||||
if (key.compareTo((E) c) <= 0)
|
||||
break;
|
||||
queue[k] = c;
|
||||
k = child;
|
||||
}
|
||||
queue[k] = key;
|
||||
}
|
||||
|
||||
private void siftDownUsingComparator(int k, E x) {
|
||||
int half = size >>> 1;
|
||||
while (k < half) {
|
||||
int child = (k << 1) + 1;
|
||||
Object c = queue[child];
|
||||
int right = child + 1;
|
||||
if (right < size &&
|
||||
comparator.compare((E) c, (E) queue[right]) > 0)
|
||||
c = queue[child = right];
|
||||
if (comparator.compare(x, (E) c) <= 0)
|
||||
break;
|
||||
queue[k] = c;
|
||||
k = child;
|
||||
}
|
||||
queue[k] = x;
|
||||
}
|
||||
|
||||
/**
|
||||
* Establishes the heap invariant (described above) in the entire tree,
|
||||
* assuming nothing about the order of the elements prior to the call.
|
||||
*/
|
||||
private void heapify() {
|
||||
for (int i = (size >>> 1) - 1; i >= 0; i--)
|
||||
siftDown(i, (E) queue[i]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the comparator used to order the elements in this
|
||||
* queue, or {@code null} if this queue is sorted according to
|
||||
* the {@linkplain Comparable natural ordering} of its elements.
|
||||
*
|
||||
* @return the comparator used to order this queue, or
|
||||
* {@code null} if this queue is sorted according to the
|
||||
* natural ordering of its elements
|
||||
*/
|
||||
public Comparator<? super E> comparator() {
|
||||
return comparator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves the state of the instance to a stream (that
|
||||
* is, serializes it).
|
||||
*
|
||||
* @serialData The length of the array backing the instance is
|
||||
* emitted (int), followed by all of its elements
|
||||
* (each an {@code Object}) in the proper order.
|
||||
* @param s the stream
|
||||
*/
|
||||
private void writeObject(java.io.ObjectOutputStream s)
|
||||
throws java.io.IOException{
|
||||
// Write out element count, and any hidden stuff
|
||||
s.defaultWriteObject();
|
||||
|
||||
// Write out array length, for compatibility with 1.5 version
|
||||
s.writeInt(Math.max(2, size + 1));
|
||||
|
||||
// Write out all elements in the "proper order".
|
||||
for (int i = 0; i < size; i++)
|
||||
s.writeObject(queue[i]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconstitutes the {@code PriorityQueue} instance from a stream
|
||||
* (that is, deserializes it).
|
||||
*
|
||||
* @param s the stream
|
||||
*/
|
||||
private void readObject(java.io.ObjectInputStream s)
|
||||
throws java.io.IOException, ClassNotFoundException {
|
||||
// Read in size, and any hidden stuff
|
||||
s.defaultReadObject();
|
||||
|
||||
// Read in (and discard) array length
|
||||
s.readInt();
|
||||
|
||||
queue = new Object[size];
|
||||
|
||||
// Read in all elements.
|
||||
for (int i = 0; i < size; i++)
|
||||
queue[i] = s.readObject();
|
||||
|
||||
// Elements are guaranteed to be in "proper order", but the
|
||||
// spec has never explained what that might be.
|
||||
heapify();
|
||||
}
|
||||
}
|
|
@ -1,78 +0,0 @@
|
|||
|
||||
package java.util;
|
||||
|
||||
public interface Queue<E> extends Collection<E> {
|
||||
/**
|
||||
* Inserts the specified element into this queue if it is possible to do so
|
||||
* immediately without violating capacity restrictions, returning
|
||||
* <tt>true</tt> upon success and throwing an <tt>IllegalStateException</tt>
|
||||
* if no space is currently available.
|
||||
*
|
||||
* @param e the element to add
|
||||
* @return <tt>true</tt> (as specified by {@link Collection#add})
|
||||
* @throws IllegalStateException if the element cannot be added at this
|
||||
* time due to capacity restrictions
|
||||
* @throws ClassCastException if the class of the specified element
|
||||
* prevents it from being added to this queue
|
||||
* @throws NullPointerException if the specified element is null and
|
||||
* this queue does not permit null elements
|
||||
* @throws IllegalArgumentException if some property of this element
|
||||
* prevents it from being added to this queue
|
||||
*/
|
||||
boolean add(E e);
|
||||
|
||||
/**
|
||||
* Inserts the specified element into this queue if it is possible to do
|
||||
* so immediately without violating capacity restrictions.
|
||||
* When using a capacity-restricted queue, this method is generally
|
||||
* preferable to {@link #add}, which can fail to insert an element only
|
||||
* by throwing an exception.
|
||||
*
|
||||
* @param e the element to add
|
||||
* @return <tt>true</tt> if the element was added to this queue, else
|
||||
* <tt>false</tt>
|
||||
* @throws ClassCastException if the class of the specified element
|
||||
* prevents it from being added to this queue
|
||||
* @throws NullPointerException if the specified element is null and
|
||||
* this queue does not permit null elements
|
||||
* @throws IllegalArgumentException if some property of this element
|
||||
* prevents it from being added to this queue
|
||||
*/
|
||||
boolean offer(E e);
|
||||
|
||||
/**
|
||||
* Retrieves and removes the head of this queue. This method differs
|
||||
* from {@link #poll poll} only in that it throws an exception if this
|
||||
* queue is empty.
|
||||
*
|
||||
* @return the head of this queue
|
||||
* @throws NoSuchElementException if this queue is empty
|
||||
*/
|
||||
E remove();
|
||||
|
||||
/**
|
||||
* Retrieves and removes the head of this queue,
|
||||
* or returns <tt>null</tt> if this queue is empty.
|
||||
*
|
||||
* @return the head of this queue, or <tt>null</tt> if this queue is empty
|
||||
*/
|
||||
E poll();
|
||||
|
||||
/**
|
||||
* Retrieves, but does not remove, the head of this queue. This method
|
||||
* differs from {@link #peek peek} only in that it throws an exception
|
||||
* if this queue is empty.
|
||||
*
|
||||
* @return the head of this queue
|
||||
* @throws NoSuchElementException if this queue is empty
|
||||
*/
|
||||
E element();
|
||||
|
||||
/**
|
||||
* Retrieves, but does not remove, the head of this queue,
|
||||
* or returns <tt>null</tt> if this queue is empty.
|
||||
*
|
||||
* @return the head of this queue, or <tt>null</tt> if this queue is empty
|
||||
*/
|
||||
E peek();
|
||||
}
|
|
@ -1,97 +0,0 @@
|
|||
|
||||
package java.util;
|
||||
|
||||
public
|
||||
class Stack<E> extends Vector<E> {
|
||||
/**
|
||||
* Creates an empty Stack.
|
||||
*/
|
||||
public Stack() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Pushes an item onto the top of this stack. This has exactly
|
||||
* the same effect as:
|
||||
* <blockquote><pre>
|
||||
* addElement(item)</pre></blockquote>
|
||||
*
|
||||
* @param item the item to be pushed onto this stack.
|
||||
* @return the <code>item</code> argument.
|
||||
* @see java.util.Vector#addElement
|
||||
*/
|
||||
public E push(E item) {
|
||||
addElement(item);
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the object at the top of this stack and returns that
|
||||
* object as the value of this function.
|
||||
*
|
||||
* @return The object at the top of this stack (the last item
|
||||
* of the <tt>Vector</tt> object).
|
||||
* @throws EmptyStackException if this stack is empty.
|
||||
*/
|
||||
public synchronized E pop() {
|
||||
E obj;
|
||||
int len = size();
|
||||
|
||||
obj = peek();
|
||||
removeElementAt(len - 1);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
/**
|
||||
* Looks at the object at the top of this stack without removing it
|
||||
* from the stack.
|
||||
*
|
||||
* @return the object at the top of this stack (the last item
|
||||
* of the <tt>Vector</tt> object).
|
||||
* @throws EmptyStackException if this stack is empty.
|
||||
*/
|
||||
public synchronized E peek() {
|
||||
int len = size();
|
||||
|
||||
if (len == 0)
|
||||
throw new EmptyStackException();
|
||||
return elementAt(len - 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if this stack is empty.
|
||||
*
|
||||
* @return <code>true</code> if and only if this stack contains
|
||||
* no items; <code>false</code> otherwise.
|
||||
*/
|
||||
public boolean empty() {
|
||||
return size() == 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the 1-based position where an object is on this stack.
|
||||
* If the object <tt>o</tt> occurs as an item in this stack, this
|
||||
* method returns the distance from the top of the stack of the
|
||||
* occurrence nearest the top of the stack; the topmost item on the
|
||||
* stack is considered to be at distance <tt>1</tt>. The <tt>equals</tt>
|
||||
* method is used to compare <tt>o</tt> to the
|
||||
* items in this stack.
|
||||
*
|
||||
* @param o the desired object.
|
||||
* @return the 1-based position from the top of the stack where
|
||||
* the object is located; the return value <code>-1</code>
|
||||
* indicates that the object is not on the stack.
|
||||
*/
|
||||
public synchronized int search(Object o) {
|
||||
int i = lastIndexOf(o);
|
||||
|
||||
if (i >= 0) {
|
||||
return size() - i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/** use serialVersionUID from JDK 1.0.2 for interoperability */
|
||||
private static final long serialVersionUID = 1224463164541339165L;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -1,527 +0,0 @@
|
|||
|
||||
|
||||
package java.lang;
|
||||
|
||||
public final class StringBuffer
|
||||
extends AbstractStringBuilder
|
||||
implements java.io.Serializable, CharSequence
|
||||
{
|
||||
|
||||
/** use serialVersionUID from JDK 1.0.2 for interoperability */
|
||||
static final long serialVersionUID = 3388685877147921107L;
|
||||
|
||||
/**
|
||||
* Constructs a string buffer with no characters in it and an
|
||||
* initial capacity of 16 characters.
|
||||
*/
|
||||
public StringBuffer() {
|
||||
super(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a string buffer with no characters in it and
|
||||
* the specified initial capacity.
|
||||
*
|
||||
* @param capacity the initial capacity.
|
||||
* @exception NegativeArraySizeException if the <code>capacity</code>
|
||||
* argument is less than <code>0</code>.
|
||||
*/
|
||||
public StringBuffer(int capacity) {
|
||||
super(capacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a string buffer initialized to the contents of the
|
||||
* specified string. The initial capacity of the string buffer is
|
||||
* <code>16</code> plus the length of the string argument.
|
||||
*
|
||||
* @param str the initial contents of the buffer.
|
||||
* @exception NullPointerException if <code>str</code> is <code>null</code>
|
||||
*/
|
||||
public StringBuffer(String str) {
|
||||
super(str.length() + 16);
|
||||
append(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a string buffer that contains the same characters
|
||||
* as the specified <code>CharSequence</code>. The initial capacity of
|
||||
* the string buffer is <code>16</code> plus the length of the
|
||||
* <code>CharSequence</code> argument.
|
||||
* <p>
|
||||
* If the length of the specified <code>CharSequence</code> is
|
||||
* less than or equal to zero, then an empty buffer of capacity
|
||||
* <code>16</code> is returned.
|
||||
*
|
||||
* @param seq the sequence to copy.
|
||||
* @exception NullPointerException if <code>seq</code> is <code>null</code>
|
||||
* @since 1.5
|
||||
*/
|
||||
public StringBuffer(CharSequence seq) {
|
||||
this(seq.length() + 16);
|
||||
append(seq);
|
||||
}
|
||||
|
||||
public synchronized int length() {
|
||||
return count;
|
||||
}
|
||||
|
||||
public synchronized int capacity() {
|
||||
return value.length;
|
||||
}
|
||||
|
||||
|
||||
public synchronized void ensureCapacity(int minimumCapacity) {
|
||||
if (minimumCapacity > value.length) {
|
||||
expandCapacity(minimumCapacity);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.5
|
||||
*/
|
||||
public synchronized void trimToSize() {
|
||||
super.trimToSize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
* @see #length()
|
||||
*/
|
||||
public synchronized void setLength(int newLength) {
|
||||
super.setLength(newLength);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
* @see #length()
|
||||
*/
|
||||
public synchronized char charAt(int index) {
|
||||
if ((index < 0) || (index >= count))
|
||||
throw new StringIndexOutOfBoundsException(index);
|
||||
return value[index];
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.5
|
||||
*/
|
||||
public synchronized int codePointAt(int index) {
|
||||
return super.codePointAt(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.5
|
||||
*/
|
||||
public synchronized int codePointBefore(int index) {
|
||||
return super.codePointBefore(index);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.5
|
||||
*/
|
||||
public synchronized int codePointCount(int beginIndex, int endIndex) {
|
||||
return super.codePointCount(beginIndex, endIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.5
|
||||
*/
|
||||
public synchronized int offsetByCodePoints(int index, int codePointOffset) {
|
||||
return super.offsetByCodePoints(index, codePointOffset);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public synchronized void getChars(int srcBegin, int srcEnd, char[] dst,
|
||||
int dstBegin)
|
||||
{
|
||||
super.getChars(srcBegin, srcEnd, dst, dstBegin);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
* @see #length()
|
||||
*/
|
||||
public synchronized void setCharAt(int index, char ch) {
|
||||
if ((index < 0) || (index >= count))
|
||||
throw new StringIndexOutOfBoundsException(index);
|
||||
value[index] = ch;
|
||||
}
|
||||
|
||||
public synchronized StringBuffer append(Object obj) {
|
||||
super.append(String.valueOf(obj));
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized StringBuffer append(String str) {
|
||||
super.append(str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the specified <tt>StringBuffer</tt> to this sequence.
|
||||
* <p>
|
||||
* The characters of the <tt>StringBuffer</tt> argument are appended,
|
||||
* in order, to the contents of this <tt>StringBuffer</tt>, increasing the
|
||||
* length of this <tt>StringBuffer</tt> by the length of the argument.
|
||||
* If <tt>sb</tt> is <tt>null</tt>, then the four characters
|
||||
* <tt>"null"</tt> are appended to this <tt>StringBuffer</tt>.
|
||||
* <p>
|
||||
* Let <i>n</i> be the length of the old character sequence, the one
|
||||
* contained in the <tt>StringBuffer</tt> just prior to execution of the
|
||||
* <tt>append</tt> method. Then the character at index <i>k</i> in
|
||||
* the new character sequence is equal to the character at index <i>k</i>
|
||||
* in the old character sequence, if <i>k</i> is less than <i>n</i>;
|
||||
* otherwise, it is equal to the character at index <i>k-n</i> in the
|
||||
* argument <code>sb</code>.
|
||||
* <p>
|
||||
* This method synchronizes on <code>this</code> (the destination)
|
||||
* object but does not synchronize on the source (<code>sb</code>).
|
||||
*
|
||||
* @param sb the <tt>StringBuffer</tt> to append.
|
||||
* @return a reference to this object.
|
||||
* @since 1.4
|
||||
*/
|
||||
public synchronized StringBuffer append(StringBuffer sb) {
|
||||
super.append(sb);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Appends the specified <code>CharSequence</code> to this
|
||||
* sequence.
|
||||
* <p>
|
||||
* The characters of the <code>CharSequence</code> argument are appended,
|
||||
* in order, increasing the length of this sequence by the length of the
|
||||
* argument.
|
||||
*
|
||||
* <p>The result of this method is exactly the same as if it were an
|
||||
* invocation of this.append(s, 0, s.length());
|
||||
*
|
||||
* <p>This method synchronizes on this (the destination)
|
||||
* object but does not synchronize on the source (<code>s</code>).
|
||||
*
|
||||
* <p>If <code>s</code> is <code>null</code>, then the four characters
|
||||
* <code>"null"</code> are appended.
|
||||
*
|
||||
* @param s the <code>CharSequence</code> to append.
|
||||
* @return a reference to this object.
|
||||
* @since 1.5
|
||||
*/
|
||||
public StringBuffer append(CharSequence s) {
|
||||
// Note, synchronization achieved via other invocations
|
||||
if (s == null)
|
||||
s = "null";
|
||||
if (s instanceof String)
|
||||
return this.append((String)s);
|
||||
if (s instanceof StringBuffer)
|
||||
return this.append((StringBuffer)s);
|
||||
return this.append(s, 0, s.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
* @since 1.5
|
||||
*/
|
||||
public synchronized StringBuffer append(CharSequence s, int start, int end)
|
||||
{
|
||||
super.append(s, start, end);
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized StringBuffer append(char[] str) {
|
||||
super.append(str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public synchronized StringBuffer append(char[] str, int offset, int len) {
|
||||
super.append(str, offset, len);
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized StringBuffer append(boolean b) {
|
||||
super.append(b);
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized StringBuffer append(char c) {
|
||||
super.append(c);
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized StringBuffer append(int i) {
|
||||
super.append(i);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.5
|
||||
*/
|
||||
public synchronized StringBuffer appendCodePoint(int codePoint) {
|
||||
super.appendCodePoint(codePoint);
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized StringBuffer append(long lng) {
|
||||
super.append(lng);
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized StringBuffer append(float f) {
|
||||
super.append(f);
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized StringBuffer append(double d) {
|
||||
super.append(d);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
* @since 1.2
|
||||
*/
|
||||
public synchronized StringBuffer delete(int start, int end) {
|
||||
super.delete(start, end);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
* @since 1.2
|
||||
*/
|
||||
public synchronized StringBuffer deleteCharAt(int index) {
|
||||
super.deleteCharAt(index);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
* @since 1.2
|
||||
*/
|
||||
public synchronized StringBuffer replace(int start, int end, String str) {
|
||||
super.replace(start, end, str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
* @since 1.2
|
||||
*/
|
||||
public synchronized String substring(int start) {
|
||||
return substring(start, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
* @since 1.4
|
||||
*/
|
||||
public synchronized CharSequence subSequence(int start, int end) {
|
||||
return super.substring(start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
* @since 1.2
|
||||
*/
|
||||
public synchronized String substring(int start, int end) {
|
||||
return super.substring(start, end);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
* @since 1.2
|
||||
*/
|
||||
public synchronized StringBuffer insert(int index, char[] str, int offset,
|
||||
int len)
|
||||
{
|
||||
super.insert(index, str, offset, len);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public synchronized StringBuffer insert(int offset, Object obj) {
|
||||
super.insert(offset, String.valueOf(obj));
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public synchronized StringBuffer insert(int offset, String str) {
|
||||
super.insert(offset, str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public synchronized StringBuffer insert(int offset, char[] str) {
|
||||
super.insert(offset, str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
* @since 1.5
|
||||
*/
|
||||
public StringBuffer insert(int dstOffset, CharSequence s) {
|
||||
// Note, synchronization achieved via other invocations
|
||||
if (s == null)
|
||||
s = "null";
|
||||
if (s instanceof String)
|
||||
return this.insert(dstOffset, (String)s);
|
||||
return this.insert(dstOffset, s, 0, s.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
* @since 1.5
|
||||
*/
|
||||
public synchronized StringBuffer insert(int dstOffset, CharSequence s,
|
||||
int start, int end)
|
||||
{
|
||||
super.insert(dstOffset, s, start, end);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuffer insert(int offset, boolean b) {
|
||||
return insert(offset, String.valueOf(b));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public synchronized StringBuffer insert(int offset, char c) {
|
||||
super.insert(offset, c);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuffer insert(int offset, int i) {
|
||||
return insert(offset, String.valueOf(i));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuffer insert(int offset, long l) {
|
||||
return insert(offset, String.valueOf(l));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuffer insert(int offset, float f) {
|
||||
return insert(offset, String.valueOf(f));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuffer insert(int offset, double d) {
|
||||
return insert(offset, String.valueOf(d));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
* @since 1.4
|
||||
*/
|
||||
public int indexOf(String str) {
|
||||
return indexOf(str, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
* @since 1.4
|
||||
*/
|
||||
public synchronized int indexOf(String str, int fromIndex) {
|
||||
return String.indexOf(value, 0, count,
|
||||
str.toCharArray(), 0, str.length(), fromIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
* @since 1.4
|
||||
*/
|
||||
public int lastIndexOf(String str) {
|
||||
// Note, synchronization achieved via other invocations
|
||||
return lastIndexOf(str, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
* @since 1.4
|
||||
*/
|
||||
public synchronized int lastIndexOf(String str, int fromIndex) {
|
||||
return String.lastIndexOf(value, 0, count,
|
||||
str.toCharArray(), 0, str.length(), fromIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since JDK1.0.2
|
||||
*/
|
||||
public synchronized StringBuffer reverse() {
|
||||
super.reverse();
|
||||
return this;
|
||||
}
|
||||
|
||||
public synchronized String toString() {
|
||||
return new String(value, 0, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Serializable fields for StringBuffer.
|
||||
*
|
||||
* @serialField value char[]
|
||||
* The backing character array of this StringBuffer.
|
||||
* @serialField count int
|
||||
* The number of characters in this StringBuffer.
|
||||
* @serialField shared boolean
|
||||
* A flag indicating whether the backing array is shared.
|
||||
* The value is ignored upon deserialization.
|
||||
*/
|
||||
private static final java.io.ObjectStreamField[] serialPersistentFields =
|
||||
{
|
||||
new java.io.ObjectStreamField("value", char[].class),
|
||||
new java.io.ObjectStreamField("count", Integer.TYPE),
|
||||
new java.io.ObjectStreamField("shared", Boolean.TYPE),
|
||||
};
|
||||
|
||||
/**
|
||||
* readObject is called to restore the state of the StringBuffer from
|
||||
* a stream.
|
||||
*/
|
||||
private synchronized void writeObject(java.io.ObjectOutputStream s)
|
||||
throws java.io.IOException {
|
||||
java.io.ObjectOutputStream.PutField fields = s.putFields();
|
||||
fields.put("value", value);
|
||||
fields.put("count", count);
|
||||
fields.put("shared", false);
|
||||
s.writeFields();
|
||||
}
|
||||
|
||||
/**
|
||||
* readObject is called to restore the state of the StringBuffer from
|
||||
* a stream.
|
||||
*/
|
||||
private void readObject(java.io.ObjectInputStream s)
|
||||
throws java.io.IOException, ClassNotFoundException {
|
||||
java.io.ObjectInputStream.GetField fields = s.readFields();
|
||||
value = (char[])fields.get("value", null);
|
||||
count = fields.get("count", 0);
|
||||
}
|
||||
}
|
|
@ -1,369 +0,0 @@
|
|||
|
||||
package java.lang;
|
||||
|
||||
public final class StringBuilder
|
||||
extends AbstractStringBuilder
|
||||
implements java.io.Serializable, CharSequence
|
||||
{
|
||||
|
||||
/** use serialVersionUID for interoperability */
|
||||
static final long serialVersionUID = 4383685877147921099L;
|
||||
|
||||
/**
|
||||
* Constructs a string builder with no characters in it and an
|
||||
* initial capacity of 16 characters.
|
||||
*/
|
||||
public StringBuilder() {
|
||||
super(16);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a string builder with no characters in it and an
|
||||
* initial capacity specified by the <code>capacity</code> argument.
|
||||
*
|
||||
* @param capacity the initial capacity.
|
||||
* @throws NegativeArraySizeException if the <code>capacity</code>
|
||||
* argument is less than <code>0</code>.
|
||||
*/
|
||||
public StringBuilder(int capacity) {
|
||||
super(capacity);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a string builder initialized to the contents of the
|
||||
* specified string. The initial capacity of the string builder is
|
||||
* <code>16</code> plus the length of the string argument.
|
||||
*
|
||||
* @param str the initial contents of the buffer.
|
||||
* @throws NullPointerException if <code>str</code> is <code>null</code>
|
||||
*/
|
||||
public StringBuilder(String str) {
|
||||
super(str.length() + 16);
|
||||
append(str);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a string builder that contains the same characters
|
||||
* as the specified <code>CharSequence</code>. The initial capacity of
|
||||
* the string builder is <code>16</code> plus the length of the
|
||||
* <code>CharSequence</code> argument.
|
||||
*
|
||||
* @param seq the sequence to copy.
|
||||
* @throws NullPointerException if <code>seq</code> is <code>null</code>
|
||||
*/
|
||||
public StringBuilder(CharSequence seq) {
|
||||
this(seq.length() + 16);
|
||||
append(seq);
|
||||
}
|
||||
|
||||
public StringBuilder append(Object obj) {
|
||||
return append(String.valueOf(obj));
|
||||
}
|
||||
|
||||
public StringBuilder append(String str) {
|
||||
super.append(str);
|
||||
return this;
|
||||
}
|
||||
|
||||
// Appends the specified string builder to this sequence.
|
||||
private StringBuilder append(StringBuilder sb) {
|
||||
if (sb == null)
|
||||
return append("null");
|
||||
int len = sb.length();
|
||||
int newcount = count + len;
|
||||
if (newcount > value.length)
|
||||
expandCapacity(newcount);
|
||||
sb.getChars(0, len, value, count);
|
||||
count = newcount;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends the specified <tt>StringBuffer</tt> to this sequence.
|
||||
* <p>
|
||||
* The characters of the <tt>StringBuffer</tt> argument are appended,
|
||||
* in order, to this sequence, increasing the
|
||||
* length of this sequence by the length of the argument.
|
||||
* If <tt>sb</tt> is <tt>null</tt>, then the four characters
|
||||
* <tt>"null"</tt> are appended to this sequence.
|
||||
* <p>
|
||||
* Let <i>n</i> be the length of this character sequence just prior to
|
||||
* execution of the <tt>append</tt> method. Then the character at index
|
||||
* <i>k</i> in the new character sequence is equal to the character at
|
||||
* index <i>k</i> in the old character sequence, if <i>k</i> is less than
|
||||
* <i>n</i>; otherwise, it is equal to the character at index <i>k-n</i>
|
||||
* in the argument <code>sb</code>.
|
||||
*
|
||||
* @param sb the <tt>StringBuffer</tt> to append.
|
||||
* @return a reference to this object.
|
||||
*/
|
||||
public StringBuilder append(StringBuffer sb) {
|
||||
super.append(sb);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
*/
|
||||
public StringBuilder append(CharSequence s) {
|
||||
if (s == null)
|
||||
s = "null";
|
||||
if (s instanceof String)
|
||||
return this.append((String)s);
|
||||
if (s instanceof StringBuffer)
|
||||
return this.append((StringBuffer)s);
|
||||
if (s instanceof StringBuilder)
|
||||
return this.append((StringBuilder)s);
|
||||
return this.append(s, 0, s.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder append(CharSequence s, int start, int end) {
|
||||
super.append(s, start, end);
|
||||
return this;
|
||||
}
|
||||
|
||||
public StringBuilder append(char[] str) {
|
||||
super.append(str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder append(char[] str, int offset, int len) {
|
||||
super.append(str, offset, len);
|
||||
return this;
|
||||
}
|
||||
|
||||
public StringBuilder append(boolean b) {
|
||||
super.append(b);
|
||||
return this;
|
||||
}
|
||||
|
||||
public StringBuilder append(char c) {
|
||||
super.append(c);
|
||||
return this;
|
||||
}
|
||||
|
||||
public StringBuilder append(int i) {
|
||||
super.append(i);
|
||||
return this;
|
||||
}
|
||||
|
||||
public StringBuilder append(long lng) {
|
||||
super.append(lng);
|
||||
return this;
|
||||
}
|
||||
|
||||
public StringBuilder append(float f) {
|
||||
super.append(f);
|
||||
return this;
|
||||
}
|
||||
|
||||
public StringBuilder append(double d) {
|
||||
super.append(d);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.5
|
||||
*/
|
||||
public StringBuilder appendCodePoint(int codePoint) {
|
||||
super.appendCodePoint(codePoint);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder delete(int start, int end) {
|
||||
super.delete(start, end);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder deleteCharAt(int index) {
|
||||
super.deleteCharAt(index);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder replace(int start, int end, String str) {
|
||||
super.replace(start, end, str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int index, char[] str, int offset,
|
||||
int len)
|
||||
{
|
||||
super.insert(index, str, offset, len);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int offset, Object obj) {
|
||||
return insert(offset, String.valueOf(obj));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int offset, String str) {
|
||||
super.insert(offset, str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int offset, char[] str) {
|
||||
super.insert(offset, str);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int dstOffset, CharSequence s) {
|
||||
if (s == null)
|
||||
s = "null";
|
||||
if (s instanceof String)
|
||||
return this.insert(dstOffset, (String)s);
|
||||
return this.insert(dstOffset, s, 0, s.length());
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int dstOffset, CharSequence s,
|
||||
int start, int end)
|
||||
{
|
||||
super.insert(dstOffset, s, start, end);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int offset, boolean b) {
|
||||
super.insert(offset, b);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws IndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int offset, char c) {
|
||||
super.insert(offset, c);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int offset, int i) {
|
||||
return insert(offset, String.valueOf(i));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int offset, long l) {
|
||||
return insert(offset, String.valueOf(l));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int offset, float f) {
|
||||
return insert(offset, String.valueOf(f));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws StringIndexOutOfBoundsException {@inheritDoc}
|
||||
*/
|
||||
public StringBuilder insert(int offset, double d) {
|
||||
return insert(offset, String.valueOf(d));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
*/
|
||||
public int indexOf(String str) {
|
||||
return indexOf(str, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
*/
|
||||
public int indexOf(String str, int fromIndex) {
|
||||
return String.indexOf(value, 0, count,
|
||||
str.toCharArray(), 0, str.length(), fromIndex);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
*/
|
||||
public int lastIndexOf(String str) {
|
||||
return lastIndexOf(str, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NullPointerException {@inheritDoc}
|
||||
*/
|
||||
public int lastIndexOf(String str, int fromIndex) {
|
||||
return String.lastIndexOf(value, 0, count,
|
||||
str.toCharArray(), 0, str.length(), fromIndex);
|
||||
}
|
||||
|
||||
public StringBuilder reverse() {
|
||||
super.reverse();
|
||||
return this;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
// Create a copy, don't share the array
|
||||
return new String(value, 0, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the state of the <tt>StringBuilder</tt> instance to a stream
|
||||
* (that is, serialize it).
|
||||
*
|
||||
* @serialData the number of characters currently stored in the string
|
||||
* builder (<tt>int</tt>), followed by the characters in the
|
||||
* string builder (<tt>char[]</tt>). The length of the
|
||||
* <tt>char</tt> array may be greater than the number of
|
||||
* characters currently stored in the string builder, in which
|
||||
* case extra characters are ignored.
|
||||
*/
|
||||
private void writeObject(java.io.ObjectOutputStream s)
|
||||
throws java.io.IOException {
|
||||
s.defaultWriteObject();
|
||||
s.writeInt(count);
|
||||
s.writeObject(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* readObject is called to restore the state of the StringBuffer from
|
||||
* a stream.
|
||||
*/
|
||||
private void readObject(java.io.ObjectInputStream s)
|
||||
throws java.io.IOException, ClassNotFoundException {
|
||||
s.defaultReadObject();
|
||||
count = s.readInt();
|
||||
value = (char[]) s.readObject();
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because it is too large
Load Diff
|
@ -1,451 +0,0 @@
|
|||
|
||||
package java.util;
|
||||
|
||||
public class TreeSet<E> extends AbstractSet<E>
|
||||
implements NavigableSet<E>, Cloneable, java.io.Serializable
|
||||
{
|
||||
/**
|
||||
* The backing map.
|
||||
*/
|
||||
private transient NavigableMap<E,Object> m;
|
||||
|
||||
// Dummy value to associate with an Object in the backing Map
|
||||
private static final Object PRESENT = new Object();
|
||||
|
||||
/**
|
||||
* Constructs a set backed by the specified navigable map.
|
||||
*/
|
||||
TreeSet(NavigableMap<E,Object> m) {
|
||||
this.m = m;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new, empty tree set, sorted according to the
|
||||
* natural ordering of its elements. All elements inserted into
|
||||
* the set must implement the {@link Comparable} interface.
|
||||
* Furthermore, all such elements must be <i>mutually
|
||||
* comparable</i>: {@code e1.compareTo(e2)} must not throw a
|
||||
* {@code ClassCastException} for any elements {@code e1} and
|
||||
* {@code e2} in the set. If the user attempts to add an element
|
||||
* to the set that violates this constraint (for example, the user
|
||||
* attempts to add a string element to a set whose elements are
|
||||
* integers), the {@code add} call will throw a
|
||||
* {@code ClassCastException}.
|
||||
*/
|
||||
public TreeSet() {
|
||||
this(new TreeMap<E,Object>());
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new, empty tree set, sorted according to the specified
|
||||
* comparator. All elements inserted into the set must be <i>mutually
|
||||
* comparable</i> by the specified comparator: {@code comparator.compare(e1,
|
||||
* e2)} must not throw a {@code ClassCastException} for any elements
|
||||
* {@code e1} and {@code e2} in the set. If the user attempts to add
|
||||
* an element to the set that violates this constraint, the
|
||||
* {@code add} call will throw a {@code ClassCastException}.
|
||||
*
|
||||
* @param comparator the comparator that will be used to order this set.
|
||||
* If {@code null}, the {@linkplain Comparable natural
|
||||
* ordering} of the elements will be used.
|
||||
*/
|
||||
public TreeSet(Comparator<? super E> comparator) {
|
||||
this(new TreeMap<>(comparator));
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new tree set containing the elements in the specified
|
||||
* collection, sorted according to the <i>natural ordering</i> of its
|
||||
* elements. All elements inserted into the set must implement the
|
||||
* {@link Comparable} interface. Furthermore, all such elements must be
|
||||
* <i>mutually comparable</i>: {@code e1.compareTo(e2)} must not throw a
|
||||
* {@code ClassCastException} for any elements {@code e1} and
|
||||
* {@code e2} in the set.
|
||||
*
|
||||
* @param c collection whose elements will comprise the new set
|
||||
* @throws ClassCastException if the elements in {@code c} are
|
||||
* not {@link Comparable}, or are not mutually comparable
|
||||
* @throws NullPointerException if the specified collection is null
|
||||
*/
|
||||
public TreeSet(Collection<? extends E> c) {
|
||||
this();
|
||||
addAll(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a new tree set containing the same elements and
|
||||
* using the same ordering as the specified sorted set.
|
||||
*
|
||||
* @param s sorted set whose elements will comprise the new set
|
||||
* @throws NullPointerException if the specified sorted set is null
|
||||
*/
|
||||
public TreeSet(SortedSet<E> s) {
|
||||
this(s.comparator());
|
||||
addAll(s);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over the elements in this set in ascending order.
|
||||
*
|
||||
* @return an iterator over the elements in this set in ascending order
|
||||
*/
|
||||
public Iterator<E> iterator() {
|
||||
return m.navigableKeySet().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an iterator over the elements in this set in descending order.
|
||||
*
|
||||
* @return an iterator over the elements in this set in descending order
|
||||
* @since 1.6
|
||||
*/
|
||||
public Iterator<E> descendingIterator() {
|
||||
return m.descendingKeySet().iterator();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.6
|
||||
*/
|
||||
public NavigableSet<E> descendingSet() {
|
||||
return new TreeSet<>(m.descendingMap());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of elements in this set (its cardinality).
|
||||
*
|
||||
* @return the number of elements in this set (its cardinality)
|
||||
*/
|
||||
public int size() {
|
||||
return m.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if this set contains no elements.
|
||||
*
|
||||
* @return {@code true} if this set contains no elements
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return m.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if this set contains the specified element.
|
||||
* More formally, returns {@code true} if and only if this set
|
||||
* contains an element {@code e} such that
|
||||
* <tt>(o==null ? e==null : o.equals(e))</tt>.
|
||||
*
|
||||
* @param o object to be checked for containment in this set
|
||||
* @return {@code true} if this set contains the specified element
|
||||
* @throws ClassCastException if the specified object cannot be compared
|
||||
* with the elements currently in the set
|
||||
* @throws NullPointerException if the specified element is null
|
||||
* and this set uses natural ordering, or its comparator
|
||||
* does not permit null elements
|
||||
*/
|
||||
public boolean contains(Object o) {
|
||||
return m.containsKey(o);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified element to this set if it is not already present.
|
||||
* More formally, adds the specified element {@code e} to this set if
|
||||
* the set contains no element {@code e2} such that
|
||||
* <tt>(e==null ? e2==null : e.equals(e2))</tt>.
|
||||
* If this set already contains the element, the call leaves the set
|
||||
* unchanged and returns {@code false}.
|
||||
*
|
||||
* @param e element to be added to this set
|
||||
* @return {@code true} if this set did not already contain the specified
|
||||
* element
|
||||
* @throws ClassCastException if the specified object cannot be compared
|
||||
* with the elements currently in this set
|
||||
* @throws NullPointerException if the specified element is null
|
||||
* and this set uses natural ordering, or its comparator
|
||||
* does not permit null elements
|
||||
*/
|
||||
public boolean add(E e) {
|
||||
return m.put(e, PRESENT)==null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes the specified element from this set if it is present.
|
||||
* More formally, removes an element {@code e} such that
|
||||
* <tt>(o==null ? e==null : o.equals(e))</tt>,
|
||||
* if this set contains such an element. Returns {@code true} if
|
||||
* this set contained the element (or equivalently, if this set
|
||||
* changed as a result of the call). (This set will not contain the
|
||||
* element once the call returns.)
|
||||
*
|
||||
* @param o object to be removed from this set, if present
|
||||
* @return {@code true} if this set contained the specified element
|
||||
* @throws ClassCastException if the specified object cannot be compared
|
||||
* with the elements currently in this set
|
||||
* @throws NullPointerException if the specified element is null
|
||||
* and this set uses natural ordering, or its comparator
|
||||
* does not permit null elements
|
||||
*/
|
||||
public boolean remove(Object o) {
|
||||
return m.remove(o)==PRESENT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all of the elements from this set.
|
||||
* The set will be empty after this call returns.
|
||||
*/
|
||||
public void clear() {
|
||||
m.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 TreeMap) {
|
||||
SortedSet<? extends E> set = (SortedSet<? extends E>) c;
|
||||
TreeMap<E,Object> map = (TreeMap<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);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ClassCastException {@inheritDoc}
|
||||
* @throws NullPointerException if {@code fromElement} or {@code toElement}
|
||||
* is null and this set uses natural ordering, or its comparator
|
||||
* does not permit null elements
|
||||
* @throws IllegalArgumentException {@inheritDoc}
|
||||
* @since 1.6
|
||||
*/
|
||||
public NavigableSet<E> subSet(E fromElement, boolean fromInclusive,
|
||||
E toElement, boolean toInclusive) {
|
||||
return new TreeSet<>(m.subMap(fromElement, fromInclusive,
|
||||
toElement, toInclusive));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ClassCastException {@inheritDoc}
|
||||
* @throws NullPointerException if {@code toElement} is null and
|
||||
* this set uses natural ordering, or its comparator does
|
||||
* not permit null elements
|
||||
* @throws IllegalArgumentException {@inheritDoc}
|
||||
* @since 1.6
|
||||
*/
|
||||
public NavigableSet<E> headSet(E toElement, boolean inclusive) {
|
||||
return new TreeSet<>(m.headMap(toElement, inclusive));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ClassCastException {@inheritDoc}
|
||||
* @throws NullPointerException if {@code fromElement} is null and
|
||||
* this set uses natural ordering, or its comparator does
|
||||
* not permit null elements
|
||||
* @throws IllegalArgumentException {@inheritDoc}
|
||||
* @since 1.6
|
||||
*/
|
||||
public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
|
||||
return new TreeSet<>(m.tailMap(fromElement, inclusive));
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ClassCastException {@inheritDoc}
|
||||
* @throws NullPointerException if {@code fromElement} or
|
||||
* {@code toElement} is null and this set uses natural ordering,
|
||||
* or its comparator does not permit null elements
|
||||
* @throws IllegalArgumentException {@inheritDoc}
|
||||
*/
|
||||
public SortedSet<E> subSet(E fromElement, E toElement) {
|
||||
return subSet(fromElement, true, toElement, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ClassCastException {@inheritDoc}
|
||||
* @throws NullPointerException if {@code toElement} is null
|
||||
* and this set uses natural ordering, or its comparator does
|
||||
* not permit null elements
|
||||
* @throws IllegalArgumentException {@inheritDoc}
|
||||
*/
|
||||
public SortedSet<E> headSet(E toElement) {
|
||||
return headSet(toElement, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ClassCastException {@inheritDoc}
|
||||
* @throws NullPointerException if {@code fromElement} is null
|
||||
* and this set uses natural ordering, or its comparator does
|
||||
* not permit null elements
|
||||
* @throws IllegalArgumentException {@inheritDoc}
|
||||
*/
|
||||
public SortedSet<E> tailSet(E fromElement) {
|
||||
return tailSet(fromElement, true);
|
||||
}
|
||||
|
||||
public Comparator<? super E> comparator() {
|
||||
return m.comparator();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NoSuchElementException {@inheritDoc}
|
||||
*/
|
||||
public E first() {
|
||||
return m.firstKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws NoSuchElementException {@inheritDoc}
|
||||
*/
|
||||
public E last() {
|
||||
return m.lastKey();
|
||||
}
|
||||
|
||||
// NavigableSet API methods
|
||||
|
||||
/**
|
||||
* @throws ClassCastException {@inheritDoc}
|
||||
* @throws NullPointerException if the specified element is null
|
||||
* and this set uses natural ordering, or its comparator
|
||||
* does not permit null elements
|
||||
* @since 1.6
|
||||
*/
|
||||
public E lower(E e) {
|
||||
return m.lowerKey(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ClassCastException {@inheritDoc}
|
||||
* @throws NullPointerException if the specified element is null
|
||||
* and this set uses natural ordering, or its comparator
|
||||
* does not permit null elements
|
||||
* @since 1.6
|
||||
*/
|
||||
public E floor(E e) {
|
||||
return m.floorKey(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ClassCastException {@inheritDoc}
|
||||
* @throws NullPointerException if the specified element is null
|
||||
* and this set uses natural ordering, or its comparator
|
||||
* does not permit null elements
|
||||
* @since 1.6
|
||||
*/
|
||||
public E ceiling(E e) {
|
||||
return m.ceilingKey(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws ClassCastException {@inheritDoc}
|
||||
* @throws NullPointerException if the specified element is null
|
||||
* and this set uses natural ordering, or its comparator
|
||||
* does not permit null elements
|
||||
* @since 1.6
|
||||
*/
|
||||
public E higher(E e) {
|
||||
return m.higherKey(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.6
|
||||
*/
|
||||
public E pollFirst() {
|
||||
Map.Entry<E,?> e = m.pollFirstEntry();
|
||||
return (e == null) ? null : e.getKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 1.6
|
||||
*/
|
||||
public E pollLast() {
|
||||
Map.Entry<E,?> e = m.pollLastEntry();
|
||||
return (e == null) ? null : e.getKey();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a shallow copy of this {@code TreeSet} instance. (The elements
|
||||
* themselves are not cloned.)
|
||||
*
|
||||
* @return a shallow copy of this set
|
||||
*/
|
||||
public Object clone() {
|
||||
TreeSet<E> clone = null;
|
||||
try {
|
||||
clone = (TreeSet<E>) super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new InternalError();
|
||||
}
|
||||
|
||||
clone.m = new TreeMap<>(m);
|
||||
return clone;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save the state of the {@code TreeSet} instance to a stream (that is,
|
||||
* serialize it).
|
||||
*
|
||||
* @serialData Emits the comparator used to order this set, or
|
||||
* {@code null} if it obeys its elements' natural ordering
|
||||
* (Object), followed by the size of the set (the number of
|
||||
* elements it contains) (int), followed by all of its
|
||||
* elements (each an Object) in order (as determined by the
|
||||
* set's Comparator, or by the elements' natural ordering if
|
||||
* the set has no Comparator).
|
||||
*/
|
||||
private void writeObject(java.io.ObjectOutputStream s)
|
||||
throws java.io.IOException {
|
||||
// Write out any hidden stuff
|
||||
s.defaultWriteObject();
|
||||
|
||||
// Write out Comparator
|
||||
s.writeObject(m.comparator());
|
||||
|
||||
// Write out size
|
||||
s.writeInt(m.size());
|
||||
|
||||
// Write out all elements in the proper order.
|
||||
for (E e : m.keySet())
|
||||
s.writeObject(e);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reconstitute the {@code TreeSet} instance from a stream (that is,
|
||||
* deserialize it).
|
||||
*/
|
||||
private void readObject(java.io.ObjectInputStream s)
|
||||
throws java.io.IOException, ClassNotFoundException {
|
||||
// Read in any hidden stuff
|
||||
s.defaultReadObject();
|
||||
|
||||
// Read in Comparator
|
||||
Comparator<? super E> c = (Comparator<? super E>) s.readObject();
|
||||
|
||||
// Create backing TreeMap
|
||||
TreeMap<E,Object> tm;
|
||||
if (c==null)
|
||||
tm = new TreeMap<>();
|
||||
else
|
||||
tm = new TreeMap<>(c);
|
||||
m = tm;
|
||||
|
||||
// Read in size
|
||||
int size = s.readInt();
|
||||
|
||||
tm.readTreeSet(size, s, PRESENT);
|
||||
}
|
||||
|
||||
private static final long serialVersionUID = -2479143000061671589L;
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user