001: /*______________________________________________________________________________
002: *
003: * Macker http://innig.net/macker/
004: *
005: * Copyright 2002 Paul Cantrell
006: *
007: * This program is free software; you can redistribute it and/or modify it under
008: * the terms of the GNU General Public License version 2, as published by the
009: * Free Software Foundation. See the file LICENSE.html for more information.
010: *
011: * This program is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the license for more details.
014: *
015: * You should have received a copy of the GNU General Public License along with
016: * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
017: * Place, Suite 330 / Boston, MA 02111-1307 / USA.
018: *______________________________________________________________________________
019: */
020:
021: package net.innig.macker.rule;
022:
023: import net.innig.macker.structure.ClassInfo;
024: import net.innig.macker.util.IncludeExcludeLogic;
025: import net.innig.macker.util.IncludeExcludeNode;
026:
027: public final class CompositePattern implements Pattern {
028: //--------------------------------------------------------------------------
029: // Constructors
030: //--------------------------------------------------------------------------
031:
032: public static Pattern create(CompositePatternType type,
033: Pattern head, Pattern child, Pattern next) {
034: if (type == null)
035: throw new NullPointerException(
036: "type parameter cannot be null");
037:
038: if (head == null && child == null && next == null)
039: return (type == CompositePatternType.INCLUDE) ? Pattern.ALL
040: : Pattern.NONE;
041: if (head == null && child == null)
042: return create(type, next, null, null);
043: if (head == null)
044: return create(type, child, null, next);
045: if (type == CompositePatternType.INCLUDE && child == null
046: && next == null)
047: return head;
048:
049: return new CompositePattern(type, head, child, next);
050: }
051:
052: private CompositePattern(CompositePatternType type, Pattern head,
053: Pattern child, Pattern next) {
054: this .type = type;
055: this .head = head;
056: this .child = child;
057: this .next = next;
058: }
059:
060: //--------------------------------------------------------------------------
061: // Properties
062: //--------------------------------------------------------------------------
063:
064: public CompositePatternType getType() {
065: return type;
066: }
067:
068: public Pattern getHead() {
069: return head;
070: }
071:
072: public Pattern getChild() {
073: return child;
074: }
075:
076: public Pattern getNext() {
077: return next;
078: }
079:
080: private final CompositePatternType type;
081: private final Pattern head, child, next;
082:
083: //--------------------------------------------------------------------------
084: // Evaluation
085: //--------------------------------------------------------------------------
086:
087: public boolean matches(EvaluationContext context,
088: ClassInfo classInfo) throws RulesException {
089: return IncludeExcludeLogic.apply(makeIncludeExcludeNode(this ,
090: context, classInfo));
091: }
092:
093: private static IncludeExcludeNode makeIncludeExcludeNode(
094: final Pattern pat, final EvaluationContext context,
095: final ClassInfo classInfo) {
096: if (pat == null)
097: return null;
098:
099: final boolean include;
100: final Pattern head, child, next;
101:
102: if (pat instanceof CompositePattern) {
103: CompositePattern compositePat = (CompositePattern) pat;
104: include = (compositePat.getType() == CompositePatternType.INCLUDE);
105: head = compositePat.getHead();
106: child = compositePat.getChild();
107: next = compositePat.getNext();
108: } else {
109: include = true;
110: head = pat;
111: child = next = null;
112: }
113:
114: return new IncludeExcludeNode() {
115: public boolean isInclude() {
116: return include;
117: }
118:
119: public boolean matches() throws RulesException {
120: return head.matches(context, classInfo);
121: }
122:
123: public IncludeExcludeNode getChild() {
124: return makeIncludeExcludeNode(child, context, classInfo);
125: }
126:
127: public IncludeExcludeNode getNext() {
128: return makeIncludeExcludeNode(next, context, classInfo);
129: }
130: };
131: }
132:
133: //--------------------------------------------------------------------------
134: // Object
135: //--------------------------------------------------------------------------
136:
137: public String toString() {
138: return "(" + type + ' ' + head
139: + (child == null ? "" : " + " + child) + ')'
140: + (next == null ? "" : ", " + next);
141: }
142: }
|