-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
2,623 additions
and
707 deletions.
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
api/maven-api-core/src/main/java/org/apache/maven/api/services/Interpolator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.maven.api.services; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.function.BiFunction; | ||
import java.util.function.Function; | ||
|
||
import org.apache.maven.api.Service; | ||
import org.apache.maven.api.annotations.Experimental; | ||
import org.apache.maven.api.annotations.Nonnull; | ||
import org.apache.maven.api.annotations.Nullable; | ||
|
||
/** | ||
* The Interpolator service provides methods for variable substitution in strings and maps. | ||
* It allows for the replacement of placeholders (e.g., ${variable}) with their corresponding values. | ||
* | ||
* @since 4.0.0 | ||
*/ | ||
@Experimental | ||
public interface Interpolator extends Service { | ||
|
||
/** | ||
* Interpolates the values in the given map using the provided callback function. | ||
* This method defaults to setting empty strings for unresolved placeholders. | ||
* | ||
* @param properties The map containing key-value pairs to be interpolated. | ||
* @param callback The function to resolve variable values not found in the map. | ||
*/ | ||
default void interpolate(@Nonnull Map<String, String> properties, @Nullable Function<String, String> callback) { | ||
interpolate(properties, callback, null, true); | ||
} | ||
|
||
/** | ||
* Interpolates the values in the given map using the provided callback function. | ||
* | ||
* @param map The map containing key-value pairs to be interpolated. | ||
* @param callback The function to resolve variable values not found in the map. | ||
* @param defaultsToEmpty If true, unresolved placeholders are replaced with empty strings. If false, they are left unchanged. | ||
*/ | ||
default void interpolate( | ||
@Nonnull Map<String, String> map, @Nullable Function<String, String> callback, boolean defaultsToEmpty) { | ||
interpolate(map, callback, null, defaultsToEmpty); | ||
} | ||
|
||
/** | ||
* Interpolates the values in the given map using the provided callback function. | ||
* | ||
* @param map The map containing key-value pairs to be interpolated. | ||
* @param callback The function to resolve variable values not found in the map. | ||
* @param defaultsToEmpty If true, unresolved placeholders are replaced with empty strings. If false, they are left unchanged. | ||
*/ | ||
void interpolate( | ||
@Nonnull Map<String, String> map, | ||
@Nullable Function<String, String> callback, | ||
@Nullable BiFunction<String, String, String> postprocessor, | ||
boolean defaultsToEmpty); | ||
|
||
/** | ||
* Interpolates a single string value using the provided callback function. | ||
* This method defaults to not replacing unresolved placeholders. | ||
* | ||
* @param val The string to be interpolated. | ||
* @param callback The function to resolve variable values. | ||
* @return The interpolated string, or null if the input was null. | ||
*/ | ||
@Nullable | ||
default String interpolate(@Nullable String val, @Nullable Function<String, String> callback) { | ||
return interpolate(val, callback, false); | ||
} | ||
|
||
/** | ||
* Interpolates a single string value using the provided callback function. | ||
* | ||
* @param val The string to be interpolated. | ||
* @param callback The function to resolve variable values. | ||
* @param defaultsToEmpty If true, unresolved placeholders are replaced with empty strings. | ||
* @return The interpolated string, or null if the input was null. | ||
*/ | ||
@Nullable | ||
default String interpolate( | ||
@Nullable String val, @Nullable Function<String, String> callback, boolean defaultsToEmpty) { | ||
return interpolate(val, callback, null, defaultsToEmpty); | ||
} | ||
|
||
/** | ||
* Interpolates a single string value using the provided callback function. | ||
* | ||
* @param val The string to be interpolated. | ||
* @param callback The function to resolve variable values. | ||
* @param defaultsToEmpty If true, unresolved placeholders are replaced with empty strings. | ||
* @return The interpolated string, or null if the input was null. | ||
*/ | ||
@Nullable | ||
String interpolate( | ||
@Nullable String val, | ||
@Nullable Function<String, String> callback, | ||
@Nullable BiFunction<String, String, String> postprocessor, | ||
boolean defaultsToEmpty); | ||
|
||
/** | ||
* Creates a composite function from a collection of functions. | ||
* | ||
* @param functions A collection of functions, each taking a String as input and returning a String. | ||
* @return A function that applies each function in the collection in order until a non-null result is found. | ||
* If all functions return null, the composite function returns null. | ||
* | ||
* @throws NullPointerException if the input collection is null or contains null elements. | ||
*/ | ||
static Function<String, String> chain(Collection<? extends Function<String, String>> functions) { | ||
return s -> { | ||
for (Function<String, String> function : functions) { | ||
String v = function.apply(s); | ||
if (v != null) { | ||
return v; | ||
} | ||
} | ||
return null; | ||
}; | ||
} | ||
|
||
/** | ||
* Memoizes a given function that takes a String input and produces a String output. | ||
* This method creates a new function that caches the results of the original function, | ||
* improving performance for repeated calls with the same input. | ||
* | ||
* @param callback The original function to be memoized. It takes a String as input and returns a String. | ||
* @return A new {@code Function<String, String>} that caches the results of the original function. | ||
* If the original function returns null for a given input, null will be cached and returned for subsequent calls with the same input. | ||
* | ||
* @see Function | ||
* @see Optional | ||
* @see HashMap#computeIfAbsent(Object, Function) | ||
*/ | ||
static Function<String, String> memoize(Function<String, String> callback) { | ||
Map<String, Optional<String>> cache = new HashMap<>(); | ||
return s -> cache.computeIfAbsent(s, v -> Optional.ofNullable(callback.apply(v))) | ||
.orElse(null); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
api/maven-api-core/src/main/java/org/apache/maven/api/services/InterpolatorException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.maven.api.services; | ||
|
||
import java.io.Serial; | ||
|
||
import org.apache.maven.api.annotations.Experimental; | ||
|
||
/** | ||
* Exception thrown by {@link Interpolator} implementations when an error occurs during interpolation. | ||
* This can include syntax errors in variable placeholders or recursive variable references. | ||
* | ||
* @since 4.0.0 | ||
*/ | ||
@Experimental | ||
public class InterpolatorException extends MavenException { | ||
|
||
@Serial | ||
private static final long serialVersionUID = -1219149033636851813L; | ||
|
||
/** | ||
* Constructs a new InterpolatorException with {@code null} as its | ||
* detail message. The cause is not initialized, and may subsequently be | ||
* initialized by a call to {@link #initCause}. | ||
*/ | ||
public InterpolatorException() {} | ||
|
||
/** | ||
* Constructs a new InterpolatorException with the specified detail message. | ||
* The cause is not initialized, and may subsequently be initialized by | ||
* a call to {@link #initCause}. | ||
* | ||
* @param message the detail message. The detail message is saved for | ||
* later retrieval by the {@link #getMessage()} method. | ||
*/ | ||
public InterpolatorException(String message) { | ||
super(message); | ||
} | ||
|
||
/** | ||
* Constructs a new InterpolatorException with the specified detail message and cause. | ||
* | ||
* <p>Note that the detail message associated with {@code cause} is <i>not</i> | ||
* automatically incorporated in this exception's detail message.</p> | ||
* | ||
* @param message the detail message (which is saved for later retrieval | ||
* by the {@link #getMessage()} method). | ||
* @param cause the cause (which is saved for later retrieval by the | ||
* {@link #getCause()} method). A {@code null} value is | ||
* permitted, and indicates that the cause is nonexistent or unknown. | ||
*/ | ||
public InterpolatorException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.