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

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » Class » Clone 




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:[email protected]">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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.