Skip to content

Commit

Permalink
map methods
Browse files Browse the repository at this point in the history
  • Loading branch information
JimLaskey committed Mar 11, 2024
1 parent 3a6fd91 commit 5d8510b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/java.base/share/classes/java/lang/StringTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,60 @@ static StringTemplate combine(List<StringTemplate> stringTemplates) {
return JTA.combine(stringTemplates.toArray(new StringTemplate[0]));
}

/**
* Constructs a new {@link StringTemplate} using this instance's values and
* fragments mapped from this instance's fragments by the specified
* {@code mapper} function. For example:
* {@snippet lang = JAVA:
* StringTemplate st2 = st1.mapFragments(f -> f.toUpperCase());
* }
* or
* {@snippet lan g =JAVA:
* StringTemplate st4 = st3.mapFragments(f -> {
* if (f.contains("\"")) {
* throw new RuntimeException("Should not use quotes in the template");
* }
* return f;
* });
* }
*
* @param mapper mapper function
* @return new {@link StringTemplate}
*/
default StringTemplate mapFragments(Function<String, String> mapper) {
Objects.requireNonNull(mapper, "mapper must not be null");
List<String> fragments = fragments()
.stream()
.map(mapper)
.toList();
return StringTemplate.of(fragments, values());
}

/**
* Constructs a new {@link StringTemplate} using this instance's fragments and
* values mapped from this instance's values by the specified
* {@code mapper} function.
* {@snippet lan g =JAVA :
* StringTemplate st2 = st1.mapValue(v -> {
* if (v instanceof Supplier<?> s) {
* return s.get();
* }
* return v;
* });
* }
*
* @param mapper mapper function
* @return new {@link StringTemplate}
*/
default StringTemplate mapValues(Function<Object, Object> mapper) {
Objects.requireNonNull(mapper, "mapper must not be null");
List<Object> values = values()
.stream()
.map(mapper)
.toList();
return StringTemplate.of(fragments(), values);
}

/**
* Test two {@link StringTemplate StringTemplates} for equality.
*
Expand Down
33 changes: 33 additions & 0 deletions test/jdk/java/lang/template/Basic.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public static void main(String... arg) {
limitsTests();
stringTemplateCoverage();
emptyExpressionTest();
mapTests();
}

static void ASSERT(String a, String b) {
Expand Down Expand Up @@ -398,4 +399,36 @@ static void emptyExpressionTest() {
ASSERT("\{}".fragments().size(), 1);
ASSERT("\{}".fragments().get(0), "");
}

/*
* mapFragments and mapValues
*/
static void mapTests() {
int x = 10, y = 20;
StringTemplate st = "The sum of \{x} and \{y} equals \{x + y}";
StringTemplate st1 = st.mapFragments(String::toUpperCase);
StringTemplate st2 = st.mapValues(v -> v instanceof Integer i ? i * 100 : v);
ASSERT(st1.join(), "THE SUM OF 10 AND 20 EQUALS 30");
ASSERT(st2.join(), "The sum of 1000 and 2000 equals 3000");
}




















}

0 comments on commit 5d8510b

Please sign in to comment.