From 173d95a166bcf3b7a363a1d06f292bf6a90a1f42 Mon Sep 17 00:00:00 2001 From: Uwe Schindler Date: Sun, 18 Oct 2009 10:12:26 +0000 Subject: [PATCH] LUCENE-1989: Generify CharArraySet git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@826395 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/analysis/CharArraySet.java | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/lucene/analysis/CharArraySet.java b/src/java/org/apache/lucene/analysis/CharArraySet.java index 587ccad10aa..5d350caa11c 100644 --- a/src/java/org/apache/lucene/analysis/CharArraySet.java +++ b/src/java/org/apache/lucene/analysis/CharArraySet.java @@ -31,9 +31,18 @@ import java.util.Iterator; * etc. It is designed to be quick to test if a char[] * is in the set without the necessity of converting it * to a String first. + *

+ * Please note: This class implements {@link Set} but + * does not behave like it should in all cases. The generic type is + * {@code Set}, because you can add any object to it, + * that has a string representation. The add methods will use + * {@link Object#toString} and store the result using a {@code char[]} + * buffer. The same behaviour have the {@code contains()} methods. + * The {@link #iterator()} returns an {@code Iterator}. + * For type safety also {@link #stringIterator()} is provided. */ -public class CharArraySet extends AbstractSet { +public class CharArraySet extends AbstractSet { private final static int INIT_SIZE = 8; private char[][] entries; private int count; @@ -49,11 +58,12 @@ public class CharArraySet extends AbstractSet { entries = new char[size][]; } - /** Create set from a Collection of char[] or String */ - public CharArraySet(Collection c, boolean ignoreCase) { + /** Create set from a Collection of char[] or String */ + public CharArraySet(Collection c, boolean ignoreCase) { this(c.size(), ignoreCase); addAll(c); } + /** Create set from entries */ private CharArraySet(char[][] entries, boolean ignoreCase, int count){ this.entries = entries; @@ -223,7 +233,7 @@ public class CharArraySet extends AbstractSet { public boolean contains(Object o) { if (o instanceof char[]) { - char[] text = (char[])o; + final char[] text = (char[])o; return contains(text, 0, text.length); } return contains(o.toString()); @@ -258,7 +268,7 @@ public class CharArraySet extends AbstractSet { /** The Iterator for this set. Strings are constructed on the fly, so * use nextCharArray for more efficient access. */ - public class CharArraySetIterator implements Iterator { + public class CharArraySetIterator implements Iterator { int pos=-1; char[] next; CharArraySetIterator() { @@ -284,7 +294,7 @@ public class CharArraySet extends AbstractSet { /** Returns the next String, as a Set would... * use nextCharArray() for better efficiency. */ - public Object next() { + public String next() { return new String(nextCharArray()); } @@ -293,10 +303,16 @@ public class CharArraySet extends AbstractSet { } } - - public Iterator iterator() { + /** returns an iterator of new allocated Strings */ + public Iterator stringIterator() { return new CharArraySetIterator(); } + + /** returns an iterator of new allocated Strings, this method violates the Set interface */ + @SuppressWarnings("unchecked") + public Iterator iterator() { + return (Iterator) stringIterator(); + } /** * Efficient unmodifiable {@link CharArraySet}. This implementation does not @@ -316,7 +332,7 @@ public class CharArraySet extends AbstractSet { throw new UnsupportedOperationException(); } - public boolean addAll(Collection coll) { + public boolean addAll(Collection coll) { throw new UnsupportedOperationException(); }