View Javadoc
1   /*
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   */
14  
15  package org.basepom.mojo.propertyhelper;
16  
17  import static com.google.common.base.Preconditions.checkNotNull;
18  import static com.google.common.base.Preconditions.checkState;
19  
20  import java.util.List;
21  import java.util.Locale;
22  import java.util.Map;
23  import java.util.function.Function;
24  
25  import com.google.common.base.CharMatcher;
26  import com.google.common.collect.ImmutableList;
27  import com.google.common.collect.ImmutableMap;
28  
29  public final class TransformerRegistry {
30  
31      public static final TransformerRegistry INSTANCE = new TransformerRegistry();
32  
33      private final Map<String, Function<String, String>> registry;
34  
35      private TransformerRegistry() {
36          final ImmutableMap.Builder<String, Function<String, String>> registry = ImmutableMap.builder();
37          registry.put("lowercase", new LowercaseTransformer());
38          registry.put("uppercase", new UppercaseTransformer());
39          registry.put("remove_whitespace", new RemoveWhitespaceTransformer());
40          registry.put("underscore_for_whitespace", new UnderscoreForWhitespaceTransformer());
41          registry.put("dash_for_whitespace", new DashForWhitespaceTransformer());
42          registry.put("use_underscore", new UseUnderscoreTransformer());
43          registry.put("use_dash", new UseDashTransformer());
44          registry.put("trim", new TrimTransformer());
45  
46          this.registry = registry.build();
47      }
48  
49      public Function<String, String> applyTransformers(List<String> transformerNames) {
50          return value -> applyTransformers(transformerNames, value);
51      }
52  
53      private String applyTransformers(final List<String> transformerNames, final String value) {
54          String res = value;
55  
56          var transformers = transformerNames.stream()
57              .map(this::forName)
58              .collect(ImmutableList.toImmutableList());
59  
60          for (var transformer : transformers) {
61              if (res != null) {
62                  res = transformer.apply(res);
63              }
64          }
65          return res;
66      }
67  
68      Function<String, String> forName(final String transformerName) {
69          checkNotNull(transformerName, "transformerName is null");
70  
71          final var transformer = registry.get(transformerName.toLowerCase(Locale.getDefault()));
72  
73          checkState(transformer != null, "Transformer '%s' is unknown.", transformerName);
74  
75          return transformer;
76      }
77  
78      public static class LowercaseTransformer
79          implements Function<String, String> {
80  
81          @Override
82          public String apply(String value) {
83              return value.toLowerCase(Locale.getDefault());
84          }
85      }
86  
87      public static class UppercaseTransformer
88          implements Function<String, String> {
89  
90          @Override
91          public String apply(String value) {
92              return value.toUpperCase(Locale.getDefault());
93          }
94      }
95  
96      public static class RemoveWhitespaceTransformer
97          implements Function<String, String> {
98  
99          @Override
100         public String apply(String value) {
101             return CharMatcher.whitespace().removeFrom(value);
102         }
103     }
104 
105     public static class UnderscoreForWhitespaceTransformer
106         implements Function<String, String> {
107 
108         @Override
109         public String apply(String value) {
110             return CharMatcher.whitespace().collapseFrom(value, '_');
111         }
112     }
113 
114     public static class DashForWhitespaceTransformer
115         implements Function<String, String> {
116 
117         @Override
118         public String apply(String value) {
119             return CharMatcher.whitespace().collapseFrom(value, '-');
120         }
121     }
122 
123     public static class UseUnderscoreTransformer
124         implements Function<String, String> {
125 
126         @Override
127         public String apply(String value) {
128             return CharMatcher.whitespace().or(CharMatcher.anyOf("-_")).collapseFrom(value, '_');
129         }
130     }
131 
132     public static class UseDashTransformer
133         implements Function<String, String> {
134 
135         @Override
136         public String apply(String value) {
137             return CharMatcher.whitespace().or(CharMatcher.anyOf("-_")).collapseFrom(value, '-');
138         }
139     }
140 
141     public static class TrimTransformer
142         implements Function<String, String> {
143 
144         @Override
145         public String apply(String value) {
146             return CharMatcher.whitespace().trimFrom(value);
147         }
148     }
149 }