Factory Wizard

Lire cette page en français Go back

This wizard will generate a factory class. A factory is a singleton that instanciates objects of a specific type.

Information needed is:
  • The name of your nex factory class
  • The type of object handled by your factory
  • If you want this factory to handle objects created outside, quote the "do put method" option.


  • This is a factory that handles java.lang.Integer :
    import java.lang.reflect.Constructor;
    import java.lang.reflect.InvocationTargetException;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.Map;
    
    public final class IntegerFactory {
    
      /** This is the list of object. */
      protected Map objectMap;
    
      /** This is the default instance used for this singleton. */
      private static IntegerFactory defaultInstance;
    
      /**
       * Constructor of the object. <br>
       *
       * This constructor should remain private
       */
      private IntegerFactory() {
        super();
        this.objectMap = new HashMap();
        // Put your code here
      }
    
      /**
       * Gets the unique instance of this class. <br>
       *
       * @return the unique instance of this class
       */
      public final synchronized static IntegerFactory getInstance() {
        if (IntegerFactory.defaultInstance == null) {
          IntegerFactory.defaultInstance = new IntegerFactory();
        }
        return IntegerFactory.defaultInstance;
      }
    
      /**
       * Gives a string representation of this object. <br>
       * 
       * @return a String representation of this object
       */
      public String toString() {
        StringBuffer sb = new StringBuffer(128);
        sb.append('{');
        Iterator it = this.objectMap.values().iterator();
        while (it.hasNext()) {
          sb.append(it.next());
          sb.append(", ");
        }
        it = null;
        if (this.objectMap.size() > 0) {
          int length = sb.length();
          sb.delete(length - 2, length);
        }
        sb.append('}');
        return sb.toString();
      }
    
      /**
       * Gives an element in the factory. <br>
       * 
       * @param aKey key of the object to find
       * @return the object available at the given key
       */
      public Integer get(Object aKey) {
        return (Integer) this.objectMap.get(aKey);
      }
    
      /**
       * Gives an element in the factory. <br>
       * 
       * @param aKey key of the object to find
       * @param createIfAbstent if true and object is not present then the create(aKey) 
       * method will be used. Warning : this will cause an error if no plublic empty 
       * constructor is available in class Integer
       * @return the object available at the given key
       */
      public Integer get(Object aKey, boolean createIfAbstent) {
        Integer resu = (Integer) this.objectMap.get(aKey);
        if (resu == null && createIfAbstent) {
          resu = this.create(aKey);
        }
        return resu;
      }
    
      /**
       * Clears the map.
       */
      public void clear() {
        this.objectMap.clear();
      }
    
      /**
       * Indicates if the factory is empty.
       * 
       * @return true if the factory is empty, false if not
       */
      public boolean isEmpty() {
        return this.objectMap.size() == 0;
      }
    
      /**
       * Deletes the factory.
       * 
       * Use this method for memory leak, the factory will not be useable anymore
       */
      public void delete() {
        this.clear();
        this.objectMap = null;
      }
    
      /**
       * Returns the number of element in the factory.
       * 
       * @return the number of element in the factory
       */
      public int size() {
        return this.objectMap.size();
      }
    
      /**
       * Tests if an element is present or not. <br>
       * 
       * @param anElm the object to find
       * @return true if the element is present, false if not present
       */
      public boolean containsValue(Integer anElm) {
        return this.objectMap.containsValue(anElm);
      }
    
      /**
       * Creates an element and manage it. <br>
       *
       * The element is put in the map, and returned. <br>
       *
       * Caution : if a contructor needs a basic type (like int, double,
       * char, ...), you will be forced to pass it by creating a wrapper 
       * (Integer, Double, Character). Doing so, you will also have to 
       * precise in the boolean array (<i>isBasicType</i>) that this argument is a basic type and not an Object.
       *
       * @param aKey the key that will identify the object created
       * @param params an array of parameters to use with the constructor of the object
       * @param isBasicType an array of boolean that indicates if the params[i] is a basic type or not
       */
      public Integer create(
        Object aKey,
        Object[] params,
        boolean[] isBasicType) {
        Class[] paramsType = null;
        if (params != null) {
          paramsType = new Class[params.length];
          for (int i = 0; i < params.length; i++) {
            if (isBasicType[i]) {
              if (params[i] instanceof Integer) {
                paramsType[i] = Integer.TYPE;
              } else if (params[i] instanceof Double) {
                paramsType[i] = Double.TYPE;
              } else if (params[i] instanceof Float) {
                paramsType[i] = Float.TYPE;
              } else if (params[i] instanceof Long) {
                paramsType[i] = Long.TYPE;
              } else if (params[i] instanceof Byte) {
                paramsType[i] = Byte.TYPE;
              } else if (params[i] instanceof Short) {
                paramsType[i] = Short.TYPE;
              } else if (params[i] instanceof Character) {
                paramsType[i] = Character.TYPE;
              }
            } else {
              paramsType[i] = params[i].getClass();
            }
          }
        }
        try {
          Constructor ct = Integer.class.getConstructor(paramsType);
          Integer resu = (Integer) ct.newInstance(params);
          this.objectMap.put(aKey, resu);
          return resu;
        } catch (NoSuchMethodException nse) {
          nse.printStackTrace();
        } catch (InvocationTargetException ite) {
          ite.printStackTrace();
        } catch (IllegalAccessException iae) {
          iae.printStackTrace();
        } catch (InstantiationException ie) {
          ie.printStackTrace();
        }
        return null;
      }
    
      /**
       * Creates an element and manage it. <br>
       *
       * The element is put in the map, and returned. <br>
       * By default, all parameters will be concidered as Object, and not basic type. <br>
       *
       * @param aKey the key that will identify the object created
       * @param params an array of parameters to use with the constructor of the object
       */
      public Integer create(Object aKey, Object[] params) {
        boolean[] isBasicType = new boolean[params.length];
        return this.create(aKey, params, isBasicType);
      }
    
      /**
       * Creates an element and manage it. <br>
       *
       * The element is put in the map, and returned. <br>
       * By default, this method will try to use the constructor of the object that has no parameter. <br>
       *
       * @param aKey the key that will identify the object created
       */
      public Integer create(Object aKey) {
        return this.create(aKey, new Object[0], new boolean[0]);
      }
    
      /**
       * Removes the element with the specified key in this factory.
       *
       * @param aKey the key of the object to remove.
       */
      public void remove(Object aKey) {
        this.objectMap.remove(aKey);
      }
    }
      



    Comments, ideas and bugs mail me at : Contact