001: package net.bagaluten.jca.lucene.connector;
002:
003: import java.io.IOException;
004: import java.io.Serializable;
005: import java.util.Collection;
006: import java.util.HashMap;
007: import java.util.HashSet;
008: import java.util.Iterator;
009: import java.util.LinkedList;
010: import java.util.List;
011: import java.util.ListIterator;
012: import java.util.Map;
013: import java.util.Set;
014:
015: public class Entries implements List<Entry>, Serializable {
016:
017: /**
018: *
019: */
020: private static final long serialVersionUID = 236876586L;
021:
022: private int hits;
023:
024: private List<Entry> list = new LinkedList<Entry>();
025:
026: private Map<String, Integer> keyMap = new HashMap<String, Integer>();
027:
028: private transient Map<Integer, Integer> countMap = new HashMap<Integer, Integer>();
029:
030: private transient Map<Integer, String> valueMap = new HashMap<Integer, String>();
031:
032: private class EntryImpl implements Entry, Serializable {
033:
034: private static final long serialVersionUID = 872364865L;
035:
036: private class MapEntry implements
037: java.util.Map.Entry<String, Field> {
038: private String key;
039:
040: MapEntry(String key) {
041: this .key = key;
042: }
043:
044: public String getKey() {
045: return key;
046: }
047:
048: public Field getValue() {
049: return map.get(keyMap.get(key));
050: }
051:
052: public Field setValue(Field value) {
053: return map.put(keyMap.get(key), value);
054: }
055:
056: }
057:
058: private Map<Integer, Field> map = new HashMap<Integer, Field>();
059:
060: private double ranking;
061:
062: public double getRanking() {
063: return ranking;
064: }
065:
066: public void setRanking(double ranking) {
067: this .ranking = ranking;
068: }
069:
070: public void clear() {
071: for (Integer p : map.keySet()) {
072: remove(p);
073: }
074: }
075:
076: public boolean containsKey(Object key) {
077: return keyMap.containsKey(key) ? map.containsKey(keyMap
078: .get(key)) : false;
079: }
080:
081: public boolean containsValue(Object value) {
082: return map.containsValue(value);
083: }
084:
085: public Set<java.util.Map.Entry<String, Field>> entrySet() {
086: Set<java.util.Map.Entry<String, Field>> set = new HashSet<Entry<String, Field>>(
087: map.size());
088: for (Integer i : map.keySet()) {
089: java.util.Map.Entry<String, Field> e = new MapEntry(
090: valueMap.get(i));
091: set.add(e);
092: }
093: return set;
094: }
095:
096: public Field get(Object key) {
097:
098: return map.get(keyMap.get(key));
099: }
100:
101: public boolean isEmpty() {
102: return map.isEmpty();
103: }
104:
105: public Set<String> keySet() {
106: Set<String> set = new HashSet<String>(map.size());
107: for (Integer i : map.keySet()) {
108: String s = valueMap.get(i);
109: set.add(s);
110: }
111: return set;
112: }
113:
114: Set<Integer> tokenSet() {
115: return map.keySet();
116: }
117:
118: public Field put(String key, Field value) {
119: Integer i = keyMap.get(key);
120: if (i == null) {
121: i = keyMap.size();
122: keyMap.put(key, i);
123: valueMap.put(i, key);
124: countMap.put(i, 1);
125: } else {
126: countMap.put(i, countMap.get(i) + 1);
127: }
128: return map.put(i, value);
129: }
130:
131: public void putAll(Map<? extends String, ? extends Field> t) {
132: for (Map.Entry<? extends String, ? extends Field> p : t
133: .entrySet()) {
134: put(p.getKey(), p.getValue());
135: }
136: }
137:
138: private Field remove(Integer i) {
139: if (i != null) {
140: Integer count = countMap.get(i) - 1;
141: if (count == 0) {
142: countMap.remove(i);
143: valueMap.remove(keyMap.get(i));
144: keyMap.remove(i);
145: } else {
146: countMap.put(i, count);
147: }
148: }
149: return map.remove(i);
150: }
151:
152: public Field remove(Object key) {
153: Integer i = keyMap.get(key);
154: return remove(i);
155: }
156:
157: public int size() {
158: return map.size();
159: }
160:
161: public Collection<Field> values() {
162: return map.values();
163: }
164:
165: }
166:
167: public boolean add(Entry o) {
168: return list.contains(o);
169: }
170:
171: public Entry createEntry() {
172: Entry e = new EntryImpl();
173: list.add(e);
174: return e;
175: }
176:
177: public void add(int index, Entry element) {
178: throw new UnsupportedOperationException();
179: }
180:
181: public boolean addAll(Collection<? extends Entry> c) {
182: throw new UnsupportedOperationException();
183: }
184:
185: public boolean addAll(int index, Collection<? extends Entry> c) {
186: throw new UnsupportedOperationException();
187: }
188:
189: public void clear() {
190: list.clear();
191: keyMap.clear();
192: valueMap.clear();
193: }
194:
195: public boolean contains(Object o) {
196: return list.contains(o);
197: }
198:
199: public boolean containsAll(Collection<?> c) {
200: throw new UnsupportedOperationException();
201: }
202:
203: public Entry get(int index) {
204: return list.get(index);
205: }
206:
207: public int indexOf(Object o) {
208: return list.indexOf(o);
209: }
210:
211: public boolean isEmpty() {
212: return list.isEmpty();
213: }
214:
215: public Iterator<Entry> iterator() {
216: return list.iterator();
217: }
218:
219: public int lastIndexOf(Object o) {
220: return list.lastIndexOf(o);
221: }
222:
223: public ListIterator<Entry> listIterator() {
224: return list.listIterator();
225: }
226:
227: public ListIterator<Entry> listIterator(int index) {
228: return list.listIterator(index);
229: }
230:
231: /**
232: * removes Object from collection end clears the object.
233: * @param o object to remove
234: * @return true on success
235: */
236: public boolean remove(Object o) {
237: boolean ret = false;
238: if (o instanceof EntryImpl) {
239: EntryImpl e = (EntryImpl) o;
240: e.clear();
241: ret = list.remove(o);
242: }
243: return ret;
244: }
245:
246: public Entry remove(int index) {
247: Entry ret = list.get(index);
248: if (!remove(list.get(index))) {
249: ret = null;
250: }
251: return ret;
252: }
253:
254: public boolean removeAll(Collection<?> c) {
255: throw new UnsupportedOperationException();
256: }
257:
258: public boolean retainAll(Collection<?> c) {
259: throw new UnsupportedOperationException();
260: }
261:
262: public Entry set(int index, Entry element) {
263: throw new UnsupportedOperationException();
264: }
265:
266: public int size() {
267: return list.size();
268: }
269:
270: public List<Entry> subList(int fromIndex, int toIndex) {
271: return list.subList(fromIndex, toIndex);
272: }
273:
274: public Object[] toArray() {
275: return list.toArray();
276: }
277:
278: public <T> T[] toArray(T[] a) {
279: return list.toArray(a);
280: }
281:
282: private void readObject(java.io.ObjectInputStream in)
283: throws IOException, ClassNotFoundException {
284: // forst initialize the non transient part
285: in.defaultReadObject();
286: // construct the transient revers mapping
287: for (String key : keyMap.keySet()) {
288: valueMap.put(keyMap.get(key), key);
289: }
290: // construct reference count
291: for (Entry e : list) {
292:
293: for (Integer key : ((EntryImpl) e).tokenSet()) {
294: if (countMap.containsKey(key)) {
295: countMap.put(key, countMap.get(key) + 1);
296:
297: } else {
298: countMap.put(key, 1);
299:
300: }
301: }
302: }
303: }
304:
305: public int getHits() {
306: return hits;
307: }
308:
309: public void setHits(int hits) {
310: this.hits = hits;
311: }
312: }
|