Implements a pool of internalized objects : Clone « Class « Java






Implements a pool of internalized objects

   
/*
 * Copyright 2007 (C) TJDO.
 * All rights reserved.
 *
 * This software is distributed under the terms of the TJDO License version 1.0.
 * See the terms of the TJDO License in the documentation provided with this software.
 *
 * $Id: InternPool.java,v 1.1 2007/11/30 19:42:26 jackknifebarber Exp $
 */


import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.AbstractCollection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.WeakHashMap;


/**
 * Implements a pool of <dfn>internalized</dfn> objects.
 * Used to implement pools that behave like the one used by String.intern().
 * <p>
 * This implementation is synchronized.
 * It is a generic collection that is unmodifiable with the exception of the
 * {@link #intern} method.
 *
 * @author <a href="mailto:jackknifebarber@users.sourceforge.net">Mike Martin</a>
 * @version $Revision: 1.1 $
 */

public class InternPool extends AbstractCollection
{
    private final Map pool = new WeakHashMap();

    /**
     * Returns a canonical representation for the specified object.
     * <p>
     * This method behaves much like <code>String.intern()</code>.
     * A pool of objects, initially empty, is maintained privately.
     * <p>
     * If the pool already contains an object equal to the specified object as
     * determined by the <code>equals(Object)</code> method, then the object
     * from the pool is returned.
     * Otherwise, the specified object is added to the pool and a reference to
     * the specified object is returned.
     * <p>
     * It follows that for any two objects <code>o1</code> and <code>o2</code>,
     * <code>intern(o1)&nbsp;==&nbsp;intern(o2)</code> is <code>true</code>
     * if and only if <code>o1.equals(o2)</code> is <code>true</code>.
     *
     * @param candidate
     *      the object to internalize
     *
     * @return
     *      an object that is equivalent to the specified object, but is
     *      guaranteed to be from a pool of unique objects.
     */
    public synchronized Object intern(Object candidate)
    {
        Reference ref = (Reference)pool.get(candidate);
        Object pooled = ref == null ? null : ref.get();

        if (pooled == null)
        {
            pooled = candidate;
            pool.put(pooled, new WeakReference(pooled));
        }

        return pooled;
    }

    public synchronized boolean contains(Object o)
    {
        return pool.containsKey(o);
    }

    public synchronized Iterator iterator()
    {
        return Collections.unmodifiableSet(pool.keySet()).iterator();
    }

    public synchronized int size()
    {
        return pool.size();
    }
}

   
    
    
  








Related examples in the same category

1.A Cloning Example
2.Class is declared to be cloneable.
3.Arrays are automatically cloneable
4.Clone objects
5.Creating a Deep Copy
6.Shallow Copy TestShallow Copy Test
7.Deep Copy TestDeep Copy Test
8.Uses serialization to perform deep copy cloning.
9.Tests cloning to see if destination of references are also clonedTests cloning to see if destination of references are also cloned
10.Creating local copies with cloneCreating local copies with clone
11.You can insert Cloneability at any level of inheritance
12.Cloning a composed objectCloning a composed object
13.Serializable and cloneSerializable and clone
14.Go through a few gyrations to add cloning to your own classGo through a few gyrations to add cloning to your own class
15.Checking to see if a reference can be clonedChecking to see if a reference can be cloned
16.The clone operation works for only a few items in the standard Java libraryThe clone operation works for only a few items in the standard Java library
17.Demonstration of cloning
18.Simple demo of avoiding side-effects by using Object.cloneSimple demo of avoiding side-effects by using Object.clone
19.Clone an object with clone method from parent
20.Manipulate properties after clone operation
21.Deep clone ObjectDeep clone Object
22.Serializable Clone
23.Utility for object cloning
24.Clone Via Serialization
25.Clone demo
26.Deep clone serializing/de-serializng Clone
27.A collection of utilities to workaround limitations of Java clone framework
28.This program demonstrates cloning
29.Returns a copy of the object, or null if the object cannot be serialized.Returns a copy of the object, or null if the object cannot be serialized.
30.Deep-copies the values from one object to the other
31.Object Deep copy