APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.sun.beans.util

public abstract Class Cache<K, V>

extends Object
Class Inheritance
Imports
java.lang.ref.ReferenceQueue, .SoftReference, .WeakReference, java.util.Objects

Hash table based implementation of the cache, which allows to use weak or soft references for keys and values. An entry in a Cache will automatically be removed when its key or value is no longer in ordinary use.
Author
Sergey Malenkov
Since
1.8

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class
Cache.CacheEntry<K, V>

Represents a cache entry (key-value pair).

public static enum
Cache.Kind

Represents a reference kind.

private static interface
Cache.Ref<
the type of object to refer
T
>

Basic interface for references.

Field Summary

Modifier and TypeField and Description
private final boolean
private final Cache.Kind
private static final int
private final ReferenceQueue<Object>
private int
private volatile Cache<K, V>.CacheEntry<K, V>[]
private int
private final Cache.Kind

Constructor Summary

AccessConstructor and Description
public
Cache(Cache.Kind
a reference kind for keys
keyKind
,
Cache.Kind
a reference kind for values
valueKind
)

Constructs an empty Cache.

public
Cache(Cache.Kind
a reference kind for keys
keyKind
,
Cache.Kind
a reference kind for values
valueKind
,
boolean
defines whether reference-equality is used in place of object-equality
identity
)

Constructs an empty Cache with the specified comparison method.

Method Summary

Modifier and TypeMethod and Description
public final void
clear()

Removes all of the mappings from this cache.

public abstract V

Returns:

a corresponding value for the specified key
create
(K
a key that can be used to create a value
key
)

Creates a corresponding value for the specified key.

public final V

Returns:

a value to which the specified key is mapped, or null if there is no mapping for key
get
(K
the key whose cached value is to be returned
key
)

Returns the value to which the specified key is mapped, or null if there is no mapping for the key.

private V
getEntryValue(K key, int hash, Cache<K, V>.CacheEntry<K, V> entry)

private int

Returns:

a hash code value for the specified object
hash
(Object
the object which hash code is to be calculated
key
)

Retrieves object hash code and applies a supplemental hash function to the result hash, which defends against poor quality hash functions.

private static int

Returns:

an index of the specified hash code in the given table
index
(int
the hash code
hash
,
Object[]
the table
table
)

Returns index of the specified hash code in the given table.

private Cache<K, V>.CacheEntry<K, V>[]

Returns:

a new array for the cache entries
newTable
(int
requested capacity MUST be a power of two
size
)

Creates a new array for the cache entries.

public final void
remove(K
the key whose mapping is to be removed from this cache
key
)

Removes the cached value that corresponds to the specified key.

private void
private void
transfer(Cache<K, V>.CacheEntry<K, V>[] oldTable, Cache<K, V>.CacheEntry<K, V>[] newTable)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

identityback to summary
private final boolean identity
keyKindback to summary
private final Cache.Kind keyKind
MAXIMUM_CAPACITYback to summary
private static final int MAXIMUM_CAPACITY
queueback to summary
private final ReferenceQueue<Object> queue
sizeback to summary
private int size
tableback to summary
private volatile Cache<K, V>.CacheEntry<K, V>[] table
thresholdback to summary
private int threshold
valueKindback to summary
private final Cache.Kind valueKind

Constructor Detail

Cacheback to summary
public Cache(Cache.Kind keyKind, Cache.Kind valueKind)

Constructs an empty Cache. The default initial capacity is 8. The default load factor is 0.75.

Parameters
keyKind:Cache.Kind

a reference kind for keys

valueKind:Cache.Kind

a reference kind for values

Exceptions
NullPointerException:
if keyKind or valueKind are null
Cacheback to summary
public Cache(Cache.Kind keyKind, Cache.Kind valueKind, boolean identity)

Constructs an empty Cache with the specified comparison method. The default initial capacity is 8. The default load factor is 0.75.

