summaryrefslogtreecommitdiff
path: root/songdbj/org/tritonus/share/StringHashedSet.java
diff options
context:
space:
mode:
Diffstat (limited to 'songdbj/org/tritonus/share/StringHashedSet.java')
-rw-r--r--songdbj/org/tritonus/share/StringHashedSet.java112
1 files changed, 112 insertions, 0 deletions
diff --git a/songdbj/org/tritonus/share/StringHashedSet.java b/songdbj/org/tritonus/share/StringHashedSet.java
new file mode 100644
index 0000000000..8e244665fb
--- /dev/null
+++ b/songdbj/org/tritonus/share/StringHashedSet.java
@@ -0,0 +1,112 @@
1/*
2 * StringHashedSet.java
3 *
4 * This file is part of Tritonus: http://www.tritonus.org/
5 */
6
7/*
8 * Copyright (c) 2000 by Florian Bomers <http://www.bomers.de>
9 *
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Library General Public License as published
13 * by the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Library General Public License for more details.
20 *
21 * You should have received a copy of the GNU Library General Public
22 * License along with this program; if not, write to the Free Software
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 *
25 */
26
27/*
28|<--- this code is formatted to fit into 80 columns --->|
29*/
30
31package org.tritonus.share;
32
33import java.util.ArrayList;
34import java.util.Collection;
35import java.util.Iterator;
36
37import org.tritonus.share.ArraySet;
38
39
40/**
41 * A set where the elements are uniquely referenced by their
42 * string representation as given by the objects toString()
43 * method. No 2 objects with the same toString() can
44 * be in the set.
45 * <p>
46 * The <code>contains(Object elem)</code> and <code>get(Object elem)</code>
47 * methods can be called with Strings as <code>elem</code> parameter.
48 * For <code>get(Object elem)</code>, the object that has been added
49 * is returned, and not its String representation.
50 * <p>
51 * Though it's possible to store
52 * Strings as objects in this class, it doesn't make sense
53 * as you could use ArraySet for that equally well.
54 * <p>
55 * You shouldn't use the ArrayList specific functions
56 * like those that take index parameters.
57 * <p>
58 * It is not possible to add <code>null</code> elements.
59 */
60
61public class StringHashedSet<E> extends ArraySet<E>
62{
63 public StringHashedSet()
64 {
65 super();
66 }
67
68 public StringHashedSet(Collection<E> c)
69 {
70 super(c);
71 }
72
73 public boolean add(E elem)
74 {
75 if (elem==null) {
76 return false;
77 }
78 return super.add(elem);
79 }
80
81 public boolean contains(Object elem)
82 {
83 if (elem==null) {
84 return false;
85 }
86 String comp=elem.toString();
87 Iterator<E> it=iterator();
88 while (it.hasNext()) {
89 if (comp.equals(it.next().toString())) {
90 return true;
91 }
92 }
93 return false;
94 }
95
96 public E get(E elem) {
97 if (elem==null) {
98 return null;
99 }
100 String comp=elem.toString();
101 Iterator<E> it=iterator();
102 while (it.hasNext()) {
103 E thisElem=it.next();
104 if (comp.equals(thisElem.toString())) {
105 return thisElem;
106 }
107 }
108 return null;
109 }
110}
111
112/*** StringHashedSet.java ***/