KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > model > extended > PatternSet


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.model.extended;
10
11 public class PatternSet
12 {
13   private PatternSetEntry first = null;
14
15   public PatternSet() {}
16
17   public PatternSet(PatternIterator pattern)
18   {
19     while (pattern.hasNext())
20       addPattern(pattern.next());
21   }
22
23   public boolean addPattern(Pattern pattern)
24   {
25     if (pattern==null)
26       throw new NullPointerException JavaDoc();
27
28     for (PatternSetEntry entry = first; entry!=null; entry = entry.next)
29       if (entry.pattern==pattern)
30         return false;
31
32     first = new PatternSetEntry(pattern, first);
33     return true;
34   }
35
36   public boolean addPattern(PatternSet set)
37   {
38     boolean modified = false;
39     for (PatternSetEntry entry = set.first; entry!=null; entry = entry.next)
40       modified |= addPattern(entry.pattern);
41
42     return modified;
43   }
44
45   public PatternIterator getPattern()
46   {
47     return new PatternSetEntryIterator(first);
48   }
49
50   public boolean contains(Pattern pattern)
51   {
52     for (PatternSetEntry entry = first; entry!=null; entry = entry.next)
53       if (entry.pattern==pattern)
54         return true;
55
56     return false;
57   }
58
59   public int getPatternCount()
60   {
61     int count = 0;
62     for (PatternSetEntry entry = first; entry!=null; entry = entry.next)
63       count++;
64
65     return count;
66   }
67
68   public void clear()
69   {
70     first = null;
71   }
72
73   public boolean equals(Object JavaDoc o)
74   {
75     if (o instanceof PatternSet)
76     {
77       PatternSet set = (PatternSet)o;
78
79       if (set.getPatternCount()!=getPatternCount())
80         return false;
81
82       for (PatternSetEntry entry = first; entry!=null; entry = entry.next)
83         for (PatternSetEntry foreignentry = set.first; foreignentry!=null;
84              foreignentry = foreignentry.next)
85         {
86           if (entry.pattern==foreignentry.pattern)
87             break;
88
89           if (foreignentry.next==null)
90             return false;
91         }
92
93       return true;
94     }
95
96     return false;
97   }
98
99   public String JavaDoc toString()
100   {
101     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
102
103     buffer.append("{");
104     for (PatternSetEntry entry = first; entry!=null; entry = entry.next)
105     {
106       if (entry!=first)
107         buffer.append(",");
108
109       buffer.append(entry.pattern.toString());
110     }
111
112     buffer.append("}");
113     return buffer.toString();
114   }
115
116   private class PatternSetEntry
117   {
118     public final Pattern pattern;
119     public final PatternSetEntry next;
120
121     private PatternSetEntry(Pattern pattern, PatternSetEntry next)
122     {
123       this.pattern = pattern;
124       this.next = next;
125     }
126   }
127
128   public class PatternSetEntryIterator implements PatternIterator
129   {
130     private PatternSetEntry entry = null;
131
132     private PatternSetEntryIterator(PatternSetEntry entry)
133     {
134       this.entry = entry;
135     }
136
137     public boolean hasNext()
138     {
139       return entry!=null;
140     }
141
142     public Pattern next()
143     {
144       Pattern pattern = entry.pattern;
145       this.entry = entry.next;
146       return pattern;
147     }
148   }
149 }
150
Popular Tags