1
2
3
4
5
6
7
8
9
10
11 package ch.qos.logback.core.joran.spi;
12
13 import java.util.ArrayList;
14
15
16
17
18
19
20
21
22
23 public class Pattern {
24
25
26 ArrayList<String> partList = new ArrayList<String>();
27
28 public Pattern() {
29 }
30
31
32
33
34
35
36
37 public Pattern(String p) {
38 this();
39
40 if (p == null) {
41 return;
42 }
43
44 int lastIndex = 0;
45
46
47 while (true) {
48 int k = p.indexOf('/', lastIndex);
49
50
51 if (k == -1) {
52 String lastPart = p.substring(lastIndex);
53 if (lastPart != null && lastPart.length() > 0) {
54 partList.add(p.substring(lastIndex));
55 }
56 break;
57 } else {
58 String c = p.substring(lastIndex, k);
59
60 if (c.length() > 0) {
61 partList.add(c);
62 }
63
64 lastIndex = k + 1;
65 }
66 }
67
68
69 }
70
71 public Object clone() {
72 Pattern p = new Pattern();
73 p.partList.addAll(this.partList);
74 return p;
75 }
76
77 public void push(String s) {
78 partList.add(s);
79 }
80
81 public int size() {
82 return partList.size();
83 }
84
85 public String get(int i) {
86 return (String) partList.get(i);
87 }
88
89 public void pop() {
90 if (!partList.isEmpty()) {
91 partList.remove(partList.size() - 1);
92 }
93 }
94
95 public String peekLast() {
96 if (!partList.isEmpty()) {
97 int size = partList.size();
98 return (String) partList.get(size - 1);
99 } else {
100 return null;
101 }
102 }
103
104
105
106
107
108
109 public int getTailMatchLength(Pattern p) {
110 if (p == null) {
111 return 0;
112 }
113
114 int lSize = this.partList.size();
115 int rSize = p.partList.size();
116
117
118 if ((lSize == 0) || (rSize == 0)) {
119 return 0;
120 }
121
122 int minLen = (lSize <= rSize) ? lSize : rSize;
123 int match = 0;
124
125
126 for (int i = 1; i <= minLen; i++) {
127 String l = (String) this.partList.get(lSize - i);
128 String r = (String) p.partList.get(rSize - i);
129
130 if (l.equals(r)) {
131 match++;
132 } else {
133 break;
134 }
135 }
136
137 return match;
138 }
139
140
141
142
143
144
145 public int getPrefixMatchLength(Pattern p) {
146 if (p == null) {
147 return 0;
148 }
149
150 int lSize = this.partList.size();
151 int rSize = p.partList.size();
152
153
154 if ((lSize == 0) || (rSize == 0)) {
155 return 0;
156 }
157
158 int minLen = (lSize <= rSize) ? lSize : rSize;
159 int match = 0;
160
161 for (int i = 0; i < minLen; i++) {
162 String l = (String) this.partList.get(i);
163 String r = (String) p.partList.get(i);
164
165
166 if (l.equals(r)) {
167 match++;
168 } else {
169 break;
170 }
171 }
172
173 return match;
174 }
175
176 @Override
177 public boolean equals(Object o) {
178
179 if ((o == null) || !(o instanceof Pattern)) {
180 return false;
181 }
182
183
184 Pattern r = (Pattern) o;
185
186 if (r.size() != size()) {
187 return false;
188 }
189
190
191 int len = size();
192
193 for (int i = 0; i < len; i++) {
194 if (!(get(i).equals(r.get(i)))) {
195 return false;
196 }
197 }
198
199
200 return true;
201 }
202
203 @Override
204 public int hashCode() {
205 int hc = 0;
206 int len = size();
207
208 for (int i = 0; i < len; i++) {
209 hc ^= get(i).hashCode();
210
211
212 }
213
214 return hc;
215 }
216
217 @Override
218 public String toString() {
219 int size = partList.size();
220 String result = "";
221 for (int i = 0; i < size; i++) {
222 result += "[" + partList.get(i) + "]";
223 }
224 return result;
225 }
226 }