Index: lutinutil/src/java/org/codelutin/util/OptionKeyFactory.java diff -u /dev/null lutinutil/src/java/org/codelutin/util/OptionKeyFactory.java:1.1 --- /dev/null Wed Nov 28 01:22:21 2007 +++ lutinutil/src/java/org/codelutin/util/OptionKeyFactory.java Wed Nov 28 01:22:16 2007 @@ -0,0 +1,195 @@ +/* *##% +* Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 Code Lutin, +* Cédric Pineau, Benjamin Poussin, +* +* +* This program is free software; you can redistribute it and/or +* modify it under the terms of the GNU General Public License +* as published by the Free Software Foundation; either version 2 +* of the License, or (at your option) any later version. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with this program; if not, write to the Free Software +* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*##%*/ + +package org.codelutin.util; + +import java.util.Map; +import java.util.TreeMap; + +/** + * This classes contains OptionKey and OptionArgumentKey factories. + *

+ * A given key is always instanciate once for all and kept in cache. + *

+ * Anyway it is not possible to instanciate {@link OptionKey} nor + * {@link OptionArgumentKey}, + *

+ * Use instead {@link #getOptionKey(String)} or + * {@link #getArgumentKey(OptionArgumentDefinition)} static methods to obtain + * the required key. + * + * @author chemit + */ + +public class OptionKeyFactory { + + private static Map> argumentKeysCache; + + private static Map optionKeysCache; + + public static OptionKey getOptionKey(String name) { + OptionKey result = getOptionKeysCache().get(name); + if (result == null) { + optionKeysCache.put(name, result = new OptionKey(name)); + } + return result; + } + + public static OptionArgumentKey getArgumentKey(OptionArgumentDefinition argumentDefinition) { + Class clazz = argumentDefinition.getValueType().getClazz(); + String key = argumentDefinition.getKey().hashCode() + "_" + clazz.hashCode(); + OptionArgumentKey result; + result = getArgumentKey0(key, argumentDefinition.getValueType().getClazz()); + return result; + } + + public static void clearCache() { + if (optionKeysCache != null) { + optionKeysCache.clear(); + optionKeysCache = null; + } + if (argumentKeysCache != null) { + argumentKeysCache.clear(); + argumentKeysCache = null; + } + } + + @Override + protected void finalize() throws Throwable { + super.finalize(); + clearCache(); + } + + protected static Map> getArgumentKeysCache() { + if (argumentKeysCache == null) { + argumentKeysCache = new TreeMap>(); + } + return argumentKeysCache; + } + + protected static Map getOptionKeysCache() { + if (optionKeysCache == null) { + optionKeysCache = new TreeMap(); + } + return optionKeysCache; + } + + @SuppressWarnings({"unchecked"}) + private static OptionArgumentKey getArgumentKey0(String key, Class clazz) { + OptionArgumentKey optionArgumentKey = (OptionArgumentKey) getArgumentKeysCache().get(key); + if (optionArgumentKey == null) { + optionArgumentKey = new OptionArgumentKey(key, clazz); + argumentKeysCache.put(key, optionArgumentKey); + } + return optionArgumentKey; + } + + //TODO Make a KeyUtil class + /** + * Une clef d'option + * + * @author chemit + */ + public static final class OptionKey extends Key { + private OptionKey(String name) { + super(name); + } + } + + /** + * Une clef d'argument d'option + * + * @author chemit + */ + + public static class OptionArgumentKey extends TypedKey { + + private OptionArgumentKey(String name, Class type) { + super(name, type); + } + + } + + /** + * Clef typée générique + * + * @author chemit + */ + + public static class TypedKey extends Key { + + /** final modifier : since it must never mutate! */ + protected final Class type; + + /** final modifier : since it must never mutate! */ + protected final String key; + + public TypedKey(String name, Class type) { + super(name); + this.type = type; + this.key = name.hashCode() + "_" + type.hashCode(); + } + + public Class getType() { + return type; + } + + @Override + public String toString() { + return super.toString() + ", type:" + type; + } + + @Override + public int compareTo(Key o) { + return key.compareTo(((TypedKey) o).key); + } + + } + + /** + * Clef simple générique + * + * @author chemit + */ + + public static class Key implements Comparable { + + /** final modifier : since it must never mutate! */ + protected final String name; + + public Key(String name) { + this.name = name; + } + + public String name() { + return name; + } + + public int compareTo(Key o) { + return name.compareTo(o.name); + } + + @Override + public String toString() { + return super.toString() + ", name:" + name; + } + + } +}