Skip to content

Commit 8c62c50

Browse files
dr0icboehme
authored andcommitted
Add a wrapper for WildcardTrie enabling simple character classes
1 parent eae51eb commit 8c62c50

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2013 Pascal Christoph, hbz
3+
*
4+
* Licensed under the Apache License, Version 2.0 the "License";
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.culturegraph.mf.util.tries;
17+
18+
import java.util.List;
19+
20+
/**
21+
* A wrapper for the {@link WildcardTrie} enabling the use of simple character
22+
* classes .
23+
*
24+
* @author Pascal Christoph
25+
*
26+
* @param <P>
27+
* type of value stored
28+
*/
29+
public class SimpleRegexTrie<P> {
30+
31+
private final WildcardTrie<P> trie;
32+
public static final String SIMPLE_CHARACTER_CLASS = "\\[.*\\]";
33+
34+
public SimpleRegexTrie() {
35+
trie = new WildcardTrie<P>();
36+
}
37+
38+
/**
39+
* Enables the use of simple character classes like 'a[agt][ac]'. Calls the
40+
* method of {@link WildcardTrie} for further treatment.
41+
*
42+
* @param keys
43+
* @param value
44+
*/
45+
public void put(final String keys, final P value) {
46+
if (keys.matches(".*" + SIMPLE_CHARACTER_CLASS + ".*")) {
47+
int charClassStart = keys.indexOf('[', 0);
48+
final int charClassEnd = keys.indexOf(']', 1);
49+
String begin = keys.substring(0, charClassStart);
50+
for (; charClassStart < charClassEnd - 1; charClassStart++) {
51+
char middle = keys.charAt(charClassStart + 1);
52+
String end = keys.substring(charClassEnd + 1, keys.length());
53+
put(begin + middle + end, value);
54+
}
55+
} else
56+
trie.put(keys, value);
57+
}
58+
59+
public List<P> get(final String key) {
60+
return trie.get(key);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)