FileDocCategorySizeDatePackage
IndirectMap.javaAPI DocGlassfish v2 API23495Tue May 22 16:54:20 BST 2007oracle.toplink.essentials.indirection

IndirectMap

public class IndirectMap extends Hashtable implements IndirectContainer
IndirectMap allows a domain class to take advantage of TopLink indirection without having to declare its instance variable as a ValueHolderInterface.

To use an IndirectMap:

  • Declare the appropriate instance variable with type Hashtable (jdk1.1) or Map (jdk1.2).
  • Send the message #useTransparentMap(String) to the appropriate CollectionMapping.
TopLink will place an IndirectMap in the instance variable when the containing domain object is read from the datatabase. With the first message sent to the IndirectMap, the contents are fetched from the database and normal Hashtable/Map behavior is resumed.
see
oracle.toplink.essentials.mappings.CollectionMapping
see
oracle.toplink.essentials.indirection.IndirectList
author
Big Country
since
TOPLink/Java 2.5

Fields Summary
protected Hashtable
delegate
Reduce type casting
protected ValueHolderInterface
valueHolder
Delegate indirection behavior to a value holder
private transient String
attributeName
The mapping attribute name, used to raise change events.
protected int
initialCapacity
Store initial size for lazy init.
protected float
loadFactor
Store load factor for lazy init.
Constructors Summary
public IndirectMap()
PUBLIC: Construct a new, empty IndirectMap with a default capacity and load factor.


                      
      
        this(11);
    
public IndirectMap(int initialCapacity)
PUBLIC: Construct a new, empty IndirectMap with the specified initial capacity and default load factor.

param
initialCapacity the initial capacity of the hashtable

        this(initialCapacity, 0.75f);
    
public IndirectMap(int initialCapacity, float loadFactor)
PUBLIC: Construct a new, empty IndirectMap with the specified initial capacity and load factor.

param
initialCapacity the initial capacity of the hashtable
param
loadFactor a number between 0.0 and 1.0
exception
IllegalArgumentException if the initial capacity is less than or equal to zero, or if the load factor is less than or equal to zero

        super(0);
        this.initialize(initialCapacity, loadFactor);
    
public IndirectMap(Map m)
PUBLIC: Construct a new IndirectMap with the same mappings as the given Map. The IndirectMap is created with a capacity of twice the number of entries in the given Map or 11 (whichever is greater), and a default load factor, which is 0.75.

param
m a map containing the mappings to use

        super(0);
        this.initialize(m);
    
Methods Summary
protected java.util.HashtablebuildDelegate()
Return the freshly-built delegate.

        return (Hashtable)getValueHolder().getValue();
    
public synchronized voidclear()

see
java.util.Hashtable#clear()

        this.getDelegate().clear();
    
public synchronized java.lang.Objectclone()

see
java.util.Hashtable#clone() This will result in a database query if necessary.

        IndirectMap result = (IndirectMap)super.clone();
        result.delegate = (Hashtable)this.getDelegate().clone();
        return result;
    
public synchronized booleancontains(java.lang.Object value)

see
java.util.Hashtable#contains(java.lang.Object)

        return this.getDelegate().contains(value);
    
public synchronized booleancontainsKey(java.lang.Object key)

see
java.util.Hashtable#containsKey(java.lang.Object)

        return this.getDelegate().containsKey(key);
    
public booleancontainsValue(java.lang.Object value)

see
java.util.Hashtable#containsValue(java.lang.Object)

        return this.getDelegate().containsValue(value);
    
public synchronized java.util.Enumerationelements()

see
java.util.Hashtable#elements()

        return this.getDelegate().elements();
    
public java.util.SetentrySet()

