net.sf.cglib
Class KeyFactory

java.lang.Object
  extended by net.sf.cglib.KeyFactory

public abstract class KeyFactory
extends java.lang.Object

Generates classes to handle multi-valued keys, for use in things such as Maps and Sets. Code for equals and hashCode methods follow the the rules laid out in Effective Java by Joshua Bloch.

To generate a KeyFactory, you need to supply an interface which describes the structure of the key. The interface should have a single method named newInstance, which returns an Object. The arguments array can be anything--Objects, primitive values, or single or multi-dimension arrays of either.

Once you have made a KeyFactory, you generate a new key by calling the newInstance method defined by your interface.

This example should print true followed by false:

   import net.sf.cglib.KeyFactory;
   public class KeySample {
       private interface MyFactory {
           public Object newInstance(int a, char[] b, String c);
       }
       public static void main(String[] args) {
           MyFactory f = (MyFactory)KeyFactory.create(MyFactory.class, null);
           Object key1 = f.newInstance(20, new char[]{ 'a', 'b' }, "hello");
           Object key2 = f.newInstance(20, new char[]{ 'a', 'b' }, "hello");
           Object key3 = f.newInstance(20, new char[]{ 'a', '_' }, "hello");
           System.out.println(key1.equals(key2));
           System.out.println(key2.equals(key3));
       }
   }
 

Note: hashCode equality between two keys key1 and key2 is guaranteed if key1.equals(key2) and the keys were produced by the same factory.

Version:
$Id: KeyFactory.java,v 1.14 2003/01/31 01:18:50 herbyderby Exp $

Method Summary
static KeyFactory create(java.lang.Class keyInterface, java.lang.ClassLoader loader)
           
abstract  java.lang.Object[] getArgs()
          Returns the list of objects that were used to create the key.
 int hashCode()
           
 
Methods inherited from class java.lang.Object
equals, getClass, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

create

public static KeyFactory create(java.lang.Class keyInterface,
                                java.lang.ClassLoader loader)

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

getArgs

public abstract java.lang.Object[] getArgs()
Returns the list of objects that were used to create the key. Primitive objects are wrapped.



Copyright (c) 2001 - Apache Software Foundation