Parameters
keyKind:Cache.Kind

a reference kind for keys

valueKind:Cache.Kind

a reference kind for values

identity:boolean

defines whether reference-equality is used in place of object-equality

Exceptions
NullPointerException:
if keyKind or valueKind are null

Method Detail

clearback to summary
public final void clear()

Removes all of the mappings from this cache. It will be empty after this call returns.

createback to summary
public abstract V create(K key)

Creates a corresponding value for the specified key.

Parameters
key:K

a key that can be used to create a value

Returns:V

a corresponding value for the specified key

getback to summary
public final V get(K key)

Returns the value to which the specified key is mapped, or null if there is no mapping for the key.

Parameters
key:K

the key whose cached value is to be returned

Returns:V

a value to which the specified key is mapped, or null if there is no mapping for key

Exceptions
NullPointerException:
if key is null or corresponding value is null
getEntryValueback to summary
private V getEntryValue(K key, int hash, Cache<K, V>.CacheEntry<K, V> entry)
hashback to summary
private int hash(Object key)

Retrieves object hash code and applies a supplemental hash function to the result hash, which defends against poor quality hash functions. This is critical because Cache uses power-of-two length hash tables, that otherwise encounter collisions for hashCodes that do not differ in lower bits.

Parameters
key:Object

the object which hash code is to be calculated

Returns:int

a hash code value for the specified object

indexback to summary
private static int index(int hash, Object[] table)

Returns index of the specified hash code in the given table. Note that the table size must be a power of two.

Parameters
hash:int

the hash code

table:Object[]

the table

Returns:int

an index of the specified hash code in the given table

newTableback to summary
private Cache<K, V>.CacheEntry<K, V>[] newTable(int size)

Creates a new array for the cache entries.

Parameters
size:int

requested capacity MUST be a power of two

Returns:Cache<K, V>.CacheEntry<K, V>[]

a new array for the cache entries

Annotations
@SuppressWarnings:unchecked, rawtypes
removeback to summary
public final void remove(K key)

Removes the cached value that corresponds to the specified key.

Parameters
key:K

the key whose mapping is to be removed from this cache

removeStaleEntriesback to summary
private void removeStaleEntries()
transferback to summary
private void transfer(Cache<K, V>.CacheEntry<K, V>[] oldTable, Cache<K, V>.CacheEntry<K, V>[] newTable)
com.sun.beans.util back to summary

private final Class Cache.CacheEntry<K, V>

extends Object
Class Inheritance

Represents a cache entry (key-value pair).

Field Summary

Modifier and TypeField and Description
private final int
private final Cache.Ref<K>
private volatile Cache<K, V>.CacheEntry<K, V>
private final Cache.Ref<V>

Constructor Summary

AccessConstructor and Description
private
CacheEntry(int
the hash code calculated for the entry key
hash
,
K
the entry key
key
,
V
the initial value of the entry
value
,
Cache<K, V>.CacheEntry<K, V>
the next entry in a chain
next
)

Constructs an entry for the cache.

Method Summary

Modifier and TypeMethod and Description
private boolean

Returns:

true if the entry has the given key with the given hash code; false otherwise
matches
(int
an expected hash code
hash
,
Object
an object to be compared with the entry key
object
)

Determines whether the entry has the given key with the given hash code.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

hashback to summary
private final int hash
keyback to summary
private final Cache.Ref<K> key
nextback to summary
private volatile Cache<K, V>.CacheEntry<K, V> next
valueback to summary
private final Cache.Ref<V> value

Constructor Detail

CacheEntryback to summary
private CacheEntry(int hash, K key, V value, Cache<K, V>.CacheEntry<K, V> next)

Constructs an entry for the cache.

Parameters
hash:int

the hash code calculated for the entry key

key:K

the entry key

value:V

the initial value of the entry

next:Cache<K, V>.CacheEntry<K, V>

the next entry in a chain

Method Detail

