001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify it under the
005: * terms of the GNU General Public License as published by the Free Software
006: * Foundation; either version 2 of the License, or (at your option) any later
007: * version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful, but WITHOUT ANY
010: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
011: * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
012: *
013: * You should have received a copy of the GNU General Public License along with
014: * DrFTPD; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
015: * Suite 330, Boston, MA 02111-1307 USA
016: */
017: package org.drftpd.dynamicdata;
018:
019: import java.util.Collections;
020: import java.util.Date;
021: import java.util.HashMap;
022: import java.util.Hashtable;
023: import java.util.Map;
024:
025: import org.apache.log4j.Logger;
026:
027: /**
028: * Implements Map for javabeans support.
029: * @author mog
030: * @version $Id: KeyedMap.java 1562 2007-01-05 12:37:07Z zubov $
031: */
032: public class KeyedMap<K, V> extends Hashtable {
033: private static final Logger logger = Logger
034: .getLogger(KeyedMap.class);
035:
036: public KeyedMap() {
037: super ();
038: }
039:
040: public Map<Key, Object> getAllObjects() {
041: return Collections.unmodifiableMap(this );
042: }
043:
044: public Object getObject(Key key) throws KeyNotFoundException {
045: Object ret = get(key);
046: if (ret == null) {
047: throw new KeyNotFoundException();
048: }
049: return ret;
050: }
051:
052: public Object getObject(Key key, Object def) {
053: try {
054: return getObject(key);
055: } catch (KeyNotFoundException e) {
056: return def;
057: }
058: }
059:
060: public boolean getObjectBoolean(Key key) {
061: try {
062: return ((Boolean) getObject(key)).booleanValue();
063: } catch (KeyNotFoundException e) {
064: return false;
065: }
066: }
067:
068: public Date getObjectDate(Key key) {
069: return ((Date) getObject(key, new Date(System
070: .currentTimeMillis())));
071: }
072:
073: public float getObjectFloat(Key key) {
074: return ((Float) getObject(key, new Float(0))).floatValue();
075: }
076:
077: public int getObjectInt(Key key) {
078: try {
079: return ((Integer) getObject(key)).intValue();
080: } catch (KeyNotFoundException e) {
081: return 0;
082: }
083: }
084:
085: public long getObjectLong(Key key) {
086: return ((Long) getObject(key, new Long(0))).longValue();
087: }
088:
089: public String getObjectString(Key key) {
090: return (String) getObject(key, "");
091: }
092:
093: public void incrementObjectInt(Key key, int amount) {
094: if (!key.getType().equals(Integer.class)) {
095: throw new ClassCastException();
096: }
097:
098: synchronized (this ) {
099: Integer i;
100:
101: try {
102: i = (Integer) getObject(key);
103: } catch (KeyNotFoundException e) {
104: i = new Integer(0);
105: }
106:
107: setObject(key, new Integer(i.intValue() + amount));
108: }
109: }
110:
111: public void incrementObjectLong(Key key) {
112: incrementObjectInt(key, 1);
113: }
114:
115: public void incrementObjectLong(Key key, long amount) {
116: if (!key.getType().equals(Long.class)) {
117: throw new ClassCastException();
118: }
119:
120: synchronized (this ) {
121: Long i;
122:
123: try {
124: i = (Long) getObject(key);
125: } catch (KeyNotFoundException e) {
126: i = new Long(0);
127: }
128:
129: setObject(key, new Long(i.longValue() + amount));
130: }
131: }
132:
133: public void setAllObjects(KeyedMap m) {
134: putAll(m.getAllObjects());
135: }
136:
137: public void setObject(Key key, Object obj) {
138: if (obj == null) {
139: throw new NullPointerException(key + " - is null");
140: }
141:
142: if (!key.getType().isInstance(obj)) {
143: throw new ClassCastException(key + " - "
144: + key.getType().getName() + " - " + obj + " - "
145: + obj.getClass().getName());
146: }
147: put(key, obj);
148: }
149:
150: public void setObject(Key k, int v) {
151: setObject(k, new Integer(v));
152: }
153:
154: public void setObject(Key k, long v) {
155: setObject(k, new Long(v));
156: }
157: }
|