see
java.util.Hashtable#entrySet()

        return new Set (){
            Set delegateSet = IndirectMap.this.getDelegate().entrySet();
            
            public int size(){
                return this.delegateSet.size();
            }
        
            public boolean isEmpty(){
                return this.delegateSet.isEmpty();
            }
        
            public boolean contains(Object o){
                return this.delegateSet.contains(o);
            }
        
            public Iterator iterator(){
                return new Iterator() {
                    Iterator delegateIterator = delegateSet.iterator();
                    Object currentObject;
                    
                    public boolean hasNext() {
                        return this.delegateIterator.hasNext();
                    }
                    
                    public Object next() {
                        this.currentObject = this.delegateIterator.next();
                        return this.currentObject;
                    }
                    
                    public void remove() {
                        raiseRemoveChangeEvent(((Map.Entry)currentObject).getKey(), ((Map.Entry)currentObject).getValue());
                        this.delegateIterator.remove();
                    }
                };
            }
        
            public Object[] toArray(){
                return this.delegateSet.toArray();
            }
    
            public Object[] toArray(Object a[]){
                return this.delegateSet.toArray(a);
            }
    
            public boolean add(Object o){
                return this.delegateSet.add(o);
            }
        
            public boolean remove(Object o){
                if (!(o instanceof Map.Entry)) {
                    return false;
                }
                return (IndirectMap.this.remove(((Map.Entry)o).getKey()) != null);
            }
        
            public boolean containsAll(Collection c){
                return this.delegateSet.containsAll(c);
            }
        
            public boolean addAll(Collection c){
                return this.delegateSet.addAll(c);
            }
        
            public boolean retainAll(Collection c){
                boolean result = false;
                Iterator objects = delegateSet.iterator();
                while (objects.hasNext()) {
                    Map.Entry object = (Map.Entry)objects.next();
                    if (!c.contains(object)) {
                        objects.remove();
                        raiseRemoveChangeEvent(object.getKey(), object.getValue());
                        result = true;
                    }
                }
                return result;
            }
            
            public boolean removeAll(Collection c){
                boolean result = false;
                for (Iterator cs = c.iterator(); cs.hasNext(); ){
                    Object object = cs.next();
                    if ( ! (object instanceof Map.Entry)){
                        continue;
                    }
                    Object removed = IndirectMap.this.remove(((Map.Entry)object).getKey());
                    if (removed != null){
                        result = true;
                    }
                }
                return result;
            }
        
            public void clear(){
                IndirectMap.this.clear();
            }
        
            public boolean equals(Object o){
                return this.delegateSet.equals(o);
            }
            
            public int hashCode(){
                return this.delegateSet.hashCode();
            }
        };
    
public synchronized booleanequals(java.lang.Object o)

see
java.util.Hashtable#equals(java.lang.Object)

        return this.getDelegate().equals(o);
    
public synchronized java.lang.Objectget(java.lang.Object key)

see
java.util.Hashtable#get(java.lang.Object)

        return this.getDelegate().get(key);
    
protected synchronized java.util.HashtablegetDelegate()
Check whether the contents have been read from the database. If they have not, read them and set the delegate.

        if (delegate == null) {
            delegate = this.buildDelegate();
        }
        return delegate;
    
public java.lang.StringgetTopLinkAttributeName()
Return the mapping attribute name, used to raise change events.

         return attributeName;
     
public synchronized ValueHolderInterfacegetValueHolder()
PUBLIC: Return the valueHolder.

        // PERF: lazy initialize value holder and vector as are normally set after creation.
        if (valueHolder == null) {
            valueHolder = new ValueHolder(new Hashtable(initialCapacity, loadFactor));
        }
        return valueHolder;
    
public synchronized inthashCode()

see
java.util.Hashtable#hashCode()

        return this.getDelegate().hashCode();
    
protected voidinitialize(int initialCapacity, float loadFactor)
Initialize the instance.

        this.delegate = null;
        this.loadFactor = loadFactor;
        this.initialCapacity = initialCapacity;
        this.valueHolder = null;
    
protected voidinitialize(java.util.Map m)
Initialize the instance.

        this.delegate = null;
        Hashtable temp = new Hashtable(m);

        this.valueHolder = new ValueHolder(temp);
    
public booleanisEmpty()

see
java.util.Hashtable#isEmpty()

        return this.getDelegate().isEmpty();
    
