com.sap.netweaver.bc.rf.util.flyweight

Class FlyWeight

java.lang.Object
  extended by com.sap.netweaver.bc.rf.util.flyweight.FlyWeight
All Implemented Interfaces:
Serializable, Cloneable
Direct Known Subclasses:
Enum, FlyWeightWithDescription, Name, Permission

public abstract class FlyWeight
extends Object
implements Cloneable, Serializable

Abstract class implementing the flyweight pattern. You have to extend this class and make all methods derived from this abstract class final (except for toString())! This is necessary in order to ensure, that no class can be derived from your class overriding and thereby changing the inner mechanism to create an flyweight instance only once, return the same object on clone() etc. It is of greatest importance to ensure that both the id and the flyweight object itself are immutable! Preknown flyweight instances may be added as public final static flyweight members. If you just need to have the predefined flyweight instances, implement an appropriate private constructor, otherwise make it public:


 public class Property extends FlyWeight
 {
     public final static Property RO = new Property( "RO" );
     public final static Property MV = new Property( "MV" );

     private Property ( final Serializable id )
     {
         super( id );
     }
 }
 

See Also:
Serialized Form

Constructor Summary
protected FlyWeight(Serializable id, boolean ignoreDuplicateRegistration)
          Construct instance of a flyweight.
 
Method Summary
 Object clone()
          Clone this flyweight.
 boolean equals(Object obj)
          Compare this and another object for equality.
static List getAllFlyWeights(Class flyWeightClass)
          Get all so far registered flyweight instances of the given flyweight class.
static List getAllFlyWeightsInherited(Class flyWeightClass)
          Get all so far registered flyweight instances of the given flyweight class and all sub classes of that class.
static FlyWeight getFlyWeight(Class flyWeightClass, Serializable id)
          Get flyweight instance by id of the given flyweight class.
 Serializable getFlyWeightId()
          Get id of the flyweight instance.
static FlyWeight getFlyWeightInherited(Class flyWeightClass, Serializable id)
          Get flyweight instance by id of the given flyweight class and all sub classes of that class.
 int hashCode()
          Get hashcode for this flyweight instance.
static List removeAllFlyWeights(Class flyWeightClass)
          Remove all so far registered flyweight instances of the given flyweight class.
static List removeAllFlyWeightsInherited(Class flyWeightClass)
          Remove all so far registered flyweight instances of the given flyweight class and all sub classes of that class.
static FlyWeight removeFlyWeight(Class flyWeightClass, Serializable id)
          Remove flyweight instance by id of the given flyweight class.
static FlyWeight removeFlyWeightInherited(Class flyWeightClass, Serializable id)
          Remove flyweight instance by id of the given flyweight class and all sub classes of that class.
 String toString()
          Get descriptive text for this flyweight instance.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

FlyWeight

protected FlyWeight(Serializable id,
                    boolean ignoreDuplicateRegistration)
             throws IllegalArgumentException
Construct instance of a flyweight.

Parameters:
id - flyweight id
ignoreDuplicateRegistration - when true, don't throw IllegalArgumentException exception when the id is already in use
Throws:
IllegalArgumentException - when the id is already in use
Method Detail

getFlyWeightId

public Serializable getFlyWeightId()
Get id of the flyweight instance.

Returns:
flyweight id

toString

public String toString()
Get descriptive text for this flyweight instance.

Overrides:
toString in class Object
Returns:
descriptive text for this flyweight instance

hashCode

public int hashCode()
Get hashcode for this flyweight instance.

Overrides:
hashCode in class Object
Returns:
flyweight hashcode

equals

public boolean equals(Object obj)
Compare this and another object for equality. Works only fine when handling objects of classes loaded by same class classloader.

Overrides:
equals in class Object
Parameters:
obj - object to be compared against
Returns:
result of comparison

clone

public Object clone()
Clone this flyweight. The clone method will always return the same instance for faster equality comparison. This is not harmful, since the flyweight is immutable.

Overrides:
clone in class Object
Returns:
this flyweight instance

getFlyWeight

public static FlyWeight getFlyWeight(Class flyWeightClass,
                                     Serializable id)
Get flyweight instance by id of the given flyweight class.

Parameters:
id - flyweight id
flyWeightClass - flyweight class
Returns:
flyweight instance

getFlyWeightInherited

public static FlyWeight getFlyWeightInherited(Class flyWeightClass,
                                              Serializable id)
Get flyweight instance by id of the given flyweight class and all sub classes of that class. If, follwoing the inheritance, multiple fyweights have the same id, the first found (behaviour is not deterministic) will be returned.

Parameters:
id - flyweight id
flyWeightClass - flyweight class
Returns:
flyweight instance

removeFlyWeight

public static FlyWeight removeFlyWeight(Class flyWeightClass,
                                        Serializable id)
Remove flyweight instance by id of the given flyweight class.

Parameters:
id - flyweight id
flyWeightClass - flyweight class
Returns:
flyweight instance removed or null

removeFlyWeightInherited

public static FlyWeight removeFlyWeightInherited(Class flyWeightClass,
                                                 Serializable id)
Remove flyweight instance by id of the given flyweight class and all sub classes of that class. If, follwoing the inheritance, multiple fyweights have the same id, the first found (behaviour is not deterministic) will be removed.

Parameters:
id - flyweight id
flyWeightClass - flyweight class
Returns:
flyweight instance removed or null

getAllFlyWeights

public static List getAllFlyWeights(Class flyWeightClass)
Get all so far registered flyweight instances of the given flyweight class. Note that only the so far registered instances can be returned. When for example some instances haven't been registered, because the class wasn't loaded, these instances won't be returned.

Parameters:
flyWeightClass - flyweight class for which all instances should be looked up
Returns:
all so far registered flyweight instances of the given flyweight class

getAllFlyWeightsInherited

public static List getAllFlyWeightsInherited(Class flyWeightClass)
Get all so far registered flyweight instances of the given flyweight class and all sub classes of that class. Note that only the so far registered instances can be returned. When for example some instances haven't been registered, because the class wasn't loaded, these instances won't be returned.

Parameters:
flyWeightClass - flyweight class (all sub classes of that class will also be checked) for which all instances should be looked up
Returns:
all so far registered flyweight instances of the given flyweight class and all sub classes of that class

removeAllFlyWeights

public static List removeAllFlyWeights(Class flyWeightClass)
Remove all so far registered flyweight instances of the given flyweight class. Note that only the so far registered instances can be removed. When for example some instances haven't been registered, because the class wasn't loaded, these instances won't be removed.

Parameters:
flyWeightClass - flyweight class for which all instances should be removed
Returns:
all so far removed flyweight instances of the given flyweight class

removeAllFlyWeightsInherited

public static List removeAllFlyWeightsInherited(Class flyWeightClass)
Remove all so far registered flyweight instances of the given flyweight class and all sub classes of that class. Note that only the so far registered instances can be removed. When for example some instances haven't been registered, because the class wasn't loaded, these instances won't be removed.

Parameters:
flyWeightClass - flyweight class (all sub classes of that class will also be checked) for which all instances should be removed
Returns:
all so far removed flyweight instances of the given flyweight class and all sub classes of that class
Access Rights

This class can be accessed from:


SC DC Public Part ACH
[sap.com] KMC-WPC [sap.com] tc/kmc/wpc/wpcfacade api EP-PIN-WPC-WCM
[sap.com] KMC-CM [sap.com] tc/km/frwk api EP-KM-CM


Copyright 2011 SAP AG Complete Copyright Notice