001: /*
002: * Copyright (C) 2004 NNL Technology AB
003: * Visit www.infonode.net for information about InfoNode(R)
004: * products and how to contact NNL Technology AB.
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
019: * MA 02111-1307, USA.
020: */
021:
022: // $Id: ConstVectorMap.java,v 1.8 2005/03/17 16:13:03 jesper Exp $
023: package net.infonode.util.collection.map;
024:
025: import net.infonode.util.collection.map.base.ConstMap;
026: import net.infonode.util.collection.map.base.ConstMapIterator;
027:
028: import java.util.ArrayList;
029:
030: public class ConstVectorMap implements ConstMap {
031: private class ConstIterator implements ConstMapIterator {
032: private int index = 1;
033: private ConstMapIterator iterator;
034:
035: ConstIterator() {
036: if (maps.size() > 0) {
037: iterator = getMap(0).constIterator();
038: advance();
039: } else
040: iterator = EmptyIterator.INSTANCE;
041: }
042:
043: public Object getKey() {
044: return iterator.getKey();
045: }
046:
047: public Object getValue() {
048: return iterator.getValue();
049: }
050:
051: public void next() {
052: iterator.next();
053: advance();
054: }
055:
056: public boolean atEntry() {
057: return iterator.atEntry();
058: }
059:
060: private void advance() {
061: while (true) {
062: while (!iterator.atEntry()) {
063: if (index == maps.size())
064: return;
065:
066: iterator = getMap(index++).constIterator();
067: }
068:
069: if (ConstVectorMap.this .getValue(iterator.getKey(), 0,
070: index - 1) == null)
071: return;
072:
073: iterator.next();
074: }
075: }
076: }
077:
078: private ArrayList maps = new ArrayList(2);
079:
080: private Object getValue(Object key, int fromIndex, int toIndex) {
081: for (int i = fromIndex; i < toIndex; i++) {
082: Object value = getMap(i).get(key);
083:
084: if (value != null)
085: return value;
086: }
087:
088: return null;
089: }
090:
091: public void addMap(ConstMap map) {
092: addMap(maps.size(), map);
093: }
094:
095: public void addMap(int index, final ConstMap map) {
096: maps.add(index, map);
097: }
098:
099: public int getMapCount() {
100: return maps.size();
101: }
102:
103: public ConstMap removeMap(int index) {
104: return (ConstMap) maps.remove(index);
105: }
106:
107: public Object get(Object key) {
108: for (int i = 0; i < maps.size(); i++) {
109: Object v = getMap(i).get(key);
110:
111: if (v != null)
112: return v;
113: }
114:
115: return null;
116: }
117:
118: public boolean containsKey(Object key) {
119: for (int i = 0; i < maps.size(); i++) {
120: if (getMap(i).containsKey(key))
121: return true;
122: }
123:
124: return false;
125: }
126:
127: public boolean containsValue(Object value) {
128: for (int i = 0; i < maps.size(); i++) {
129: if (getMap(i).containsValue(value))
130: return true;
131: }
132:
133: return false;
134: }
135:
136: public boolean isEmpty() {
137: for (int i = 0; i < maps.size(); i++) {
138: if (!getMap(i).isEmpty())
139: return false;
140: }
141:
142: return true;
143: }
144:
145: public ConstMap getMap(int index) {
146: return (ConstMap) maps.get(index);
147: }
148:
149: public int getMapIndex(ConstMap map) {
150: return maps.indexOf(map);
151: }
152:
153: public ConstMapIterator constIterator() {
154: return new ConstIterator();
155: }
156: }
|