1.3.8 TableStack<K,V>
static final class TableStack < K, V> {
int length;
int index;
Node< K, V> [ ] tab;
TableStack< K, V> next;
}
1.3.9 Traverser<K,V>
主要用于遍历操作,有BaseIterator、KeySpliterator、ValueSpliterator、EntrySpliterator四个子类 BaseIterator 用于遍历操作。KeySplitertor、ValueSpliterator、EntrySpliterator则用于键、值、键值对的划分。
static class Traverser < K, V> {
Node< K, V> [ ] tab;
Node< K, V> next;
TableStack< K, V> stack, spare;
int index;
int baseIndex;
int baseLimit;
final int baseSize;
Traverser ( Node< K, V> [ ] tab, int size, int index, int limit) {
this . tab = tab;
this . baseSize = size;
this . baseIndex = this . index = index;
this . baseLimit = limit;
this . next = null;
}
final Node< K, V> advance ( ) {
Node< K, V> e;
if ( ( e = next) != null)
e = e. next;
for ( ; ; ) {
Node< K, V> [ ] t; int i, n;
if ( e != null)
return next = e;
if ( baseIndex >= baseLimit || ( t = tab) == null ||
( n = t. length) <= ( i = index) || i < 0 )
return next = null;
if ( ( e = tabAt ( t, i) ) != null && e. hash < 0 ) {
if ( e instanceof ForwardingNode ) {
tab = ( ( ForwardingNode< K, V> ) e) . nextTable;
e = null;
pushState ( t, i, n) ;
continue ;
}
else if ( e instanceof TreeBin )
e = ( ( TreeBin< K, V> ) e) . first;
else
e = null;
}
if ( stack != null)
recoverState ( n) ;
else if ( ( index = i + baseSize) >= n)
index = ++ baseIndex;
}
}
private void pushState ( Node< K, V> [ ] t, int i, int n) {
TableStack< K, V> s = spare;
if ( s != null)
spare = s. next;
else
s = new TableStack < K, V> ( ) ;
s. tab = t;
s. length = n;
s. index = i;
s. next = stack;
stack = s;
}
private void recoverState ( int n) {
TableStack< K, V> s; int len;
while ( ( s = stack) != null && ( index += ( len = s. length) ) >= n) {
n = len;
index = s. index;
tab = s. tab;
s. tab = null;
TableStack< K, V> next = s. next;
s. next = spare;
stack = next;
spare = s;
}
if ( s == null && ( index += baseSize) >= n)
index = ++ baseIndex;
}
}
1.3.10 BaseIterator
继承 Traverser,其子类有 KeyIterator、ValueIterator、EntryIterator。
static class BaseIterator < K, V> extends Traverser < K, V> {
finalConcurrentHashMap< K, V> map;
Node< K, V> lastReturned;
BaseIterator ( Node< K, V> [ ] tab, int size, int index, int limit,
ConcurrentHashMap< K, V> map) {
super ( tab, size, index, limit) ;
this . map = map;
advance ( ) ;
}
public final boolean hasNext ( ) { return next != null; }
public final boolean hasMoreElements ( ) { return next != null; }
public final void remove ( ) {
Node< K, V> p;
if ( ( p = lastReturned) == null)
throw new IllegalStateException ( ) ;
lastReturned = null;
map. replaceNode ( p. key, null, null) ;
}
}
1.3.11 KeyIterator
ket 值迭代器,继承 BaseIterator,实现 Enumeration
static final class KeyIterator < K, V> extends BaseIterator < K, V>
implements Iterator < K> , Enumeration< K> {
KeyIterator ( Node< K, V> [ ] tab, int index, int size, int limit,
ConcurrentHashMap< K, V> map) {
super ( tab, index, size, limit, map) ;
}
public final K next ( ) {
Node< K, V> p;
if ( ( p = next) == null)
throw new NoSuchElementException ( ) ;
K k = p. key;
lastReturned = p;
advance ( ) ;
return k;
}
public final K nextElement ( ) { return next ( ) ; }
}
1.3.12 ValueIterator<K,V>
value 值迭代器,继承 BaseIterator,实现 Enumeration
static final class ValueIterator < K, V> extends BaseIterator < K, V>
implements Iterator < V> , Enumeration< V> {
ValueIterator ( Node< K, V> [ ] tab, int index, int size, int limit,
ConcurrentHashMap< K, V> map) {
super ( tab, index, size, limit, map) ;
}
public final V next ( ) {
Node< K, V> p;
if ( ( p = next) == null)
throw new NoSuchElementException ( ) ;
V v = p. val;
lastReturned = p;
advance ( ) ;
return v;
}
public final V nextElement ( ) { return next ( ) ; }
}
1.3.13 EntryIterator<K,V>
static final class EntryIterator < K, V> extends BaseIterator < K, V>
implements Iterator < Map. Entry< K, V> > {
EntryIterator ( Node< K, V> [ ] tab, int index, int size, int limit,
ConcurrentHashMap< K, V> map) {
super ( tab, index, size, limit, map) ;
}
public final Map. Entry< K, V> next ( ) {
Node< K, V> p;
if ( ( p = next) == null)
throw new NoSuchElementException ( ) ;
K k = p. key;
V v = p. val;
lastReturned = p;
advance ( ) ;
return new MapEntry < K, V> ( k, v, map) ;
}
}
1.3.14 MapEntry
该类为 EntryIterator 服务,导出 entry 节点,但不能被外部修改
static final class MapEntry < K, V> implements Map. Entry < K, V> {
final K key;
V val;
final ConcurrentHashMap< K, V> map;
MapEntry ( K key, V val, ConcurrentHashMap< K, V> map) {
this . key = key;
this . val = val;
this . map = map;
}
public K getKey ( ) { return key; }
public V getValue ( ) { return val; }
public int hashCode ( ) { return key. hashCode ( ) ^ val. hashCode ( ) ; }
public String toString ( ) { return key + "=" + val; }
public boolean equals ( Object o) {
Object k, v; Map. Entry< ? , ? > e;
return ( ( o instanceof Map. Entry ) &&
( k = ( e = ( Map. Entry< ? , ? > ) o) . getKey ( ) ) != null &&
( v = e. getValue ( ) ) != null &&
( k == key || k. equals ( key) ) &&
( v == val || v. equals ( val) ) ) ;
}
public V setValue ( V value) {
if ( value == null) throw new NullPointerException ( ) ;
V v = val;
val = value;
map. put ( key, value) ;
return v;
}
}
1.3.15 KeySpliterator
static final class KeySpliterator < K, V> extends Traverser < K, V>
implements Spliterator < K> {
long est;
KeySpliterator ( Node< K, V> [ ] tab, int size, int index, int limit,
long est) {
super ( tab, size, index, limit) ;
this . est = est;
}
public Spliterator< K> trySplit ( ) {
int i, f, h;
return ( h = ( ( i = baseIndex) + ( f = baseLimit) ) >>> 1 ) <= i ? null :
new KeySpliterator < K, V> ( tab, baseSize, baseLimit = h,
f, est >>>= 1 ) ;
}
public void forEachRemaining ( Consumer< ? super K> action) {
if ( action == null) throw new NullPointerException ( ) ;
for ( Node< K, V> p; ( p = advance ( ) ) != null; )
action. accept ( p. key) ;
}
public boolean tryAdvance ( Consumer< ? super K> action) {
if ( action == null) throw new NullPointerException ( ) ;
Node< K, V> p;
if ( ( p = advance ( ) ) == null)
return false ;
action. accept ( p. key) ;
return true ;
}
public long estimateSize ( ) { return est; }
public int characteristics ( ) {
return Spliterator. DISTINCT | Spliterator. CONCURRENT |
Spliterator. NONNULL;
}
}
1.3.16 ValueSpliterator
value 值的分解迭代器,继承 Traverser
static final class ValueSpliterator < K, V> extends Traverser < K, V>
implements Spliterator < V> {
long est;
ValueSpliterator ( Node< K, V> [ ] tab, int size, int index, int limit,
long est) {
super ( tab, size, index, limit) ;
this . est = est;
}
public Spliterator< V> trySplit ( ) {
int i, f, h;
return ( h = ( ( i = baseIndex) + ( f = baseLimit) ) >>> 1 ) <= i ? null :
new ValueSpliterator < K, V> ( tab, baseSize, baseLimit = h,
f, est >>>= 1 ) ;
}
public void forEachRemaining ( Consumer< ? super V> action) {
if ( action == null) throw new NullPointerException ( ) ;
for ( Node< K, V> p; ( p = advance ( ) ) != null; )
action. accept ( p. val) ;
}
public boolean tryAdvance ( Consumer< ? super V> action) {
if ( action == null) throw new NullPointerException ( ) ;
Node< K, V> p;
if ( ( p = advance ( ) ) == null)
return false ;
action. accept ( p. val) ;
return true ;
}
public long estimateSize ( ) { return est; }
public int characteristics ( ) {
return Spliterator. CONCURRENT | Spliterator. NONNULL;
}
}
1.3.17 EntrySpliterator
entry 的分解迭代器,继承 Traverser
static final class EntrySpliterator < K, V> extends Traverser < K, V>
implements Spliterator < Map. Entry< K, V> > {
final ConcurrentHashMap< K, V> map;
long est;
EntrySpliterator ( Node< K, V> [ ] tab, int size, int index, int limit,
long est, ConcurrentHashMap< K, V> map) {
super ( tab, size, index, limit) ;
this . map = map;
this . est = est;
}
public Spliterator< Map. Entry< K, V> > trySplit ( ) {
int i, f, h;
return ( h = ( ( i = baseIndex) + ( f = baseLimit) ) >>> 1 ) <= i ? null :
new EntrySpliterator < K, V> ( tab, baseSize, baseLimit = h,
f, est >>>= 1 , map) ;
}
public void forEachRemaining ( Consumer< ? super Map. Entry< K, V> > action) {
if ( action == null) throw new NullPointerException ( ) ;
for ( Node< K, V> p; ( p = advance ( ) ) != null; )
action. accept ( new MapEntry < K, V> ( p. key, p. val, map) ) ;
}
public boolean tryAdvance ( Consumer< ? super Map. Entry< K, V> > action) {
if ( action == null) throw new NullPointerException ( ) ;
Node< K, V> p;
if ( ( p = advance ( ) ) == null)
return false ;
action. accept ( new MapEntry < K, V> ( p. key, p. val, map) ) ;
return true ;
}
public long estimateSize ( ) { return est; }
public int characteristics ( ) {
return Spliterator. DISTINCT | Spliterator. CONCURRENT |
Spliterator. NONNULL;
}
}
1.3.18 CollectionView
主要定义了视图操作,其子类KeySetView、ValueSetView、EntrySetView分别表示键视图、值视图、键值对视图
abstract static class CollectionView < K, V, E>
implements Collection < E> , java. io. Serializable {
private static final long serialVersionUID = 7249069246763182397 L;
final ConcurrentHashMap< K, V> map;
CollectionView ( ConcurrentHashMap< K, V> map) { this . map = map; }
public ConcurrentHashMap< K, V> getMap ( ) { return map; }
public final void clear ( ) { map. clear ( ) ; }
public final int size ( ) { return map. size ( ) ; }
public final boolean isEmpty ( ) { return map. isEmpty ( ) ; }
public abstract Iterator< E> iterator ( ) ;
public abstract boolean contains ( Object o) ;
public abstract boolean remove ( Object o) ;
private static final String oomeMsg = "Required array size too large" ;
public final Object[ ] toArray ( ) {
long sz = map. mappingCount ( ) ;
if ( sz > MAX_ARRAY_SIZE)
throw new OutOfMemoryError ( oomeMsg) ;
int n = ( int ) sz;
Object[ ] r = new Object [ n] ;
int i = 0 ;
for ( E e : this ) {
if ( i == n) {
if ( n >= MAX_ARRAY_SIZE)
throw new OutOfMemoryError ( oomeMsg) ;
if ( n >= MAX_ARRAY_SIZE - ( MAX_ARRAY_SIZE >>> 1 ) - 1 )
n = MAX_ARRAY_SIZE;
else
n += ( n >>> 1 ) + 1 ;
r = Arrays. copyOf ( r, n) ;
}
r[ i++ ] = e;
}
return ( i == n) ? r : Arrays. copyOf ( r, i) ;
}
@SuppressWarnings ( "unchecked" )
public final < T> T[ ] toArray ( T[ ] a) {
long sz = map. mappingCount ( ) ;
if ( sz > MAX_ARRAY_SIZE)
throw new OutOfMemoryError ( oomeMsg) ;
int m = ( int ) sz;
T[ ] r = ( a. length >= m) ? a :
( T[ ] ) java. lang. reflect. Array
. newInstance ( a. getClass ( ) . getComponentType ( ) , m) ;
int n = r. length;
int i = 0 ;
for ( E e : this ) {
if ( i == n) {
if ( n >= MAX_ARRAY_SIZE)
throw new OutOfMemoryError ( oomeMsg) ;
if ( n >= MAX_ARRAY_SIZE - ( MAX_ARRAY_SIZE >>> 1 ) - 1 )
n = MAX_ARRAY_SIZE;
else
n += ( n >>> 1 ) + 1 ;
r = Arrays. copyOf ( r, n) ;
}
r[ i++ ] = ( T) e;
}
if ( a == r && i < n) {
r[ i] = null;
return r;
}
return ( i == n) ? r : Arrays. copyOf ( r, i) ;
}
public final String toString ( ) {
StringBuilder sb = new StringBuilder ( ) ;
sb. append ( '[' ) ;
Iterator< E> it = iterator ( ) ;
if ( it. hasNext ( ) ) {
for ( ; ; ) {
Object e = it. next ( ) ;
sb. append ( e == this ? "(this Collection)" : e) ;
if ( ! it. hasNext ( ) )
break ;
sb. append ( ',' ) . append ( ' ' ) ;
}
}
return sb. append ( ']' ) . toString ( ) ;
}
public final boolean containsAll ( Collection< ? > c) {
if ( c != this ) {
for ( Object e : c) {
if ( e == null || ! contains ( e) )
return false ;
}
}
return true ;
}
public final boolean removeAll ( Collection< ? > c) {
if ( c == null) throw new NullPointerException ( ) ;
boolean modified = false ;
for ( Iterator< E> it = iterator ( ) ; it. hasNext ( ) ; ) {
if ( c. contains ( it. next ( ) ) ) {
it. remove ( ) ;
modified = true ;
}
}
return modified;
}
public final boolean retainAll ( Collection< ? > c) {
if ( c == null) throw new NullPointerException ( ) ;
boolean modified = false ;
for ( Iterator< E> it = iterator ( ) ; it. hasNext ( ) ; ) {
if ( ! c. contains ( it. next ( ) ) ) {
it. remove ( ) ;
modified = true ;
}
}
return modified;
}
}
1.3.19 KeySetView
key 集合视图,继承 CollectionView
public static class KeySetView < K, V> extends CollectionView < K, V, K>
implements Set < K> , java. io. Serializable {
private static final long serialVersionUID = 7249069246763182397 L;
private final V value;
KeySetView ( ConcurrentHashMap< K, V> map, V value) {
super ( map) ;
this . value = value;
}
public V getMappedValue ( ) { return value; }
public boolean contains ( Object o) { return map. containsKey ( o) ; }
public boolean remove ( Object o) { return map. remove ( o) != null; }
public Iterator< K> iterator ( ) {
Node< K, V> [ ] t;
ConcurrentHashMap< K, V> m = map;
int f = ( t = m. table) == null ? 0 : t. length;
return new KeyIterator < K, V> ( t, f, 0 , f, m) ;
}
public boolean add ( K e) {
V v;
if ( ( v = value) == null)
throw new UnsupportedOperationException ( ) ;
return map. putVal ( e, v, true ) == null;
}
public boolean addAll ( Collection< ? extends K > c) {
boolean added = false ;
V v;
if ( ( v = value) == null)
throw new UnsupportedOperationException ( ) ;
for ( K e : c) {
if ( map. putVal ( e, v, true ) == null)
added = true ;
}
return added;
}
public int hashCode ( ) {
int h = 0 ;
for ( K e : this )
h += e. hashCode ( ) ;
return h;
}
public boolean equals ( Object o) {
Set< ? > c;
return ( ( o instanceof Set ) &&
( ( c = ( Set< ? > ) o) == this ||
( containsAll ( c) && c. containsAll ( this ) ) ) ) ;
}
public Spliterator< K> spliterator ( ) {
Node< K, V> [ ] t;
ConcurrentHashMap< K, V> m = map;
long n = m. sumCount ( ) ;
int f = ( t = m. table) == null ? 0 : t. length;
return new KeySpliterator < K, V> ( t, f, 0 , f, n < 0 L ? 0 L : n) ;
}
public void forEach ( Consumer< ? super K> action) {
if ( action == null) throw new NullPointerException ( ) ;
Node< K, V> [ ] t;
if ( ( t = map. table) != null) {
Traverser< K, V> it = new Traverser < K, V> ( t, t. length, 0 , t. length) ;
for ( Node< K, V> p; ( p = it. advance ( ) ) != null; )
action. accept ( p. key) ;
}
}
}
1.3.20 ValuesView
value 值的视图,继承 CollectionView
static final class ValuesView < K, V> extends CollectionView < K, V, V>
implements Collection < V> , java. io. Serializable {
private static final long serialVersionUID = 2249069246763182397 L;
ValuesView ( ConcurrentHashMap< K, V> map) { super ( map) ; }
public final boolean contains ( Object o) {
return map. containsValue ( o) ;
}
public final boolean remove ( Object o) {
if ( o != null) {
for ( Iterator< V> it = iterator ( ) ; it. hasNext ( ) ; ) {
if ( o. equals ( it. next ( ) ) ) {
it. remove ( ) ;
return true ;
}
}
}
return false ;
}
public final Iterator< V> iterator ( ) {
ConcurrentHashMap< K, V> m = map;
Node< K, V> [ ] t;
int f = ( t = m. table) == null ? 0 : t. length;
return new ValueIterator < K, V> ( t, f, 0 , f, m) ;
}
public final boolean add ( V e) {
throw new UnsupportedOperationException ( ) ;
}
public final boolean addAll ( Collection< ? extends V > c) {
throw new UnsupportedOperationException ( ) ;
}
public Spliterator< V> spliterator ( ) {
Node< K, V> [ ] t;
ConcurrentHashMap< K, V> m = map;
long n = m. sumCount ( ) ;
int f = ( t = m. table) == null ? 0 : t. length;
return new ValueSpliterator < K, V> ( t, f, 0 , f, n < 0 L ? 0 L : n) ;
}
public void forEach ( Consumer< ? super V> action) {
if ( action == null) throw new NullPointerException ( ) ;
Node< K, V> [ ] t;
if ( ( t = map. table) != null) {
Traverser< K, V> it = new Traverser < K, V> ( t, t. length, 0 , t. length) ;
for ( Node< K, V> p; ( p = it. advance ( ) ) != null; )
action. accept ( p. val) ;
}
}
}
1.3.21 EntrySetView
static final class EntrySetView < K, V> extends CollectionView < K, V, Map. Entry< K, V> >
implements Set < Map. Entry< K, V> > , java. io. Serializable {
private static final long serialVersionUID = 2249069246763182397 L;
EntrySetView ( ConcurrentHashMap< K, V> map) { super ( map) ; }
public boolean contains ( Object o) {
Object k, v, r; Map. Entry< ? , ? > e;
return ( ( o instanceof Map. Entry ) &&
( k = ( e = ( Map. Entry< ? , ? > ) o) . getKey ( ) ) != null &&
( r = map. get ( k) ) != null &&
( v = e. getValue ( ) ) != null &&
( v == r || v. equals ( r) ) ) ;
}
public boolean remove ( Object o) {
Object k, v; Map. Entry< ? , ? > e;
return ( ( o instanceof Map. Entry ) &&
( k = ( e = ( Map. Entry< ? , ? > ) o) . getKey ( ) ) != null &&
( v = e. getValue ( ) ) != null &&
map. remove ( k, v) ) ;
}
public Iterator< Map. Entry< K, V> > iterator ( ) {
ConcurrentHashMap< K, V> m = map;
Node< K, V> [ ] t;
int f = ( t = m. table) == null ? 0 : t. length;
return new EntryIterator < K, V> ( t, f, 0 , f, m) ;
}
public boolean add ( Entry< K, V> e) {
return map. putVal ( e. getKey ( ) , e. getValue ( ) , false ) == null;
}
public boolean addAll ( Collection< ? extends Entry < K, V> > c) {
boolean added = false ;
for ( Entry< K, V> e : c) {
if ( add ( e) )
added = true ;
}
return added;
}
public final int hashCode ( ) {
int h = 0 ;
Node< K, V> [ ] t;
if ( ( t = map. table) != null) {
Traverser< K, V> it = new Traverser < K, V> ( t, t. length, 0 , t. length) ;
for ( Node< K, V> p; ( p = it. advance ( ) ) != null; ) {
h += p. hashCode ( ) ;
}
}
return h;
}
public final boolean equals ( Object o) {
Set< ? > c;
return ( ( o instanceof Set ) &&
( ( c = ( Set< ? > ) o) == this ||
( containsAll ( c) && c. containsAll ( this ) ) ) ) ;
}
public Spliterator< Map. Entry< K, V> > spliterator ( ) {
Node< K, V> [ ] t;
ConcurrentHashMap< K, V> m = map;
long n = m. sumCount ( ) ;
int f = ( t = m. table) == null ? 0 : t. length;
return new EntrySpliterator < K, V> ( t, f, 0 , f, n < 0 L ? 0 L : n, m) ;
}
public void forEach ( Consumer< ? super Map. Entry< K, V> > action) {
if ( action == null) throw new NullPointerException ( ) ;
Node< K, V> [ ] t;
if ( ( t = map. table) != null) {
Traverser< K, V> it = new Traverser < K, V> ( t, t. length, 0 , t. length) ;
for ( Node< K, V> p; ( p = it. advance ( ) ) != null; )
action. accept ( new MapEntry < K, V> ( p. key, p. val, map) ) ;
}
}
}