matchesback to summary
private boolean matches(int hash, Object object)

Determines whether the entry has the given key with the given hash code.

Parameters
hash:int

an expected hash code

object:Object

an object to be compared with the entry key

Returns:boolean

true if the entry has the given key with the given hash code; false otherwise

com.sun.beans.util back to summary

public sealed Enum Cache.Kind

extends Enum<Cache.Kind>
Class Inheritance

Represents a reference kind.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
Cache.Kind.Soft<
the type of object to refer
T
>

This is an implementation of the Cache.Ref interface that uses the soft references that are cleared at the discretion of the garbage collector in response to a memory request.

private static class
Cache.Kind.Strong<
the type of object to refer
T
>

This is an implementation of the Cache.Ref interface that uses the strong references that prevent their referents from being made finalizable, finalized, and then reclaimed.

private static class
Cache.Kind.Weak<
the type of object to refer
T
>

This is an implementation of the Cache.Ref interface that uses the weak references that do not prevent their referents from being made finalizable, finalized, and then reclaimed.

Field Summary

Modifier and TypeField and Description
public static final Cache.Kind
public static final Cache.Kind
public static final Cache.Kind

Constructor Summary

AccessConstructor and Description
private
Kind()

Method Summary

Modifier and TypeMethod and Description
pack-priv abstract <
the type of object to refer
T
>
Cache.Ref<T>

Returns:

the reference to the specified object
create
(Object
the owner of the reference, if needed
owner
,
T
the object to refer
referent
,
ReferenceQueue<? super T>
the queue to register the reference with, or null if registration is not required
queue
)

Creates a reference to the specified object.

