KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > innig > macker > rule > CompositePattern


1 /*______________________________________________________________________________
2  *
3  * Macker http://innig.net/macker/
4  *
5  * Copyright 2002 Paul Cantrell
6  *
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License version 2, as published by the
9  * Free Software Foundation. See the file LICENSE.html for more information.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the license for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
17  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
18  *______________________________________________________________________________
19  */

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     //--------------------------------------------------------------------------
31
// Constructors
32
//--------------------------------------------------------------------------
33

34     public static Pattern create(CompositePatternType type, Pattern head, Pattern child, Pattern next)
35         {
36         if(type == null)
37             throw new NullPointerException JavaDoc("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     //--------------------------------------------------------------------------
60
// Properties
61
//--------------------------------------------------------------------------
62

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     //--------------------------------------------------------------------------
79
// Evaluation
80
//--------------------------------------------------------------------------
81

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     //--------------------------------------------------------------------------
130
// Object
131
//--------------------------------------------------------------------------
132

133     public String JavaDoc toString()
134         {
135         return "(" + type + ' ' + head
136             + (child == null ? "" : " + " + child) + ')'
137             + ( next == null ? "" : ", " + next);
138         }
139     }
140
141
142
143
144
145
Popular Tags