|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.maven.api.services; |
| 20 | + |
| 21 | +import java.util.Collection; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Optional; |
| 25 | +import java.util.function.BiFunction; |
| 26 | +import java.util.function.Function; |
| 27 | + |
| 28 | +import org.apache.maven.api.Service; |
| 29 | +import org.apache.maven.api.annotations.Experimental; |
| 30 | +import org.apache.maven.api.annotations.Nonnull; |
| 31 | +import org.apache.maven.api.annotations.Nullable; |
| 32 | + |
| 33 | +/** |
| 34 | + * The Interpolator service provides methods for variable substitution in strings and maps. |
| 35 | + * It allows for the replacement of placeholders (e.g., ${variable}) with their corresponding values. |
| 36 | + * |
| 37 | + * @since 4.0.0 |
| 38 | + */ |
| 39 | +@Experimental |
| 40 | +public interface Interpolator extends Service { |
| 41 | + |
| 42 | + /** |
| 43 | + * Interpolates the values in the given map using the provided callback function. |
| 44 | + * This method defaults to setting empty strings for unresolved placeholders. |
| 45 | + * |
| 46 | + * @param properties The map containing key-value pairs to be interpolated. |
| 47 | + * @param callback The function to resolve variable values not found in the map. |
| 48 | + */ |
| 49 | + default void interpolate(@Nonnull Map<String, String> properties, @Nullable Function<String, String> callback) { |
| 50 | + interpolate(properties, callback, null, true); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Interpolates the values in the given map using the provided callback function. |
| 55 | + * |
| 56 | + * @param map The map containing key-value pairs to be interpolated. |
| 57 | + * @param callback The function to resolve variable values not found in the map. |
| 58 | + * @param defaultsToEmpty If true, unresolved placeholders are replaced with empty strings. If false, they are left unchanged. |
| 59 | + */ |
| 60 | + default void interpolate( |
| 61 | + @Nonnull Map<String, String> map, @Nullable Function<String, String> callback, boolean defaultsToEmpty) { |
| 62 | + interpolate(map, callback, null, defaultsToEmpty); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Interpolates the values in the given map using the provided callback function. |
| 67 | + * |
| 68 | + * @param map The map containing key-value pairs to be interpolated. |
| 69 | + * @param callback The function to resolve variable values not found in the map. |
| 70 | + * @param defaultsToEmpty If true, unresolved placeholders are replaced with empty strings. If false, they are left unchanged. |
| 71 | + */ |
| 72 | + void interpolate( |
| 73 | + @Nonnull Map<String, String> map, |
| 74 | + @Nullable Function<String, String> callback, |
| 75 | + @Nullable BiFunction<String, String, String> postprocessor, |
| 76 | + boolean defaultsToEmpty); |
| 77 | + |
| 78 | + /** |
| 79 | + * Interpolates a single string value using the provided callback function. |
| 80 | + * This method defaults to not replacing unresolved placeholders. |
| 81 | + * |
| 82 | + * @param val The string to be interpolated. |
| 83 | + * @param callback The function to resolve variable values. |
| 84 | + * @return The interpolated string, or null if the input was null. |
| 85 | + */ |
| 86 | + @Nullable |
| 87 | + default String interpolate(@Nullable String val, @Nullable Function<String, String> callback) { |
| 88 | + return interpolate(val, callback, false); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Interpolates a single string value using the provided callback function. |
| 93 | + * |
| 94 | + * @param val The string to be interpolated. |
| 95 | + * @param callback The function to resolve variable values. |
| 96 | + * @param defaultsToEmpty If true, unresolved placeholders are replaced with empty strings. |
| 97 | + * @return The interpolated string, or null if the input was null. |
| 98 | + */ |
| 99 | + @Nullable |
| 100 | + default String interpolate( |
| 101 | + @Nullable String val, @Nullable Function<String, String> callback, boolean defaultsToEmpty) { |
| 102 | + return interpolate(val, callback, null, defaultsToEmpty); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Interpolates a single string value using the provided callback function. |
| 107 | + * |
| 108 | + * @param val The string to be interpolated. |
| 109 | + * @param callback The function to resolve variable values. |
| 110 | + * @param defaultsToEmpty If true, unresolved placeholders are replaced with empty strings. |
| 111 | + * @return The interpolated string, or null if the input was null. |
| 112 | + */ |
| 113 | + @Nullable |
| 114 | + String interpolate( |
| 115 | + @Nullable String val, |
| 116 | + @Nullable Function<String, String> callback, |
| 117 | + @Nullable BiFunction<String, String, String> postprocessor, |
| 118 | + boolean defaultsToEmpty); |
| 119 | + |
| 120 | + /** |
| 121 | + * Creates a composite function from a collection of functions. |
| 122 | + * |
| 123 | + * @param functions A collection of functions, each taking a String as input and returning a String. |
| 124 | + * @return A function that applies each function in the collection in order until a non-null result is found. |
| 125 | + * If all functions return null, the composite function returns null. |
| 126 | + * |
| 127 | + * @implNote This implementation uses Java streams to process the collection of functions. |
| 128 | + * It applies each function to the input string and returns the first non-null result. |
| 129 | + * If all functions return null, it returns null. |
| 130 | + * The returned function is not thread-safe if the input collection is modified after this method is called. |
| 131 | + * |
| 132 | + * @throws NullPointerException if the input collection is null or contains null elements. |
| 133 | + */ |
| 134 | + static Function<String, String> chain(Collection<? extends Function<String, String>> functions) { |
| 135 | + return s -> { |
| 136 | + for (Function<String, String> function : functions) { |
| 137 | + String v = function.apply(s); |
| 138 | + if (v != null) { |
| 139 | + return v; |
| 140 | + } |
| 141 | + } |
| 142 | + return null; |
| 143 | + }; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Memoizes a given function that takes a String input and produces a String output. |
| 148 | + * This method creates a new function that caches the results of the original function, |
| 149 | + * improving performance for repeated calls with the same input. |
| 150 | + * |
| 151 | + * @param callback The original function to be memoized. It takes a String as input and returns a String. |
| 152 | + * @return A new Function<String, String> that caches the results of the original function. |
| 153 | + * If the original function returns null for a given input, null will be cached and returned for subsequent calls with the same input. |
| 154 | + * |
| 155 | + * @implNote This implementation uses a HashMap to store the cached results. |
| 156 | + * The cache keys are the input Strings, and the values are Optional<String> to handle null results. |
| 157 | + * The returned function is thread-safe for concurrent access. |
| 158 | + * |
| 159 | + * @see Function |
| 160 | + * @see Optional |
| 161 | + * @see HashMap#computeIfAbsent(Object, Function) |
| 162 | + */ |
| 163 | + static Function<String, String> memoize(Function<String, String> callback) { |
| 164 | + Map<String, Optional<String>> cache = new HashMap<>(); |
| 165 | + return s -> cache.computeIfAbsent(s, v -> Optional.ofNullable(callback.apply(v))).orElse(null); |
| 166 | + } |
| 167 | +} |
0 commit comments