01: /*
02: * Copyright (C) 2006 Joe Walnes.
03: * Copyright (C) 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 28. June 2006 by Guilherme Silveira
11: */
12: package com.thoughtworks.xstream.persistence;
13:
14: import java.util.AbstractSet;
15: import java.util.Iterator;
16:
17: /**
18: * A persistent set implementation.
19: *
20: * @author Guilherme Silveira
21: */
22: public class XmlSet extends AbstractSet {
23:
24: private final XmlMap map;
25:
26: public XmlSet(StreamStrategy streamStrategy) {
27: this .map = new XmlMap(streamStrategy);
28: }
29:
30: public Iterator iterator() {
31: return map.values().iterator();
32: }
33:
34: public int size() {
35: return map.size();
36: }
37:
38: public boolean add(Object o) {
39: if (map.containsValue(o)) {
40: return false;
41: } else {
42: // not-synchronized!
43: map.put(findEmptyKey(), o);
44: return true;
45: }
46: }
47:
48: private String findEmptyKey() {
49: long i = System.currentTimeMillis();
50: while (map.containsKey("" + i)) {
51: i++;
52: }
53: return "" + i;
54: }
55:
56: }
|