source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/selectors/MajoritySelector.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 3.3 KB
Line 
1/*
2 * Copyright 2002-2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.types.selectors;
19
20import java.io.File;
21import java.util.Enumeration;
22
23/**
24 * This selector is here just to shake up your thinking a bit. Don't get
25 * too caught up in boolean, there are other ways you can evaluate a
26 * collection of selectors. This one takes a vote of the selectors it
27 * contains, and majority wins. You could also have an "all-but-one"
28 * selector, a "weighted-average" selector, and so on. These are left
29 * as exercises for the reader (as are the usecases where this would
30 * be necessary).
31 *
32 * @since 1.5
33 */
34public class MajoritySelector extends BaseSelectorContainer {
35
36 private boolean allowtie = true;
37
38 /**
39 * Default constructor.
40 */
41 public MajoritySelector() {
42 }
43
44 /**
45 * @return a string describing this object
46 */
47 public String toString() {
48 StringBuffer buf = new StringBuffer();
49 if (hasSelectors()) {
50 buf.append("{majorityselect: ");
51 buf.append(super.toString());
52 buf.append("}");
53 }
54 return buf.toString();
55 }
56
57 /**
58 * A attribute to specify what will happen if number
59 * of yes votes is the same as the number of no votes
60 * defaults to true
61 *
62 * @param tiebreaker the value to give if there is a tie
63 */
64 public void setAllowtie(boolean tiebreaker) {
65 allowtie = tiebreaker;
66 }
67
68 /**
69 * Returns true (the file is selected) if most of the other selectors
70 * agree. In case of a tie, go by the allowtie setting. That defaults
71 * to true, meaning in case of a tie, the file is selected.
72 *
73 * @param basedir the base directory the scan is being done from
74 * @param filename is the name of the file to check
75 * @param file is a java.io.File object for the filename that the selector
76 * can use
77 * @return whether the file should be selected or not
78 */
79 public boolean isSelected(File basedir, String filename, File file) {
80 validate();
81 int yesvotes = 0;
82 int novotes = 0;
83 Enumeration e = selectorElements();
84 boolean result;
85
86 while (e.hasMoreElements()) {
87 result = ((FileSelector) e.nextElement()).isSelected(basedir,
88 filename, file);
89 if (result) {
90 yesvotes = yesvotes + 1;
91 } else {
92 novotes = novotes + 1;
93 }
94 }
95 if (yesvotes > novotes) {
96 return true;
97 } else if (novotes > yesvotes) {
98 return false;
99 }
100 // At this point, we know we have a tie.
101 return allowtie;
102 }
103}
104
Note: See TracBrowser for help on using the repository browser.