import java.util.*; /** * CharBagI.java * * * Created: Thur Jan 30 2003 * *
A bag of characters,
* i.e., an unordered collection of characters,
* that may contain some character some number of times.
* Has an iterator.
*
* @author Stuart C. Shapiro
*/
public class CharBagI extends AbstractCollection {
/**
* The real object, for which this CharBagI
is a proxy object.
*/
private StringBuffer chars;
/**
* Creates a new empty CharBagI
instance.
*
*/
public CharBagI (){
super();
chars = new StringBuffer();
}
/**
* Creates a new CharBagI
instance
* containing all the chars of b
.
*
* @param b a StringBuffer
value
*/
public CharBagI (StringBuffer b) {
this();
chars.append(b);
}
/**
* Adds c
to this bag of characters.
*
* @param c a char
value
* @return true
*/
public boolean add(char c) {
chars.append(c);
return true;
}
/**
* Returns the index of c
in the chars of this bag, or -1.
*
* @param c a char
value
* @return the index of c
, if it is in this bag of characters;
* otherwise, -1
*/
private int indexOf(char c) {
return chars.indexOf("" + c);
}
/**
* Removes char
from this bag of characters.
*
* post: this == this@pre->excluding(c)
*
* @param c a char
value
* @return true
if c
was in this@pre;
* false
otherwise
*/
public boolean remove(char c) {
int i = indexOf(c);
if (i < 0) {return false;}
chars.deleteCharAt(i);
return true;
}
/**
* Tests if c
is in this bag of characters.
*
* @param c a char
value
* @return true
if this->includes(c), else false
*/
public boolean contains(char c) {
return indexOf(c) >= 0;
}
/**
* Tests if this bag of characters is empty
*
* @return true
if this is empty, else false
*/
public boolean isEmpty() {
return chars.length() == 0;
}
/**
* Returns the number of characters in this bag of characters.
*
* @return the number of characters in this bag of characters.
*/
public int size() {
return chars.length();
}
/**
* Tests whether this bag and b
have the same characters
* the same number of times.
*
* @param b a CharBagI
value
* @return true
* if this bag and b
have the same characters
* the same number of times.
* else false
*/
public boolean equals(CharBagI b) {
if (size() != b.size()) {return false;}
CharBagI tmp = new CharBagI(b.chars);
for (int i=0; i
* Produces Characters instead of chars,
* because iterators must produce Objects.
*
*/
private class CharBagIIterator implements Iterator{
private int next = 0;
/**
* Creates a new CharBagIIterator
instance.
*
*/
public CharBagIIterator() {
super();
}
/**
* Test for whether the iterator is finished with this CharBagI.
*
* @return true if there are more chars in this CharBagI;
* false if this iterator is finished.
*/
public boolean hasNext() {
return next < chars.length();
}
/**
* Generator of Characters from this CharBagI.
*
* @return a Character wrapping the next char in this CharBagI.
*/
public Object next() {
if (!hasNext()) {throw new NoSuchElementException();}
return new Character(chars.charAt(next++));
}
/**
* Remove the char that had been the last one returned by next().
*/
public void remove() {
if (next < 1) {
throw new IllegalStateException("Nothing to remove.");
}
chars.delete(next-1, next--);
}
} // CharBagIIterator
public static void main (String[] args) {
CharBagI b = new CharBagI();
b.add('a');
b.add('b');
b.add('c');
b.add('d');
Iterator it = b.iterator();
while (it.hasNext()) System.out.println(((Character)it.next()).charValue());
} // end of main ()
}// CharBagI