1 20 21 package net.innig.macker.rule; 22 23 import net.innig.macker.structure.ClassInfo; 24 import net.innig.macker.util.IncludeExcludeLogic; 25 import net.innig.macker.util.IncludeExcludeNode; 26 27 public final class CompositePattern 28 implements Pattern 29 { 30 34 public static Pattern create(CompositePatternType type, Pattern head, Pattern child, Pattern next) 35 { 36 if(type == null) 37 throw new NullPointerException ("type parameter cannot be null"); 38 39 if(head == null && child == null && next == null) 40 return (type == CompositePatternType.INCLUDE) ? Pattern.ALL : Pattern.NONE; 41 if(head == null && child == null) 42 return create(type, next, null, null); 43 if(head == null) 44 return create(type, child, null, next); 45 if(type == CompositePatternType.INCLUDE && child == null && next == null) 46 return head; 47 48 return new CompositePattern(type, head, child, next); 49 } 50 51 private CompositePattern(CompositePatternType type, Pattern head, Pattern child, Pattern next) 52 { 53 this.type = type; 54 this.head = head; 55 this.child = child; 56 this.next = next; 57 } 58 59 63 public CompositePatternType getType() 64 { return type; } 65 66 public Pattern getHead() 67 { return head; } 68 69 public Pattern getChild() 70 { return child; } 71 72 public Pattern getNext() 73 { return next; } 74 75 private final CompositePatternType type; 76 private final Pattern head, child, next; 77 78 82 public boolean matches(EvaluationContext context, ClassInfo classInfo) 83 throws RulesException 84 { return IncludeExcludeLogic.apply(makeIncludeExcludeNode(this, context, classInfo)); } 85 86 private static IncludeExcludeNode makeIncludeExcludeNode( 87 final Pattern pat, 88 final EvaluationContext context, 89 final ClassInfo classInfo) 90 { 91 if(pat == null) 92 return null; 93 94 final boolean include; 95 final Pattern head, child, next; 96 97 if(pat instanceof CompositePattern) 98 { 99 CompositePattern compositePat = (CompositePattern) pat; 100 include = (compositePat.getType() == CompositePatternType.INCLUDE); 101 head = compositePat.getHead(); 102 child = compositePat.getChild(); 103 next = compositePat.getNext(); 104 } 105 else 106 { 107 include = true; 108 head = pat; 109 child = next = null; 110 } 111 112 return new IncludeExcludeNode() 113 { 114 public boolean isInclude() 115 { return include; } 116 117 public boolean matches() 118 throws RulesException 119 { return head.matches(context, classInfo); } 120 121 public IncludeExcludeNode getChild() 122 { return makeIncludeExcludeNode(child, context, classInfo); } 123 124 public IncludeExcludeNode getNext() 125 { return makeIncludeExcludeNode(next, context, classInfo); } 126 }; 127 } 128 129 133 public String toString() 134 { 135 return "(" + type + ' ' + head 136 + (child == null ? "" : " + " + child) + ')' 137 + ( next == null ? "" : ", " + next); 138 } 139 } 140 141 142 143 144 145 | Popular Tags |