Java Reference
In-Depth Information
routines to be used by extended sorting classes when they need to ex-
amine data or perform comparisons and swaps.
When you design a class, you can decide whether to trust its extended
classes. The SortDouble class is designed not to trust them, and that is
generally the best way to design classes for others to extend. A guarded
design not only prevents malicious use, it also prevents bugs.
SortDouble carefully restricts access to each member to the appropriate
level. It uses final on all its non- abstract methods. These factors are
all part of the contract of the SortDouble class, which includes protect-
ing the measurement of the sort algorithm from tampering. Making the
methods final ensures that no extended class overrides these methods
to change behavior, and also allows the compiler and runtime system to
make them as efficient as possible.
SortMetrics objects describe the cost of a particular sorting run. The
class has three public fields. Its only task is to communicate data,
so there is no need to hide that data behind accessor methods.
SortDouble.getMetrics returns a copy of the data so that it doesn't give
out a reference to its internal data. This prevents both the code that cre-
ates SortDouble objects and the code in the extended classes from chan-
ging the data. Here is the SortMetrics class:
final class SortMetrics implements Cloneable {
public long probeCnt, // simple data probes
compareCnt, // comparing two elements
swapCnt; // swapping two elements
public void init() {
probeCnt = swapCnt = compareCnt = 0;
}
public String toString() {
return probeCnt + " probes " +
compareCnt + " compares " +
swapCnt + " swaps";
}
 
Search WWH ::




Custom Search