1 11 package org.eclipse.core.runtime.jobs; 12 13 import java.util.ArrayList ; 14 15 30 public class MultiRule implements ISchedulingRule { 31 private ISchedulingRule[] rules; 32 33 43 public static ISchedulingRule combine(ISchedulingRule[] ruleArray) { 44 ISchedulingRule result = null; 45 for (int i = 0; i < ruleArray.length; i++) { 46 if (ruleArray[i] == null) 47 continue; 48 if (result == null) { 49 result = ruleArray[i]; 50 continue; 51 } 52 result = combine(result, ruleArray[i]); 53 } 54 return result; 55 } 56 57 67 public static ISchedulingRule combine(ISchedulingRule rule1, ISchedulingRule rule2) { 68 if (rule1 == rule2) 69 return rule1; 70 if (rule1 == null) 71 return rule2; 72 if (rule2 == null) 73 return rule1; 74 if (rule1.contains(rule2)) 75 return rule1; 76 if (rule2.contains(rule1)) 77 return rule2; 78 MultiRule result = new MultiRule(); 79 result.rules = new ISchedulingRule[] {rule1, rule2}; 80 if (rule1 instanceof MultiRule || rule2 instanceof MultiRule) 82 result.rules = flatten(result.rules); 83 return result; 84 } 85 86 90 private static ISchedulingRule[] flatten(ISchedulingRule[] nestedRules) { 91 ArrayList myRules = new ArrayList (nestedRules.length); 92 for (int i = 0; i < nestedRules.length; i++) { 93 if (nestedRules[i] instanceof MultiRule) { 94 ISchedulingRule[] children = ((MultiRule) nestedRules[i]).getChildren(); 95 for (int j = 0; j < children.length; j++) 96 myRules.add(children[j]); 97 } else { 98 myRules.add(nestedRules[i]); 99 } 100 } 101 return (ISchedulingRule[]) myRules.toArray(new ISchedulingRule[myRules.size()]); 102 } 103 104 109 public MultiRule(ISchedulingRule[] nestedRules) { 110 this.rules = flatten(nestedRules); 111 } 112 113 117 private MultiRule() { 118 } 120 121 125 public ISchedulingRule[] getChildren() { 126 return (ISchedulingRule[]) rules.clone(); 127 } 128 129 132 public boolean contains(ISchedulingRule rule) { 133 if (this == rule) 134 return true; 135 if (rule instanceof MultiRule) { 136 ISchedulingRule[] otherRules = ((MultiRule) rule).getChildren(); 137 for (int other = 0; other < otherRules.length; other++) { 139 boolean found = false; 140 for (int mine = 0; !found && mine < rules.length; mine++) 141 found = rules[mine].contains(otherRules[other]); 142 if (!found) 143 return false; 144 } 145 return true; 146 } 147 for (int i = 0; i < rules.length; i++) 148 if (rules[i].contains(rule)) 149 return true; 150 return false; 151 } 152 153 156 public boolean isConflicting(ISchedulingRule rule) { 157 if (this == rule) 158 return true; 159 if (rule instanceof MultiRule) { 160 ISchedulingRule[] otherRules = ((MultiRule) rule).getChildren(); 161 for (int j = 0; j < otherRules.length; j++) 162 for (int i = 0; i < rules.length; i++) 163 if (rules[i].isConflicting(otherRules[j])) 164 return true; 165 } else { 166 for (int i = 0; i < rules.length; i++) 167 if (rules[i].isConflicting(rule)) 168 return true; 169 } 170 return false; 171 } 172 173 176 public String toString() { 177 StringBuffer buffer = new StringBuffer (); 178 buffer.append("MultiRule["); int last = rules.length - 1; 180 for (int i = 0; i < rules.length; i++) { 181 buffer.append(rules[i]); 182 if (i != last) 183 buffer.append(','); 184 } 185 buffer.append(']'); 186 return buffer.toString(); 187 } 188 } 189 | Popular Tags |