MajoritySelectorpublic class MajoritySelector extends BaseSelectorContainer This selector is here just to shake up your thinking a bit. Don't get
too caught up in boolean, there are other ways you can evaluate a
collection of selectors. This one takes a vote of the selectors it
contains, and majority wins. You could also have an "all-but-one"
selector, a "weighted-average" selector, and so on. These are left
as exercises for the reader (as are the usecases where this would
be necessary). |
Fields Summary |
---|
private boolean | allowtie |
Constructors Summary |
---|
public MajoritySelector()Default constructor.
|
Methods Summary |
---|
public boolean | isSelected(java.io.File basedir, java.lang.String filename, java.io.File file)Returns true (the file is selected) if most of the other selectors
agree. In case of a tie, go by the allowtie setting. That defaults
to true, meaning in case of a tie, the file is selected.
validate();
int yesvotes = 0;
int novotes = 0;
Enumeration e = selectorElements();
boolean result;
while (e.hasMoreElements()) {
result = ((FileSelector) e.nextElement()).isSelected(basedir,
filename, file);
if (result) {
yesvotes = yesvotes + 1;
} else {
novotes = novotes + 1;
}
}
if (yesvotes > novotes) {
return true;
} else if (novotes > yesvotes) {
return false;
}
// At this point, we know we have a tie.
return allowtie;
| public void | setAllowtie(boolean tiebreaker)A attribute to specify what will happen if number
of yes votes is the same as the number of no votes
defaults to true
allowtie = tiebreaker;
| public java.lang.String | toString()
StringBuffer buf = new StringBuffer();
if (hasSelectors()) {
buf.append("{majorityselect: ");
buf.append(super.toString());
buf.append("}");
}
return buf.toString();
|
|