public static Cache.Kind
public static Cache.Kind[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

SOFTback to summary
public static final Cache.Kind SOFT
STRONGback to summary
public static final Cache.Kind STRONG
WEAKback to summary
public static final Cache.Kind WEAK

Constructor Detail

Kindback to summary
private Kind()

Method Detail

createback to summary
pack-priv abstract <T> Cache.Ref<T> create(Object owner, T referent, ReferenceQueue<? super T> queue)

Creates a reference to the specified object.

Parameters
<T>
the type of object to refer
owner:Object

the owner of the reference, if needed

referent:T

the object to refer

queue:ReferenceQueue<? super T>

the queue to register the reference with, or null if registration is not required

Returns:Cache.Ref<T>

the reference to the specified object

valueOfback to summary
public static Cache.Kind valueOf(String name)
valuesback to summary
public static Cache.Kind[] values()
com.sun.beans.util back to summary

private final Class Cache.Kind.Soft<T>

extends SoftReference<T>
implements Cache.Ref<T>
Class Inheritance
All Implemented Interfaces
com.sun.beans.util.Cache.Ref
Type Parameters
<T>
the type of object to refer

This is an implementation of the Cache.Ref interface that uses the soft references that are cleared at the discretion of the garbage collector in response to a memory request.
See Also
java.lang.ref.SoftReference

Field Summary

Modifier and TypeField and Description
private Object

Constructor Summary

AccessConstructor and Description
private
Soft(Object
the owner of the reference, if needed
owner
,
T
the non-null object to refer
referent
,
ReferenceQueue<? super T>
the queue to register the reference with, or null if registration is not required
queue
)

Creates a soft reference to the specified object.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

the owner of the reference or null if the owner is unknown
getOwner
()

Implements com.sun.beans.util.Cache.Ref.getOwner.

Returns the object that possesses information about the reference.

public T

Returns:

the referred object or null if it was collected
getReferent
()

Implements com.sun.beans.util.Cache.Ref.getReferent.

Returns the object to refer.

public boolean

Returns:

true if the referred object was collected
isStale
()

Implements com.sun.beans.util.Cache.Ref.isStale.

Determines whether the referred object was taken by the garbage collector or not.

public void
removeOwner()

Implements com.sun.beans.util.Cache.Ref.removeOwner.

Marks this reference as removed from the cache.

Inherited from java.lang.ref.SoftReference:
get

Field Detail

ownerback to summary
private Object owner

Constructor Detail

Softback to summary
private Soft(Object owner, T referent, ReferenceQueue<? super T> queue)

Creates a soft reference to the specified object.

Parameters
owner:Object

the owner of the reference, if needed

referent:T

the non-null object to refer

queue:ReferenceQueue<? super T>

the queue to register the reference with, or null if registration is not required

Method Detail

getOwnerback to summary
public Object getOwner()

Implements com.sun.beans.util.Cache.Ref.getOwner.

Returns the object that possesses information about the reference.

Returns:Object

the owner of the reference or null if the owner is unknown

Annotations
@Override
getReferentback to summary
public T getReferent()

Implements com.sun.beans.util.Cache.Ref.getReferent.

Returns the object to refer.

Returns:T

the referred object or null if it was collected

Annotations
@Override
isStaleback to summary
public boolean isStale()

Implements com.sun.beans.util.Cache.Ref.isStale.

Determines whether the referred object was taken by the garbage collector or not.

Returns:boolean

true if the referred object was collected

Annotations
@Override
removeOwnerback to summary
public void removeOwner()

Implements com.sun.beans.util.Cache.Ref.removeOwner.

Marks this reference as removed from the cache.

Annotations
@Override
com.sun.beans.util back to summary

private final Class Cache.Kind.Strong<T>

extends Object
implements Cache.Ref<T>
Class Inheritance
All Implemented Interfaces
com.sun.beans.util.Cache.Ref
Type Parameters
<T>
the type of object to refer

This is an implementation of the Cache.Ref interface that uses the strong references that prevent their referents from being made finalizable, finalized, and then reclaimed.

Field Summary

Modifier and TypeField and Description
private Object
private final T

Constructor Summary

AccessConstructor and Description
private
Strong(Object
the owner of the reference, if needed
owner
,
T
the non-null object to refer
referent
)

Creates a strong reference to the specified object.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

the owner of the reference or null if the owner is unknown
getOwner
()

Implements com.sun.beans.util.Cache.Ref.getOwner.

Returns the object that possesses information about the reference.

public T

Returns:

the referred object
getReferent
()

Implements com.sun.beans.util.Cache.Ref.getReferent.

Returns the object to refer.

public boolean

Returns:

true if the referred object was collected
isStale
()

Implements com.sun.beans.util.Cache.Ref.isStale.

Determines whether the referred object was taken by the garbage collector or not.

public void
removeOwner()

Implements com.sun.beans.util.Cache.Ref.removeOwner.

Marks this reference as removed from the cache.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

ownerback to summary
private Object owner
referentback to summary
private final T referent

Constructor Detail

Strongback to summary
private Strong(Object owner, T referent)

Creates a strong reference to the specified object.

Parameters
owner:Object

the owner of the reference, if needed

referent:T

the non-null object to refer

Method Detail

getOwnerback to summary
public Object getOwner()

Implements com.sun.beans.util.Cache.Ref.getOwner.

Returns the object that possesses information about the reference.

Returns:Object

the owner of the reference or null if the owner is unknown

Annotations
@Override
getReferentback to summary
public T getReferent()

Implements com.sun.beans.util.Cache.Ref.getReferent.

Returns the object to refer.

Returns:T

the referred object

Annotations
@Override
isStaleback to summary
public boolean isStale()

Implements com.sun.beans.util.Cache.Ref.isStale.

Determines whether the referred object was taken by the garbage collector or not.

Returns:boolean

true if the referred object was collected

Annotations
@Override
removeOwnerback to summary
public void removeOwner()

Implements com.sun.beans.util.Cache.Ref.removeOwner.

Marks this reference as removed from the cache.

Annotations
@Override
com.sun.beans.util back to summary

private final Class Cache.Kind.Weak<T>

extends WeakReference<T>
implements Cache.Ref<T>
Class Inheritance
All Implemented Interfaces
com.sun.beans.util.Cache.Ref
Type Parameters
<T>
the type of object to refer

This is an implementation of the Cache.Ref interface that uses the weak references that do not prevent their referents from being made finalizable, finalized, and then reclaimed.
See Also
java.lang.ref.WeakReference

Field Summary

Modifier and TypeField and Description
private Object

Constructor Summary

AccessConstructor and Description
private
Weak(Object
the owner of the reference, if needed
owner
,
T
the non-null object to refer
referent
,
ReferenceQueue<? super T>
the queue to register the reference with, or null if registration is not required
queue
)

Creates a weak reference to the specified object.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

the owner of the reference or null if the owner is unknown
getOwner
()

Implements com.sun.beans.util.Cache.Ref.getOwner.

Returns the object that possesses information about the reference.

public T

Returns:

the referred object or null if it was collected
getReferent
()

Implements com.sun.beans.util.Cache.Ref.getReferent.

Returns the object to refer.

public boolean

Returns:

true if the referred object was collected
isStale
()

Implements com.sun.beans.util.Cache.Ref.isStale.

Determines whether the referred object was taken by the garbage collector or not.

public void
removeOwner()

Implements com.sun.beans.util.Cache.Ref.removeOwner.

Marks this reference as removed from the cache.

Field Detail

ownerback to summary
private Object owner

Constructor Detail

Weakback to summary
private Weak(Object owner, T referent, ReferenceQueue<? super T> queue)

Creates a weak reference to the specified object.

Parameters
owner:Object

the owner of the reference, if needed

referent:T

the non-null object to refer

queue:ReferenceQueue<? super T>

the queue to register the reference with, or null if registration is not required

Method Detail

getOwnerback to summary
public Object getOwner()

Implements com.sun.beans.util.Cache.Ref.getOwner.

Returns the object that possesses information about the reference.

Returns:Object

the owner of the reference or null if the owner is unknown

Annotations
@Override
getReferentback to summary
public T getReferent()

Implements com.sun.beans.util.Cache.Ref.getReferent.

Returns the object to refer.

Returns:T

the referred object or null if it was collected

Annotations
@Override
isStaleback to summary
public boolean isStale()

Implements com.sun.beans.util.Cache.Ref.isStale.

Determines whether the referred object was taken by the garbage collector or not.

Returns:boolean

true if the referred object was collected

Annotations
@Override
removeOwnerback to summary
public void removeOwner()

Implements com.sun.beans.util.Cache.Ref.removeOwner.

Marks this reference as removed from the cache.

Annotations
@Override
com.sun.beans.util back to summary

private Interface Cache.Ref<T>

Known Direct Implementers
com.sun.beans.util.Cache.Kind.Strong, com.sun.beans.util.Cache.Kind.Soft, com.sun.beans.util.Cache.Kind.Weak
Type Parameters
<T>
the type of object to refer

Basic interface for references. It defines the operations common for the all kind of references.

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

the owner of the reference or null if the owner is unknown
getOwner
()

Returns the object that possesses information about the reference.

public T

Returns:

the referred object or null if it was collected
getReferent
()

Returns the object to refer.

public boolean

Returns:

true if the referred object was collected
isStale
()

Determines whether the referred object was taken by the garbage collector or not.

public void
removeOwner()

Marks this reference as removed from the cache.

Method Detail

getOwnerback to summary
public Object getOwner()

Returns the object that possesses information about the reference.

Returns:Object

the owner of the reference or null if the owner is unknown

getReferentback to summary
public T getReferent()

Returns the object to refer.

Returns:T

the referred object or null if it was collected

isStaleback to summary
public boolean isStale()

Determines whether the referred object was taken by the garbage collector or not.

Returns:boolean

true if the referred object was collected

removeOwnerback to summary
public void removeOwner()

Marks this reference as removed from the cache.