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: import net.sourceforge.chaperon.common.Decoder;
012:
013: /**
014: * This class represents a set of char values, which means there doesn't exist multiple instances
015: * of values.
016: *
017: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
018: * @version CVS $Id: SortedCharSet.java,v 1.3 2003/12/29 14:48:12 benedikta Exp $
019: */
020: public class SortedCharSet {
021: private char elementCount = 0;
022: private char[] list = new char[10];
023:
024: //private char dummy;
025:
026: /**
027: * Creates an empty set of char values.
028: */
029: public SortedCharSet() {
030: }
031:
032: /**
033: * Add a value to this set.
034: *
035: * @param value Char value.
036: *
037: * @return Index of this value.
038: */
039: public void addChar(char value) {
040: if (list.length < (elementCount + 1)) {
041: char[] newList = new char[elementCount + 10];
042: System.arraycopy(list, 0, newList, 0, list.length);
043: list = newList;
044: }
045:
046: list[elementCount++] = value;
047: sort();
048: }
049:
050: /**
051: * Add the values of an array to this set.
052: *
053: * @param array Array of char values.
054: */
055: public void addChar(char[] array) {
056: if (list.length < (elementCount + array.length)) {
057: char[] newList = new char[elementCount + array.length];
058: System.arraycopy(list, 0, newList, 0, list.length);
059: list = newList;
060: }
061:
062: System.arraycopy(array, 0, list, elementCount, array.length);
063: elementCount += array.length;
064: sort();
065: }
066:
067: /**
068: * Removes a value giving by an index.
069: *
070: * @param index Index of the char value.
071: */
072: public void removeChar(char index) {
073: if (index >= elementCount)
074: throw new ArrayIndexOutOfBoundsException(index);
075:
076: elementCount--;
077: if (index < elementCount)
078: System.arraycopy(list, index + 1, list, index, elementCount
079: - index);
080:
081: list[elementCount] = 0;
082: }
083:
084: /**
085: * Return a value from this set given by an index.
086: *
087: * @param index Index of the value.
088: *
089: * @return Char value.
090: */
091: public char getChar(int index) {
092: if (index >= elementCount)
093: throw new ArrayIndexOutOfBoundsException(index);
094:
095: return list[index];
096: }
097:
098: public char[] getChar() {
099: char[] newList = new char[elementCount];
100: System.arraycopy(list, 0, newList, 0, elementCount);
101: return newList;
102: }
103:
104: /**
105: * Returns the count of value in this set.
106: *
107: * @return Count of char values.
108: */
109: public char getCharCount() {
110: return elementCount;
111: }
112:
113: /**
114: * Return the index of a value, otherwise -1
115: *
116: * @param value Value, which should be found in this set.
117: *
118: * @return Index of this value.
119: */
120: public int indexOf(char value) {
121: for (char i = 0; i < elementCount; i++)
122: if (list[i] == value)
123: return i;
124:
125: return -1;
126: }
127:
128: /**
129: * If the list contains a value.
130: *
131: * @param value Value, which should be found in this set
132: *
133: * @return True, if this set contains the value.
134: */
135: public boolean contains(char value) {
136: for (char i = 0; i < elementCount; i++)
137: if (list[i] == value)
138: return true;
139:
140: return false;
141: }
142:
143: /**
144: * If this set contains no values.
145: *
146: * @return True, if this set is empty.
147: */
148: public boolean isEmpty() {
149: return (elementCount <= 0);
150: }
151:
152: /**
153: * Removes all values from this set
154: */
155: public void clear() {
156: elementCount = 0;
157: }
158:
159: /**
160: * Sort content by a QuickSort algorithm, and eliminate multiple instances.
161: */
162: private void sort() {
163: char c;
164: boolean modified = false;
165: do {
166: modified = false;
167:
168: for (int i = 1; i < elementCount; i++)
169: if (list[i - 1] > list[i]) {
170: c = list[i - 1];
171: list[i - 1] = list[i];
172: list[i] = c;
173:
174: modified = true;
175: } else if (list[i - 1] == list[i]) // eliminate multiple instances
176: {
177: if (i < (elementCount - 1))
178: list[i] = list[--elementCount];
179: else
180: elementCount--;
181:
182: modified = true;
183: }
184: } while (modified);
185: }
186:
187: /**
188: * Return a string representation of the collection.
189: *
190: * @return String representation of the collection.
191: */
192: public String toString() {
193: StringBuffer buffer = new StringBuffer();
194:
195: buffer.append("[");
196: for (char i = 0; i < elementCount; i++) {
197: buffer.append(Decoder.toChar(list[i]));
198: if (i < (elementCount - 1))
199: buffer.append(",");
200: }
201:
202: buffer.append("]");
203: return buffer.toString();
204: }
205: }
|