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. package org.apache.tools.ant.types.selectors;
  18. import java.io.File;
  19. import java.util.Enumeration;
  20. /**
  21. * This selector is here just to shake up your thinking a bit. Don't get
  22. * too caught up in boolean, there are other ways you can evaluate a
  23. * collection of selectors. This one takes a vote of the selectors it
  24. * contains, and majority wins. You could also have an "all-but-one"
  25. * selector, a "weighted-average" selector, and so on. These are left
  26. * as exercises for the reader (as are the usecases where this would
  27. * be necessary).
  28. *
  29. * @since 1.5
  30. */
  31. public class MajoritySelector extends BaseSelectorContainer {
  32. private boolean allowtie = true;
  33. /**
  34. * Default constructor.
  35. */
  36. public MajoritySelector() {
  37. }
  38. /**
  39. * @return a string describing this object
  40. */
  41. public String toString() {
  42. StringBuffer buf = new StringBuffer();
  43. if (hasSelectors()) {
  44. buf.append("{majorityselect: ");
  45. buf.append(super.toString());
  46. buf.append("}");
  47. }
  48. return buf.toString();
  49. }
  50. /**
  51. * A attribute to specify what will happen if number
  52. * of yes votes is the same as the number of no votes
  53. * defaults to true
  54. *
  55. * @param tiebreaker the value to give if there is a tie
  56. */
  57. public void setAllowtie(boolean tiebreaker) {
  58. allowtie = tiebreaker;
  59. }
  60. /**
  61. * Returns true (the file is selected) if most of the other selectors
  62. * agree. In case of a tie, go by the allowtie setting. That defaults
  63. * to true, meaning in case of a tie, the file is selected.
  64. *
  65. * @param basedir the base directory the scan is being done from
  66. * @param filename is the name of the file to check
  67. * @param file is a java.io.File object for the filename that the selector
  68. * can use
  69. * @return whether the file should be selected or not
  70. */
  71. public boolean isSelected(File basedir, String filename, File file) {
  72. validate();
  73. int yesvotes = 0;
  74. int novotes = 0;
  75. Enumeration e = selectorElements();
  76. boolean result;
  77. while (e.hasMoreElements()) {
  78. result = ((FileSelector) e.nextElement()).isSelected(basedir,
  79. filename, file);
  80. if (result) {
  81. yesvotes = yesvotes + 1;
  82. } else {
  83. novotes = novotes + 1;
  84. }
  85. }
  86. if (yesvotes > novotes) {
  87. return true;
  88. } else if (novotes > yesvotes) {
  89. return false;
  90. }
  91. // At this point, we know we have a tie.
  92. return allowtie;
  93. }
  94. }