01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
21:
22: import org.apache.tools.ant.types.Resource;
23:
24: /**
25: * Majority ResourceSelector.
26: * @since Ant 1.7
27: */
28: public class Majority extends ResourceSelectorContainer implements
29: 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 i = getSelectors(); i.hasNext();) {
68: if (((ResourceSelector) i.next()).isSelected(r)) {
69: ++passed;
70: if (passed > threshold
71: || (even && tie && passed == threshold)) {
72: return true;
73: }
74: } else {
75: ++failed;
76: if (failed > threshold
77: || (even && !tie && failed == threshold)) {
78: return false;
79: }
80: }
81: }
82: //dummy
83: return false;
84: }
85:
86: }
|