001: ///////////////////////////////
002: // Makumba, Makumba tag library
003: // Copyright (C) 2000-2003 http://www.makumba.org
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the GNU Lesser General Public
007: // License as published by the Free Software Foundation; either
008: // version 2.1 of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: // Lesser General Public License for more details.
014: //
015: // You should have received a copy of the GNU Lesser General Public
016: // License along with this library; if not, write to the Free Software
017: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: //
019: // -------------
020: // $Id: ArrayMap.java 1688 2007-09-25 12:54:46Z manuel_gay $
021: // $Name$
022: /////////////////////////////////////
023:
024: package org.makumba.commons;
025:
026: import java.util.Dictionary;
027: import java.util.Enumeration;
028: import java.util.Hashtable;
029:
030: /**
031: * This class exports an Object[] array as a dictionary
032: * @author Cristian Bogdan
033: */
034: public class ArrayMap extends Dictionary {
035: public Object[] data;
036:
037: Dictionary keyIndex;
038:
039: public ArrayMap(Dictionary d, Object[] o) {
040: data = o;
041: keyIndex = d;
042: }
043:
044: public ArrayMap() {
045: keyIndex = new Hashtable();
046: }
047:
048: Integer index(Object key) {
049: return (Integer) keyIndex.get(key);
050: }
051:
052: public Object get(Object key) {
053: Integer i = index(key);
054: if (i == null)
055: return null;
056: return data[i.intValue()];
057: }
058:
059: public Object put(Object key, Object value) {
060: Integer i = index(key);
061: if (i != null) {
062: Object ret = data[i.intValue()];
063: data[i.intValue()] = value;
064: return ret;
065: } else
066: throw new RuntimeException("invalid key: " + key);
067: }
068:
069: public Object remove(Object key) {
070: return put(key, null);
071: }
072:
073: public Enumeration keys() {
074: return new Enumeration() {
075: Object nxt;
076:
077: Object next;
078:
079: Enumeration e;
080: {
081: e = keyIndex.keys();
082: findNext();
083: }
084:
085: void findNext() {
086: next = null;
087: while (e.hasMoreElements()
088: && (next = get(nxt = e.nextElement())) == null)
089: ;
090: }
091:
092: public boolean hasMoreElements() {
093: return next != null;
094: }
095:
096: public Object nextElement() {
097: Object o = nxt;
098: findNext();
099: return o;
100: }
101: };
102: }
103:
104: public Enumeration elements() {
105: return new Enumeration() {
106: int i;
107: {
108: i = 0;
109: findNext();
110: }
111:
112: void findNext() {
113: while (i < data.length && data[i] == null)
114: i++;
115: }
116:
117: public boolean hasMoreElements() {
118: return i < data.length;
119: }
120:
121: public Object nextElement() {
122: Object o = data[i++];
123: findNext();
124: return o;
125: }
126: };
127: }
128:
129: public int size() {
130: int n = 0;
131: for (int i = 0; i < data.length; i++)
132: if (data[i] != null)
133: n++;
134: return n;
135: }
136:
137: public boolean isEmpty() {
138: return size() == 0;
139: }
140:
141: public String toString() {
142: StringBuffer ret = new StringBuffer();
143: ret.append("{");
144: String sep = "";
145: Object o;
146: for (Enumeration e = keys(); e.hasMoreElements();) {
147: ret.append(sep);
148: sep = ",";
149: ret.append(o = e.nextElement()).append("=").append(get(o));
150: }
151: ret.append("}");
152: return ret.toString();
153: }
154:
155: abstract class Enumerator implements Enumeration {
156:
157: }
158: }
|