001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types.selectors;
020:
021: import java.io.File;
022: import java.util.Enumeration;
023:
024: /**
025: * This selector is here just to shake up your thinking a bit. Don't get
026: * too caught up in boolean, there are other ways you can evaluate a
027: * collection of selectors. This one takes a vote of the selectors it
028: * contains, and majority wins. You could also have an "all-but-one"
029: * selector, a "weighted-average" selector, and so on. These are left
030: * as exercises for the reader (as are the usecases where this would
031: * be necessary).
032: *
033: * @since 1.5
034: */
035: public class MajoritySelector extends BaseSelectorContainer {
036:
037: private boolean allowtie = true;
038:
039: /**
040: * Default constructor.
041: */
042: public MajoritySelector() {
043: }
044:
045: /**
046: * @return a string describing this object
047: */
048: public String toString() {
049: StringBuffer buf = new StringBuffer();
050: if (hasSelectors()) {
051: buf.append("{majorityselect: ");
052: buf.append(super .toString());
053: buf.append("}");
054: }
055: return buf.toString();
056: }
057:
058: /**
059: * A attribute to specify what will happen if number
060: * of yes votes is the same as the number of no votes
061: * defaults to true
062: *
063: * @param tiebreaker the value to give if there is a tie
064: */
065: public void setAllowtie(boolean tiebreaker) {
066: allowtie = tiebreaker;
067: }
068:
069: /**
070: * Returns true (the file is selected) if most of the other selectors
071: * agree. In case of a tie, go by the allowtie setting. That defaults
072: * to true, meaning in case of a tie, the file is selected.
073: *
074: * @param basedir the base directory the scan is being done from
075: * @param filename is the name of the file to check
076: * @param file is a java.io.File object for the filename that the selector
077: * can use
078: * @return whether the file should be selected or not
079: */
080: public boolean isSelected(File basedir, String filename, File file) {
081: validate();
082: int yesvotes = 0;
083: int novotes = 0;
084: Enumeration e = selectorElements();
085: boolean result;
086:
087: while (e.hasMoreElements()) {
088: result = ((FileSelector) e.nextElement()).isSelected(
089: basedir, filename, file);
090: if (result) {
091: yesvotes = yesvotes + 1;
092: } else {
093: novotes = novotes + 1;
094: }
095: }
096: if (yesvotes > novotes) {
097: return true;
098: } else if (novotes > yesvotes) {
099: return false;
100: }
101: // At this point, we know we have a tie.
102: return allowtie;
103: }
104: }
|