22
22
*/
23
23
package jdk .jpackage .test ;
24
24
25
+ import java .nio .file .Path ;
25
26
import java .util .List ;
27
+ import java .util .Objects ;
26
28
import java .util .function .BiFunction ;
29
+ import java .util .function .Supplier ;
30
+ import java .util .stream .Stream ;
27
31
28
- public final class CannedFormattedString {
32
+ public record CannedFormattedString ( BiFunction < String , Object [], String > formatter , String key , Object [] args ) {
29
33
30
- CannedFormattedString (BiFunction <String , Object [], String > formatter ,
31
- String key , Object [] args ) {
32
- this .formatter = formatter ;
33
- this .key = key ;
34
- this .args = args ;
34
+ @ FunctionalInterface
35
+ public interface CannedArgument {
36
+ public String value ();
37
+ }
38
+
39
+ public static Object cannedArgument (Supplier <Object > supplier , String label ) {
40
+ Objects .requireNonNull (supplier );
41
+ Objects .requireNonNull (label );
42
+ return new CannedArgument () {
43
+
44
+ @ Override
45
+ public String value () {
46
+ return supplier .get ().toString ();
47
+ }
48
+
49
+ @ Override
50
+ public String toString ( ) {
51
+ return label ;
52
+ }
53
+ };
54
+ }
55
+
56
+ public static Object cannedAbsolutePath (Path v ) {
57
+ return cannedArgument (() -> v .toAbsolutePath (), String .format ("AbsolutePath(%s)" , v ));
58
+ }
59
+
60
+ public static Object cannedAbsolutePath (String v ) {
61
+ return cannedAbsolutePath (Path .of (v ));
62
+ }
63
+
64
+ public CannedFormattedString {
65
+ Objects .requireNonNull (formatter );
66
+ Objects .requireNonNull (key );
67
+ Objects .requireNonNull (args );
68
+ List .of (args ).forEach (Objects ::requireNonNull );
35
69
}
36
70
37
71
public String getValue () {
38
- return formatter .apply (key , args );
72
+ return formatter .apply (key , Stream .of (args ).map (arg -> {
73
+ if (arg instanceof CannedArgument cannedArg ) {
74
+ return cannedArg .value ();
75
+ } else {
76
+ return arg ;
77
+ }
78
+ }).toArray ());
39
79
}
40
80
41
81
@ Override
@@ -46,8 +86,4 @@ public String toString() {
46
86
return String .format ("%s+%s" , key , List .of (args ));
47
87
}
48
88
}
49
-
50
- private final BiFunction <String , Object [], String > formatter ;
51
- private final String key ;
52
- private final Object [] args ;
53
89
}
0 commit comments