public booleanisInstantiated()
PUBLIC: Return whether the contents have been read from the database.

        return this.getValueHolder().isInstantiated();
    
public java.util.SetkeySet()

see
java.util.Hashtable#keySet()

        
        return new Set (){
            Set delegateSet = IndirectMap.this.getDelegate().keySet();
            
            public int size(){
                return this.delegateSet.size();
            }
        
            public boolean isEmpty(){
                return this.delegateSet.isEmpty();
            }
        
            public boolean contains(Object o){
                return this.delegateSet.contains(o);
            }
        
            public Iterator iterator(){
                return new Iterator() {
                    Iterator delegateIterator = delegateSet.iterator();
                    Object currentObject;
                    
                    public boolean hasNext() {
                        return this.delegateIterator.hasNext();
                    }
                    
                    public Object next() {
                        this.currentObject = this.delegateIterator.next();
                        return this.currentObject;
                    }
                    
                    public void remove() {
                        IndirectMap.this.raiseRemoveChangeEvent(currentObject, IndirectMap.this.getDelegate().get(currentObject));
                        this.delegateIterator.remove();
                    }
                };
            }
        
            public Object[] toArray(){
                return this.delegateSet.toArray();
            }
    
            public Object[] toArray(Object a[]){
                return this.delegateSet.toArray(a);
            }
    
            public boolean add(Object o){
                return this.delegateSet.add(o);
            }
        
            public boolean remove(Object o){
                return (IndirectMap.this.remove(o) != null);
            }
        
            public boolean containsAll(Collection c){
                return this.delegateSet.containsAll(c);
            }
        
            public boolean addAll(Collection c){
                return this.delegateSet.addAll(c);
            }
        
            public boolean retainAll(Collection c){
                boolean result = false;
                Iterator objects = delegateSet.iterator();
                while (objects.hasNext()) {
                    Object object = objects.next();
                    if (!c.contains(object)) {
                        objects.remove();
                        IndirectMap.this.raiseRemoveChangeEvent(object, IndirectMap.this.getDelegate().get(object));
                        result = true;
                    }
                }
                return result;
            }
            
            public boolean removeAll(Collection c){
                boolean result = false;
                for (Iterator cs = c.iterator(); cs.hasNext(); ){
                    if (IndirectMap.this.remove(cs.next()) != null ) {
                        result = true;
                    }
                }
                return result;
            }
        
            public void clear(){
                IndirectMap.this.clear();
            }
        
            public boolean equals(Object o){
                return this.delegateSet.equals(o);
            }
            
            public int hashCode(){
                return this.delegateSet.hashCode();
            }
        };
            
            
    
public synchronized java.util.Enumerationkeys()

see
java.util.Hashtable#keys()

        return this.getDelegate().keys();
    
public synchronized java.lang.Objectput(java.lang.Object key, java.lang.Object value)

see
java.util.Hashtable#put(java.lang.Object, java.lang.Object)

        Object oldValue = this.getDelegate().put(key, value);
        if (oldValue != null){
            raiseRemoveChangeEvent(key, oldValue);
        }
        raiseAddChangeEvent(key, value);
        return oldValue;
    
public synchronized voidputAll(java.util.Map t)

see
java.util.Hashtable#putAll(java.util.Map)

        this.getDelegate().putAll(t);
    
protected voidraiseAddChangeEvent(java.lang.Object key, java.lang.Object value)
Raise the add change event and relationship maintainence.

        // this is where relationship maintenance would go
    
protected voidraiseRemoveChangeEvent(java.lang.Object key, java.lang.Object value)
Raise the remove change event.

        // this is where relationship maintenance would go
    
protected voidrehash()

see
java.util.Hashtable#rehash()

        throw new InternalError("unsupported");
    
public synchronized java.lang.Objectremove(java.lang.Object key)

see
java.util.Hashtable#remove(java.lang.Object)

        Object value = this.getDelegate().remove(key);
        if (value != null){
            raiseRemoveChangeEvent(key, value);
        }
        return value;
    
