001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 * http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014
015package org.basepom.mojo.propertyhelper;
016
017import static com.google.common.base.Preconditions.checkNotNull;
018import static com.google.common.base.Preconditions.checkState;
019
020import java.util.List;
021import java.util.Locale;
022import java.util.Map;
023import java.util.function.Function;
024
025import com.google.common.base.CharMatcher;
026import com.google.common.collect.ImmutableList;
027import com.google.common.collect.ImmutableMap;
028
029public final class TransformerRegistry {
030
031    public static final TransformerRegistry INSTANCE = new TransformerRegistry();
032
033    private final Map<String, Function<String, String>> registry;
034
035    private TransformerRegistry() {
036        final ImmutableMap.Builder<String, Function<String, String>> registry = ImmutableMap.builder();
037        registry.put("lowercase", new LowercaseTransformer());
038        registry.put("uppercase", new UppercaseTransformer());
039        registry.put("remove_whitespace", new RemoveWhitespaceTransformer());
040        registry.put("underscore_for_whitespace", new UnderscoreForWhitespaceTransformer());
041        registry.put("dash_for_whitespace", new DashForWhitespaceTransformer());
042        registry.put("use_underscore", new UseUnderscoreTransformer());
043        registry.put("use_dash", new UseDashTransformer());
044        registry.put("trim", new TrimTransformer());
045
046        this.registry = registry.build();
047    }
048
049    public Function<String, String> applyTransformers(List<String> transformerNames) {
050        return value -> applyTransformers(transformerNames, value);
051    }
052
053    private String applyTransformers(final List<String> transformerNames, final String value) {
054        String res = value;
055
056        var transformers = transformerNames.stream()
057            .map(this::forName)
058            .collect(ImmutableList.toImmutableList());
059
060        for (var transformer : transformers) {
061            if (res != null) {
062                res = transformer.apply(res);
063            }
064        }
065        return res;
066    }
067
068    Function<String, String> forName(final String transformerName) {
069        checkNotNull(transformerName, "transformerName is null");
070
071        final var transformer = registry.get(transformerName.toLowerCase(Locale.getDefault()));
072
073        checkState(transformer != null, "Transformer '%s' is unknown.", transformerName);
074
075        return transformer;
076    }
077
078    public static class LowercaseTransformer
079        implements Function<String, String> {
080
081        @Override
082        public String apply(String value) {
083            return value.toLowerCase(Locale.getDefault());
084        }
085    }
086
087    public static class UppercaseTransformer
088        implements Function<String, String> {
089
090        @Override
091        public String apply(String value) {
092            return value.toUpperCase(Locale.getDefault());
093        }
094    }
095
096    public static class RemoveWhitespaceTransformer
097        implements Function<String, String> {
098
099        @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}