001: /* *************************************************************************
002:
003: Millstone(TM)
004: Open Sourced User Interface Library for
005: Internet Development with Java
006:
007: Millstone is a registered trademark of IT Mill Ltd
008: Copyright (C) 2000-2005 IT Mill Ltd
009:
010: *************************************************************************
011:
012: This library is free software; you can redistribute it and/or
013: modify it under the terms of the GNU Lesser General Public
014: license version 2.1 as published by the Free Software Foundation.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: *************************************************************************
026:
027: For more information, contact:
028:
029: IT Mill Ltd phone: +358 2 4802 7180
030: Ruukinkatu 2-4 fax: +358 2 4802 7181
031: 20540, Turku email: info@itmill.com
032: Finland company www: www.itmill.com
033:
034: Primary source for MillStone information and releases: www.millstone.org
035:
036: ********************************************************************** */
037:
038: package org.millstone.base.terminal;
039:
040: import java.util.Hashtable;
041:
042: /** Simple two-way map for generating textual keys for objects and
043: * retrieving the objects later with the key.
044: *
045: * @author IT Mill Ltd.
046: * @version 3.1.1
047: * @since 3.0
048: */
049: public class KeyMapper {
050:
051: private int lastKey = 0;
052: private Hashtable objectKeyMap = new Hashtable();
053: private Hashtable keyObjectMap = new Hashtable();
054:
055: /** Get key for an object */
056: public String key(Object o) {
057:
058: if (o == null)
059: return "null";
060:
061: // If the object is already mapped, use existing key
062: String key = (String) objectKeyMap.get(o);
063: if (key != null)
064: return key;
065:
066: // If the object is not yet mapped, map it
067: key = String.valueOf(++lastKey);
068: objectKeyMap.put(o, key);
069: keyObjectMap.put(key, o);
070:
071: return key;
072: }
073:
074: /** Check if the key belongs to a new id.
075: * <p>Usage of new id:s are specific to components, but for example Select
076: * component uses newItemId:s for selection of newly added items in
077: * <code>allowNewItems</code>-mode
078: */
079: public boolean isNewIdKey(String key) {
080: return "NEW".equals(key);
081: }
082:
083: /** Retrieve object with the key*/
084: public Object get(String key) {
085:
086: return keyObjectMap.get(key);
087: }
088:
089: /** Remove object from the mapper. */
090: public void remove(Object o) {
091: String key = (String) objectKeyMap.get(o);
092:
093: if (key != null) {
094: objectKeyMap.remove(key);
095: keyObjectMap.remove(o);
096: }
097: }
098:
099: /** Remove all objects from the mapper. */
100: public void removeAll() {
101: objectKeyMap.clear();
102: keyObjectMap.clear();
103: }
104: }
|