THE Java™ Programming Language, Fourth Edition

(Jeff_L) #1

used.set(str.charAt(i)); // set bit for char
}


public String toString() {
String desc = "[";
for (int i = used.nextSetBit(0);
i >= 0;
i = used.nextSetBit(i+1) ) {
desc += (char) i;
}
return desc + "]";
}
}


If we pass WhichChars the string "Testing123" we get back


[ 123Teginst]


which shows each of the characters (including the spaces) that were used in the input string, and which,
incidentally, have now been sorted into numerical order. Notice how easy it is to iterate through all the set bits
in a bit set.


Exercise 22.2: The WhichChars class has a problem marking characters near the top of the Unicode range
because the high character values will leave many unused bits in the lower ranges. Use a HashSet to solve
this problem by storing Character objects for each character seen.


Exercise 22.3: Now use a HashMap to store a BitSet object for each different top byte (high 8 bits)
encountered in the input string, with each BitSet storing the low bytes that have been seen with the
particular high byte.


22.3. Observer/Observable


The Observer/Observable types provide a protocol for an arbitrary number of Observer objects to
watch for changes and events in any number of Observable objects. An Observable object subclasses
the Observable class, which provides methods to maintain a list of Observer objects that want to know
about changes in the Observable object. All objects in the "interested" list must implement the Observer
interface. When an Observable object experiences a noteworthy change or an event that Observer
objects may care about, the Observable object invokes its notifyObservers method, which invokes
each Observer object's update method.


The Observer interface consists of a single method:


public voidupdate(Observable obj, Object arg)

This method is invoked when the Observable object obj has a change or
an event to report. The arg parameter is a way to pass an arbitrary object to
describe the change or event to the observer.

The Observer/Observable mechanism is designed to be general. Each Observable class is left to
define the circumstances under which an Observer object's update method will be invoked. The
Observable object maintains a "changed" flag which subclass methods use to indicate when something of
interest has occurred.

Free download pdf