1
1
/*
2
- * Copyright 2006-2007 the original author or authors.
2
+ * Copyright 2006-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
package org .springframework .batch .support ;
17
17
18
18
import java .util .ArrayList ;
19
- import java .util .Collections ;
20
19
import java .util .Comparator ;
21
- import java .util .HashMap ;
22
20
import java .util .List ;
23
21
import java .util .Map ;
24
22
27
25
/**
28
26
* @author Dave Syer
29
27
* @author Dan Garrette
28
+ * @author Marten Deinum
30
29
*/
31
30
public class PatternMatcher <S > {
32
31
33
- private Map <String , S > map = new HashMap <>() ;
34
- private List <String > sorted = new ArrayList <>() ;
32
+ private final Map <String , S > map ;
33
+ private final List <String > sorted ;
35
34
36
35
/**
37
36
* Initialize a new {@link PatternMatcher} with a map of patterns to values
@@ -42,14 +41,7 @@ public PatternMatcher(Map<String, S> map) {
42
41
this .map = map ;
43
42
// Sort keys to start with the most specific
44
43
sorted = new ArrayList <>(map .keySet ());
45
- Collections .sort (sorted , new Comparator <String >() {
46
- @ Override
47
- public int compare (String o1 , String o2 ) {
48
- String s1 = o1 ; // .replace('?', '{');
49
- String s2 = o2 ; // .replace('*', '}');
50
- return s2 .compareTo (s1 );
51
- }
52
- });
44
+ sorted .sort (Comparator .reverseOrder ());
53
45
}
54
46
55
47
/**
@@ -66,12 +58,10 @@ public int compare(String o1, String o2) {
66
58
* <code>false</code> otherwise.
67
59
*/
68
60
public static boolean match (String pattern , String str ) {
69
- char [] patArr = pattern .toCharArray ();
70
- char [] strArr = str .toCharArray ();
71
61
int patIdxStart = 0 ;
72
- int patIdxEnd = patArr .length - 1 ;
62
+ int patIdxEnd = pattern .length () - 1 ;
73
63
int strIdxStart = 0 ;
74
- int strIdxEnd = strArr .length - 1 ;
64
+ int strIdxEnd = str .length () - 1 ;
75
65
char ch ;
76
66
77
67
boolean containsStar = pattern .contains ("*" );
@@ -82,9 +72,9 @@ public static boolean match(String pattern, String str) {
82
72
return false ; // Pattern and string do not have the same size
83
73
}
84
74
for (int i = 0 ; i <= patIdxEnd ; i ++) {
85
- ch = patArr [ i ] ;
75
+ ch = pattern . charAt ( i ) ;
86
76
if (ch != '?' ) {
87
- if (ch != strArr [ i ] ) {
77
+ if (ch != str . charAt ( i ) ) {
88
78
return false ;// Character mismatch
89
79
}
90
80
}
@@ -97,9 +87,9 @@ public static boolean match(String pattern, String str) {
97
87
}
98
88
99
89
// Process characters before first star
100
- while ((ch = patArr [ patIdxStart ] ) != '*' && strIdxStart <= strIdxEnd ) {
90
+ while ((ch = pattern . charAt ( patIdxStart ) ) != '*' && strIdxStart <= strIdxEnd ) {
101
91
if (ch != '?' ) {
102
- if (ch != strArr [ strIdxStart ] ) {
92
+ if (ch != str . charAt ( strIdxStart ) ) {
103
93
return false ;// Character mismatch
104
94
}
105
95
}
@@ -110,17 +100,17 @@ public static boolean match(String pattern, String str) {
110
100
// All characters in the string are used. Check if only '*'s are
111
101
// left in the pattern. If so, we succeeded. Otherwise failure.
112
102
for (int i = patIdxStart ; i <= patIdxEnd ; i ++) {
113
- if (patArr [ i ] != '*' ) {
103
+ if (pattern . charAt ( i ) != '*' ) {
114
104
return false ;
115
105
}
116
106
}
117
107
return true ;
118
108
}
119
109
120
110
// Process characters after last star
121
- while ((ch = patArr [ patIdxEnd ] ) != '*' && strIdxStart <= strIdxEnd ) {
111
+ while ((ch = pattern . charAt ( patIdxEnd ) ) != '*' && strIdxStart <= strIdxEnd ) {
122
112
if (ch != '?' ) {
123
- if (ch != strArr [ strIdxEnd ] ) {
113
+ if (ch != str . charAt ( strIdxEnd ) ) {
124
114
return false ;// Character mismatch
125
115
}
126
116
}
@@ -131,7 +121,7 @@ public static boolean match(String pattern, String str) {
131
121
// All characters in the string are used. Check if only '*'s are
132
122
// left in the pattern. If so, we succeeded. Otherwise failure.
133
123
for (int i = patIdxStart ; i <= patIdxEnd ; i ++) {
134
- if (patArr [ i ] != '*' ) {
124
+ if (pattern . charAt ( i ) != '*' ) {
135
125
return false ;
136
126
}
137
127
}
@@ -143,7 +133,7 @@ public static boolean match(String pattern, String str) {
143
133
while (patIdxStart != patIdxEnd && strIdxStart <= strIdxEnd ) {
144
134
int patIdxTmp = -1 ;
145
135
for (int i = patIdxStart + 1 ; i <= patIdxEnd ; i ++) {
146
- if (patArr [ i ] == '*' ) {
136
+ if (pattern . charAt ( i ) == '*' ) {
147
137
patIdxTmp = i ;
148
138
break ;
149
139
}
@@ -160,9 +150,9 @@ public static boolean match(String pattern, String str) {
160
150
int foundIdx = -1 ;
161
151
strLoop : for (int i = 0 ; i <= strLength - patLength ; i ++) {
162
152
for (int j = 0 ; j < patLength ; j ++) {
163
- ch = patArr [ patIdxStart + j + 1 ] ;
153
+ ch = pattern . charAt ( patIdxStart + j + 1 ) ;
164
154
if (ch != '?' ) {
165
- if (ch != strArr [ strIdxStart + i + j ] ) {
155
+ if (ch != str . charAt ( strIdxStart + i + j ) ) {
166
156
continue strLoop ;
167
157
}
168
158
}
@@ -183,7 +173,7 @@ public static boolean match(String pattern, String str) {
183
173
// All characters in the string are used. Check if only '*'s are left
184
174
// in the pattern. If so, we succeeded. Otherwise failure.
185
175
for (int i = patIdxStart ; i <= patIdxEnd ; i ++) {
186
- if (patArr [ i ] != '*' ) {
176
+ if (pattern . charAt ( i ) != '*' ) {
187
177
return false ;
188
178
}
189
179
}
0 commit comments