001: /*
002: * Copyright (C) Chaperon. All rights reserved.
003: * -------------------------------------------------------------------------
004: * This software is published under the terms of the Apache Software License
005: * version 1.1, a copy of which has been included with this distribution in
006: * the LICENSE file.
007: */
008:
009: package net.sourceforge.chaperon.common;
010:
011: /**
012: * This class represents a set of strings.
013: *
014: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
015: * @version CVS $Id: StringSet.java,v 1.2 2003/12/14 09:53:56 benedikta Exp $
016: */
017: public class StringSet {
018: private String[] strings = new String[0];
019:
020: public StringSet() {
021: }
022:
023: public StringSet(String[] strings) {
024: addString(strings);
025: }
026:
027: public boolean addString(String string) {
028: if (string == null)
029: return true;
030:
031: for (int i = 0; i < strings.length; i++)
032: if (strings[i].equals(string))
033: return false;
034:
035: String[] newStringSet = new String[strings.length + 1];
036: System.arraycopy(strings, 0, newStringSet, 0, strings.length);
037: newStringSet[strings.length] = string;
038: strings = newStringSet;
039:
040: return true;
041: }
042:
043: public boolean addString(StringSet set) {
044: boolean modified = false;
045:
046: for (int i = 0; i < set.strings.length; i++)
047: modified |= addString(set.strings[i]);
048:
049: return modified;
050: }
051:
052: public boolean addString(String[] set) {
053: boolean modified = false;
054:
055: for (int i = 0; i < set.length; i++)
056: modified |= addString(set[i]);
057:
058: return modified;
059: }
060:
061: /**
062: * Returns a string given by an index.
063: *
064: * @param index Index of the string
065: *
066: * @return String
067: */
068: public String getString(int index) {
069: return strings[index];
070: }
071:
072: public String[] getString() {
073: return strings;
074: }
075:
076: /**
077: * Return the count of string in this list.
078: *
079: * @return Count of string.
080: */
081: public int getStringCount() {
082: return strings.length;
083: }
084:
085: public boolean isEmpty() {
086: return strings.length == 0;
087: }
088:
089: public boolean contains(String string) {
090: for (int i = 0; i < strings.length; i++)
091: if (strings[i] == string)
092: return true;
093:
094: return false;
095: }
096:
097: public boolean equals(Object o) {
098: if (o instanceof StringSet) {
099: StringSet set = (StringSet) o;
100:
101: if (set.strings.length != strings.length)
102: return false;
103:
104: for (int i = 0; i < strings.length; i++)
105: for (int j = 0; j <= set.strings.length; j++) {
106: if (j == strings.length)
107: return false;
108:
109: if (strings[i].equals(set.strings[j]))
110: break;
111: }
112:
113: return true;
114: }
115:
116: return false;
117: }
118:
119: public String toString() {
120: StringBuffer buffer = new StringBuffer();
121:
122: buffer.append("[");
123:
124: for (int i = 0; i < strings.length; i++) {
125: buffer.append(strings[i]);
126:
127: if (i < (strings.length - 1))
128: buffer.append(",");
129: }
130:
131: buffer.append("]");
132:
133: return buffer.toString();
134: }
135: }
|