public voidsetTopLinkAttributeName(java.lang.String attributeName)
Set the mapping attribute name, used to raise change events. This is required if the change listener is set.

         this.attributeName = attributeName;
     
public voidsetValueHolder(ValueHolderInterface valueHolder)
PUBLIC: Set the value holder.

        this.delegate = null;
        this.valueHolder = valueHolder;
    
public intsize()

see
java.util.Hashtable#size()

        return this.getDelegate().size();
    
public java.lang.StringtoString()
PUBLIC: Use the Hashtable.toString(); but wrap it with braces to indicate there is a bit of indirection. Don't allow this method to trigger a database read.

see
java.util.Hashtable#toString()

        if (ValueHolderInterface.shouldToStringInstantiate) {
            return this.getDelegate().toString();
        }
        if (this.isInstantiated()) {
            return "{" + this.getDelegate().toString() + "}";
        } else {
            return "{" + oracle.toplink.essentials.internal.helper.Helper.getShortClassName(this.getClass()) + ": not instantiated}";
        }
    
public java.util.Collectionvalues()

see
java.util.Hashtable#values()

        return new Collection() {
            protected Collection delegateCollection = IndirectMap.this.getDelegate().values();

            public int size(){
                return delegateCollection.size();
            }
            
            public boolean isEmpty(){
                return delegateCollection.isEmpty();
            }
            
            public boolean contains(Object o){
                return delegateCollection.contains(o);
            }
            
            public Iterator iterator() {
                return new Iterator() {
                    Iterator delegateIterator = delegateCollection.iterator();
                    Object currentObject;
                    
                    public boolean hasNext() {
                        return this.delegateIterator.hasNext();
                    }
                    
                    public Object next() {
                        this.currentObject = this.delegateIterator.next();
                        return this.currentObject;
                    }
                    
                    public void remove() {
                        Iterator iterator = IndirectMap.this.getDelegate().entrySet().iterator();
                        while (iterator.hasNext()){
                            Map.Entry entry = (Map.Entry)iterator.next();
                            if (entry.getValue().equals(currentObject)){
                                IndirectMap.this.raiseRemoveChangeEvent(entry.getKey(), entry.getValue());
                            }
                            
                        }
                        this.delegateIterator.remove();
                    }
                };
            }
        
            public Object[] toArray(){
                return this.delegateCollection.toArray();
            }
            
            public Object[] toArray(Object a[]){
                return this.delegateCollection.toArray(a);
            }
            
            public boolean add(Object o){
                return this.delegateCollection.add(o);
            }
            
            public boolean remove(Object o){
                Iterator iterator = IndirectMap.this.getDelegate().entrySet().iterator();
                while (iterator.hasNext()){
                    Map.Entry entry = (Map.Entry)iterator.next();
                    if (entry.getValue().equals(o)){
                        IndirectMap.this.raiseRemoveChangeEvent(entry.getKey(), entry.getValue());
                    }
                    return true;    
                }
                return false;
            }
            
            public boolean containsAll(Collection c){
                return this.delegateCollection.containsAll(c);
            }
            
            public boolean addAll(Collection c){
                return this.delegateCollection.addAll(c);
            }
            
            public boolean removeAll(Collection c){
                boolean result = false;
                for (Iterator iterator = c.iterator(); iterator.hasNext();){
                    if (remove(iterator.next()) ){
                        result = true;
                    }
                }
                return result;
            }
            
            public boolean retainAll(Collection c){
                boolean result = false;
                for (Iterator iterator = IndirectMap.this.entrySet().iterator(); iterator.hasNext();){
                    Map.Entry entry = (Map.Entry)iterator.next();
                    if (! c.contains(entry.getValue()) ) {
                        iterator.remove();
                        result = true;
                    }
                }
                return result;
            }
            
            public void clear(){
                IndirectMap.this.clear();
            }
            
            
            public boolean equals(Object o){
                return this.delegateCollection.equals(o);
            }
            
            public int hashCode(){
                return this.delegateCollection.hashCode();
            }
            
        };