-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved some stuff to new Interpolation class for now, will decided if …
…to actually do this.
- Loading branch information
1 parent
a03a072
commit cd26a44
Showing
4 changed files
with
82 additions
and
49 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.sshtools.jini; | ||
|
||
import java.text.MessageFormat; | ||
import java.util.function.Function; | ||
import java.util.regex.Pattern; | ||
|
||
public class Interpolation { | ||
|
||
@SuppressWarnings("unchecked") | ||
public static Function<String, String> compound(Function<String, String>... sources) { | ||
return (k) -> { | ||
for(var src : sources) { | ||
var v = src.apply(k); | ||
if(v != null) | ||
return v; | ||
} | ||
return null; | ||
}; | ||
} | ||
|
||
public static Function<String, String> systemProperties() { | ||
return (k) -> { | ||
if(k.startsWith("sys.")) { | ||
return System.getProperty(k.substring(4)); | ||
} | ||
else if(k.startsWith("env.")) { | ||
return System.getenv(k.substring(4)); | ||
} | ||
else | ||
return null; | ||
}; | ||
} | ||
|
||
public static String str(String text, Function<String, String> source) { | ||
var pattern = Pattern.compile("\\$\\{(.*?)\\}"); | ||
var matcher = pattern.matcher(text); | ||
var builder = new StringBuilder(); | ||
int i = 0; | ||
while (matcher.find()) { | ||
var variable = matcher.group(1); | ||
var replacement = source.apply(variable); | ||
builder.append(text.substring(i, matcher.start())); | ||
if (replacement == null) { | ||
throw new IllegalArgumentException(MessageFormat.format("Unknown string variable $\\{{0}\\}", variable)); | ||
} else { | ||
builder.append(replacement); | ||
} | ||
i = matcher.end(); | ||
|
||
} | ||
builder.append(text.substring(i, text.length())); | ||
text = builder.toString(); | ||
return text; | ||
} | ||
} |
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