|
| 1 | +/** |
| 2 | + * |
| 3 | + * Copyright 2020, Optimizely and contributors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
1 | 17 | package com.optimizely.ab.config.audience.match; |
2 | 18 |
|
3 | 19 | import java.util.Map; |
4 | 20 | import java.util.concurrent.ConcurrentHashMap; |
5 | 21 |
|
| 22 | +/** |
| 23 | + * MatchRegistry maps a string match "type" to a match implementation. |
| 24 | + * All supported Match implementations must be registed with this registry. |
| 25 | + * Third-party {@link Match} implementations may also be registered to provide |
| 26 | + * additional functionality. |
| 27 | + */ |
6 | 28 | public class MatchRegistry { |
7 | 29 |
|
8 | 30 | private static final Map<String, Match> registry = new ConcurrentHashMap<>(); |
9 | | - private static final String EXISTS = "exists"; |
10 | | - private static final String EXACT = "exact"; |
11 | | - private static final String GREATER_THAN = "gt"; |
12 | | - private static final String LESS_THAN = "lt"; |
13 | | - private static final String SUBSTRING = "substring"; |
14 | | - private static final Match LEGACY = new DefaultMatchForLegacyAttributes(); |
| 31 | + public static final String EXISTS = "exists"; |
| 32 | + public static final String EXACT = "exact"; |
| 33 | + public static final String GREATER_THAN = "gt"; |
| 34 | + public static final String LESS_THAN = "lt"; |
| 35 | + public static final String SUBSTRING = "substring"; |
| 36 | + public static final String LEGACY = "legacy"; |
15 | 37 |
|
16 | 38 | static { |
17 | | - registry.put(EXISTS, new ExistsMatch()); |
18 | | - registry.put(EXACT, new ExactMatch()); |
19 | | - registry.put(GREATER_THAN, new GTMatch()); |
20 | | - registry.put(LESS_THAN, new LTMatch()); |
21 | | - registry.put(SUBSTRING, new SubstringMatch()); |
| 39 | + register(EXISTS, new ExistsMatch()); |
| 40 | + register(EXACT, new ExactMatch()); |
| 41 | + register(GREATER_THAN, new GTMatch()); |
| 42 | + register(LESS_THAN, new LTMatch()); |
| 43 | + register(SUBSTRING, new SubstringMatch()); |
| 44 | + register(LEGACY, new DefaultMatchForLegacyAttributes()); |
22 | 45 | } |
23 | 46 |
|
24 | 47 | // TODO rename Match to Matcher |
25 | 48 | public static Match getMatch(String name) throws UnknownMatchTypeException { |
26 | | - if (name == null) { |
27 | | - return LEGACY; |
28 | | - } |
29 | | - |
30 | | - Match match = registry.get(name); |
| 49 | + Match match = registry.get(name == null ? LEGACY : name); |
31 | 50 | if (match == null) { |
32 | 51 | throw new UnknownMatchTypeException(); |
33 | 52 | } |
34 | 53 |
|
35 | 54 | return match; |
36 | 55 | } |
37 | 56 |
|
| 57 | + /** |
| 58 | + * register registers a Match implementation with it's name. |
| 59 | + * NOTE: This does not check for existence so default implementations can |
| 60 | + * be overridden. |
| 61 | + * @param name |
| 62 | + * @param match |
| 63 | + */ |
38 | 64 | public static void register(String name, Match match) { |
39 | 65 | registry.put(name, match); |
40 | 66 | } |
|
0 commit comments