KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > selectors > MajoritySelector


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.types.selectors;
20
21 import java.io.File JavaDoc;
22 import java.util.Enumeration JavaDoc;
23
24 /**
25  * This selector is here just to shake up your thinking a bit. Don't get
26  * too caught up in boolean, there are other ways you can evaluate a
27  * collection of selectors. This one takes a vote of the selectors it
28  * contains, and majority wins. You could also have an "all-but-one"
29  * selector, a "weighted-average" selector, and so on. These are left
30  * as exercises for the reader (as are the usecases where this would
31  * be necessary).
32  *
33  * @since 1.5
34  */

35 public class MajoritySelector extends BaseSelectorContainer {
36
37     private boolean allowtie = true;
38
39     /**
40      * Default constructor.
41      */

42     public MajoritySelector() {
43     }
44
45     /**
46      * @return a string describing this object
47      */

48     public String JavaDoc toString() {
49         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
50         if (hasSelectors()) {
51             buf.append("{majorityselect: ");
52             buf.append(super.toString());
53             buf.append("}");
54         }
55         return buf.toString();
56     }
57
58     /**
59      * A attribute to specify what will happen if number
60      * of yes votes is the same as the number of no votes
61      * defaults to true
62      *
63      * @param tiebreaker the value to give if there is a tie
64      */

65     public void setAllowtie(boolean tiebreaker) {
66         allowtie = tiebreaker;
67     }
68
69     /**
70      * Returns true (the file is selected) if most of the other selectors
71      * agree. In case of a tie, go by the allowtie setting. That defaults
72      * to true, meaning in case of a tie, the file is selected.
73      *
74      * @param basedir the base directory the scan is being done from
75      * @param filename is the name of the file to check
76      * @param file is a java.io.File object for the filename that the selector
77      * can use
78      * @return whether the file should be selected or not
79      */

80     public boolean isSelected(File JavaDoc basedir, String JavaDoc filename, File JavaDoc file) {
81         validate();
82         int yesvotes = 0;
83         int novotes = 0;
84         Enumeration JavaDoc e = selectorElements();
85         boolean result;
86
87         while (e.hasMoreElements()) {
88             result = ((FileSelector) e.nextElement()).isSelected(basedir,
89                     filename, file);
90             if (result) {
91                 yesvotes = yesvotes + 1;
92             } else {
93                 novotes = novotes + 1;
94             }
95         }
96         if (yesvotes > novotes) {
97             return true;
98         } else if (novotes > yesvotes) {
99             return false;
100         }
101         // At this point, we know we have a tie.
102
return allowtie;
103     }
104 }
105
106
Popular Tags