KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > selectors > Majority


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 package org.apache.tools.ant.types.resources.selectors;
19
20 import java.util.Iterator JavaDoc;
21
22 import org.apache.tools.ant.types.Resource;
23
24 /**
25  * Majority ResourceSelector.
26  * @since Ant 1.7
27  */

28 public class Majority
29     extends ResourceSelectorContainer implements ResourceSelector {
30
31     private boolean tie = true;
32
33     /**
34      * Default constructor.
35      */

36     public Majority() {
37     }
38
39     /**
40      * Convenience constructor.
41      * @param r the ResourceSelector[] to add.
42      */

43     public Majority(ResourceSelector[] r) {
44         super(r);
45     }
46
47     /**
48      * Set whether ties are allowed.
49      * @param b whether a tie is a pass.
50      */

51     public synchronized void setAllowtie(boolean b) {
52         tie = b;
53     }
54
55     /**
56      * Return true if this Resource is selected.
57      * @param r the Resource to check.
58      * @return whether the Resource was selected.
59      */

60     public synchronized boolean isSelected(Resource r) {
61         int passed = 0;
62         int failed = 0;
63         int count = selectorCount();
64         boolean even = count % 2 == 0;
65         int threshold = count / 2;
66
67         for (Iterator JavaDoc i = getSelectors(); i.hasNext();) {
68             if (((ResourceSelector) i.next()).isSelected(r)) {
69                 ++passed;
70                 if (passed > threshold || (even && tie && passed == threshold)) {
71                     return true;
72                 }
73             } else {
74                 ++failed;
75                 if (failed > threshold || (even && !tie && failed == threshold)) {
76                     return false;
77                 }
78             }
79         }
80         //dummy
81
return false;
82     }
83
84 }
85
Popular Tags