| @@ -0,0 +1,126 | |||
|
|
1 | package org.implab.gradle.common.utils; | |
|
|
2 | ||
|
|
3 | import java.io.File; | |
|
|
4 | import java.io.IOException; | |
|
|
5 | import java.util.function.Consumer; | |
|
|
6 | ||
|
|
7 | import org.implab.gradle.common.utils.LazyValue; | |
|
|
8 | ||
|
|
9 | import com.fasterxml.jackson.annotation.JsonInclude.Include; | |
|
|
10 | import com.fasterxml.jackson.core.JsonProcessingException; | |
|
|
11 | import com.fasterxml.jackson.core.type.TypeReference; | |
|
|
12 | import com.fasterxml.jackson.core.util.DefaultIndenter; | |
|
|
13 | import com.fasterxml.jackson.core.util.DefaultPrettyPrinter; | |
|
|
14 | import com.fasterxml.jackson.databind.JsonMappingException; | |
|
|
15 | import com.fasterxml.jackson.databind.ObjectMapper; | |
|
|
16 | import com.fasterxml.jackson.databind.SerializationFeature; | |
|
|
17 | import com.fasterxml.jackson.datatype.jdk8.Jdk8Module; | |
|
|
18 | import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; | |
|
|
19 | ||
|
|
20 | public class Json { | |
|
|
21 | ||
|
|
22 | private final static LazyValue<ObjectMapper> mapper = new LazyValue<>(Json::createMapper); | |
|
|
23 | ||
|
|
24 | private static ObjectMapper createMapper() { | |
|
|
25 | DefaultPrettyPrinter prettyPrinter = new DefaultPrettyPrinter(); | |
|
|
26 | prettyPrinter.indentArraysWith(DefaultIndenter.SYSTEM_LINEFEED_INSTANCE); | |
|
|
27 | ||
|
|
28 | return new ObjectMapper() | |
|
|
29 | .registerModule(new Jdk8Module()) | |
|
|
30 | .registerModule(new JavaTimeModule()) | |
|
|
31 | .setSerializationInclusion(Include.NON_EMPTY) | |
|
|
32 | .enable(SerializationFeature.INDENT_OUTPUT) | |
|
|
33 | .setDefaultPrettyPrinter(prettyPrinter); | |
|
|
34 | } | |
|
|
35 | ||
|
|
36 | public static ObjectMapper jsonMapper() { | |
|
|
37 | return mapper.get(); | |
|
|
38 | } | |
|
|
39 | ||
|
|
40 | public static Consumer<File> fileWriter(Object value) { | |
|
|
41 | return file -> { | |
|
|
42 | try { | |
|
|
43 | jsonMapper().writeValue(file, value); | |
|
|
44 | } catch (IOException e) { | |
|
|
45 | throw new SerializationException(e); | |
|
|
46 | } | |
|
|
47 | }; | |
|
|
48 | } | |
|
|
49 | ||
|
|
50 | public static void write(File file, Object value) { | |
|
|
51 | try { | |
|
|
52 | jsonMapper().writeValue(file, value); | |
|
|
53 | } catch (IOException e) { | |
|
|
54 | throw new SerializationException(e); | |
|
|
55 | } | |
|
|
56 | } | |
|
|
57 | ||
|
|
58 | public static String stringify(Object value) { | |
|
|
59 | try { | |
|
|
60 | return jsonMapper().writeValueAsString(value); | |
|
|
61 | } catch (JsonProcessingException e) { | |
|
|
62 | throw new SerializationException(e); | |
|
|
63 | } | |
|
|
64 | } | |
|
|
65 | ||
|
|
66 | public static <T> T parse(String json, Class<T> clazz) { | |
|
|
67 | try { | |
|
|
68 | return jsonMapper().readValue(json, clazz); | |
|
|
69 | } catch (JsonProcessingException e) { | |
|
|
70 | throw new SerializationException(e); | |
|
|
71 | } | |
|
|
72 | } | |
|
|
73 | ||
|
|
74 | public static <T> T parse(String json, TypeReference<T> type) { | |
|
|
75 | try { | |
|
|
76 | return jsonMapper().readValue(json, type); | |
|
|
77 | } catch (JsonProcessingException e) { | |
|
|
78 | throw new SerializationException(e); | |
|
|
79 | } | |
|
|
80 | } | |
|
|
81 | ||
|
|
82 | public static <T> T read(File file, TypeReference<T> type) { | |
|
|
83 | try { | |
|
|
84 | return jsonMapper().readValue(file, type); | |
|
|
85 | } catch (IOException e) { | |
|
|
86 | throw new SerializationException(e); | |
|
|
87 | } | |
|
|
88 | } | |
|
|
89 | ||
|
|
90 | public static <T> T updateValue(T target, Object source) { | |
|
|
91 | try { | |
|
|
92 | return jsonMapper().updateValue(target, source); | |
|
|
93 | } catch (JsonMappingException e) { | |
|
|
94 | throw new SerializationException(e); | |
|
|
95 | } | |
|
|
96 | } | |
|
|
97 | ||
|
|
98 | public static <T> T convertValue(Object source, Class<T> clazz) { | |
|
|
99 | var mapper = jsonMapper(); | |
|
|
100 | var buf = mapper.getSerializerProvider().bufferForValueConversion(jsonMapper()); | |
|
|
101 | ||
|
|
102 | try { | |
|
|
103 | mapper.writeValue(buf, source); | |
|
|
104 | return mapper.readValue(buf.asParser(), clazz); | |
|
|
105 | } catch (IOException e) { | |
|
|
106 | throw new SerializationException(e); | |
|
|
107 | } | |
|
|
108 | } | |
|
|
109 | ||
|
|
110 | public static class SerializationException extends RuntimeException { | |
|
|
111 | public SerializationException() { | |
|
|
112 | } | |
|
|
113 | ||
|
|
114 | public SerializationException(String message) { | |
|
|
115 | super(message); | |
|
|
116 | } | |
|
|
117 | ||
|
|
118 | public SerializationException(Throwable cause) { | |
|
|
119 | super(cause); | |
|
|
120 | } | |
|
|
121 | ||
|
|
122 | public SerializationException(String message, Throwable cause) { | |
|
|
123 | super(message, cause); | |
|
|
124 | } | |
|
|
125 | } | |
|
|
126 | } | |
| @@ -1,41 +1,43 | |||
|
|
1 | 1 | plugins { |
|
|
2 | 2 | id "java-library" |
|
|
3 | 3 | id "ivy-publish" |
|
|
4 | 4 | } |
|
|
5 | 5 | |
|
|
6 | 6 | java { |
|
|
7 | 7 | withJavadocJar() |
|
|
8 | 8 | withSourcesJar() |
|
|
9 | 9 | toolchain { |
|
|
10 |
languageVersion = JavaLanguageVersion.of(1 |
|
|
|
10 | languageVersion = JavaLanguageVersion.of(21) | |
|
|
11 | 11 | } |
|
|
12 | 12 | } |
|
|
13 | 13 | |
|
|
14 | 14 | dependencies { |
|
|
15 | 15 | compileOnly libs.jdt.annotations |
|
|
16 | 16 | |
|
|
17 | implementation libs.bundles.jackson | |
|
|
18 | ||
|
|
17 | 19 | api gradleApi() |
|
|
18 | 20 | } |
|
|
19 | 21 | |
|
|
20 | 22 | task printVersion{ |
|
|
21 | 23 | doLast { |
|
|
22 | 24 | println "project: $project.group:$project.name:$project.version" |
|
|
23 | 25 | println "jar: ${->jar.archiveFileName.get()}" |
|
|
24 | 26 | } |
|
|
25 | 27 | } |
|
|
26 | 28 | |
|
|
27 | 29 | publishing { |
|
|
28 | 30 | repositories { |
|
|
29 | 31 | ivy { |
|
|
30 | 32 | url "${System.properties["user.home"]}/ivy-repo" |
|
|
31 | 33 | } |
|
|
32 | 34 | } |
|
|
33 | 35 | publications { |
|
|
34 | 36 | ivy(IvyPublication) { |
|
|
35 | 37 | from components.java |
|
|
36 | 38 | descriptor.description { |
|
|
37 | 39 | text = providers.provider({ description }) |
|
|
38 | 40 | } |
|
|
39 | 41 | } |
|
|
40 | 42 | } |
|
|
41 | 43 | } No newline at end of file |
| @@ -1,63 +1,51 | |||
|
|
1 | 1 | package org.implab.gradle.common.utils; |
|
|
2 | 2 | |
|
|
3 | 3 | import java.util.HashMap; |
|
|
4 | 4 | import java.util.Map; |
|
|
5 | 5 | import java.util.function.Function; |
|
|
6 | 6 | |
|
|
7 | 7 | import org.gradle.api.Action; |
|
|
8 | 8 | import org.gradle.api.provider.ListProperty; |
|
|
9 | 9 | import org.gradle.api.provider.MapProperty; |
|
|
10 | 10 | import org.gradle.api.provider.Provider; |
|
|
11 | 11 | |
|
|
12 | 12 | public final class Properties { |
|
|
13 | 13 | private Properties() { |
|
|
14 | 14 | } |
|
|
15 | 15 | |
|
|
16 | 16 | public static <K> void mergeMap(MapProperty<K, Object> property, Map<K, Object> map) { |
|
|
17 | map.forEach((k, v) -> { | |
|
|
18 | if (v instanceof Provider<?>) | |
|
|
19 | property.put(k, (Provider<?>) v); | |
|
|
20 | else | |
|
|
21 | property.put(k, v); | |
|
|
22 | }); | |
|
|
17 | mergeMap(property, map, Function.identity()); | |
|
|
23 | 18 | } |
|
|
24 | 19 | |
|
|
25 | public static <K, V> void mergeMap(MapProperty<K, V> property, Map<K, ?> map, Function<Object, V> mapper) { | |
|
|
20 | public static <K, V> void mergeMap(MapProperty<K, V> property, Map<K, ?> map, Function<Object,? extends V> mapper) { | |
|
|
26 | 21 | map.forEach((k, v) -> { |
|
|
27 | 22 | if (v instanceof Provider<?>) |
|
|
28 | 23 | property.put(k, ((Provider<?>) v).map(mapper::apply)); |
|
|
29 | 24 | else |
|
|
30 | 25 | property.put(k, mapper.apply(v)); |
|
|
31 | 26 | }); |
|
|
32 | 27 | } |
|
|
33 | 28 | |
|
|
34 | 29 | public static void mergeList(ListProperty<Object> property, Iterable<Object> values) { |
|
|
35 | values.forEach(v -> { | |
|
|
36 | if (v instanceof Provider<?>) | |
|
|
37 | property.add((Provider<?>) v); | |
|
|
38 | else | |
|
|
39 | property.add(v); | |
|
|
40 | }); | |
|
|
30 | mergeList(property, values, Function.identity()); | |
|
|
41 | 31 | } |
|
|
42 | 32 | |
|
|
43 | public static <V> void mergeList(ListProperty<V> property, Iterable<Object> values, Function<Object, V> mapper) { | |
|
|
33 | public static <V> void mergeList(ListProperty<V> property, Iterable<Object> values, Function<Object, ? extends V> mapper) { | |
|
|
44 | 34 | values.forEach(v -> { |
|
|
45 | 35 | if (v instanceof Provider<?>) |
|
|
46 | 36 | property.add(((Provider<?>) v).map(mapper::apply)); |
|
|
47 | 37 | else |
|
|
48 | 38 | property.add(mapper.apply(v)); |
|
|
49 | 39 | }); |
|
|
50 | 40 | } |
|
|
51 | 41 | |
|
|
52 | 42 | public static <K> void configureMap(MapProperty<K, Object> prop, Action<Map<K, Object>> configure) { |
|
|
53 | var map = new HashMap<K, Object>(); | |
|
|
54 | configure.execute(map); | |
|
|
55 | mergeMap(prop, map); | |
|
|
43 | configureMap(prop, configure, Function.identity()); | |
|
|
56 | 44 | } |
|
|
57 | 45 | |
|
|
58 | 46 | public static <K, V> void configureMap(MapProperty<K, V> prop, Action<Map<K, Object>> configure, Function<Object, V> mapper) { |
|
|
59 | 47 | var map = new HashMap<K, Object>(); |
|
|
60 | 48 | configure.execute(map); |
|
|
61 | 49 | mergeMap(prop, map, mapper); |
|
|
62 | 50 | } |
|
|
63 | 51 | } |
| @@ -1,87 +1,83 | |||
|
|
1 | 1 | package org.implab.gradle.common.utils; |
|
|
2 | 2 | |
|
|
3 | 3 | import java.text.MessageFormat; |
|
|
4 | 4 | import java.util.Iterator; |
|
|
5 | 5 | import java.util.Spliterators; |
|
|
6 | 6 | import java.util.Optional; |
|
|
7 | 7 | import java.util.Set; |
|
|
8 | 8 | import java.util.function.Function; |
|
|
9 | 9 | import java.util.function.Supplier; |
|
|
10 | 10 | import java.util.stream.Collectors; |
|
|
11 | 11 | import java.util.stream.Stream; |
|
|
12 | 12 | import java.util.stream.StreamSupport; |
|
|
13 | 13 | |
|
|
14 | 14 | import org.gradle.api.provider.Provider; |
|
|
15 | 15 | |
|
|
16 | 16 | public final class Values { |
|
|
17 | 17 | |
|
|
18 | 18 | private Values() { |
|
|
19 | 19 | } |
|
|
20 | 20 | |
|
|
21 | 21 | /** |
|
|
22 | 22 | * Converts the supplied value to a string. |
|
|
23 | 23 | */ |
|
|
24 | 24 | public static String toString(Object value) { |
|
|
25 | 25 | if (value == null) { |
|
|
26 | 26 | return null; |
|
|
27 | 27 | } else if (value instanceof String string) { |
|
|
28 | 28 | return string; |
|
|
29 | 29 | } else if (value instanceof Provider<?> provider) { |
|
|
30 | 30 | return toString(provider.getOrNull()); |
|
|
31 | 31 | } else if (value instanceof Supplier<?> supplier) { |
|
|
32 | 32 | return toString(supplier.get()); |
|
|
33 | 33 | } else { |
|
|
34 | 34 | return value.toString(); |
|
|
35 | 35 | } |
|
|
36 | 36 | } |
|
|
37 | 37 | |
|
|
38 | 38 | public static <T> Stream<T> stream(Iterator<T> remaining) { |
|
|
39 | 39 | return StreamSupport.stream( |
|
|
40 | 40 | Spliterators.spliteratorUnknownSize(remaining, 0), |
|
|
41 | 41 | false); |
|
|
42 | 42 | } |
|
|
43 | 43 | |
|
|
44 | 44 | public static <T> Optional<T> take(Iterator<T> iterator) { |
|
|
45 | 45 | return iterator.hasNext() ? Optional.of(iterator.next()) : Optional.empty(); |
|
|
46 | 46 | } |
|
|
47 | 47 | |
|
|
48 | 48 | public static <T> Iterable<T> iterable(T[] values) { |
|
|
49 | 49 | return () -> new ArrayIterator<>(values); |
|
|
50 | 50 | } |
|
|
51 | 51 | |
|
|
52 | 52 | public static <T> Optional<T> optional(Provider<T> provider) { |
|
|
53 | 53 | return provider.isPresent() ? Optional.of(provider.get()) : Optional.empty(); |
|
|
54 | 54 | } |
|
|
55 | 55 | |
|
|
56 | 56 | public static <T> T required(Provider<T> provider, String providerName) { |
|
|
57 | 57 | if (!provider.isPresent()) |
|
|
58 | 58 | throw new IllegalStateException( |
|
|
59 | 59 | MessageFormat.format("The value for the '{0}' provider must be specified", providerName)); |
|
|
60 | 60 | return provider.get(); |
|
|
61 | 61 | } |
|
|
62 | 62 | |
|
|
63 | public static <U,V> Set<V> mapSet(Set<U> values, Function<U,V> mapper) { | |
|
|
64 | return values.stream().map(mapper).collect(Collectors.toUnmodifiableSet()); | |
|
|
65 | } | |
|
|
66 | ||
|
|
67 | 63 | private static class ArrayIterator<T> implements Iterator<T> { |
|
|
68 | 64 | private final T[] data; |
|
|
69 | 65 | |
|
|
70 | 66 | private int pos = 0; |
|
|
71 | 67 | |
|
|
72 | 68 | ArrayIterator(T[] data) { |
|
|
73 | 69 | this.data = data; |
|
|
74 | 70 | } |
|
|
75 | 71 | |
|
|
76 | 72 | @Override |
|
|
77 | 73 | public boolean hasNext() { |
|
|
78 | 74 | return pos < data.length; |
|
|
79 | 75 | } |
|
|
80 | 76 | |
|
|
81 | 77 | @Override |
|
|
82 | 78 | public T next() { |
|
|
83 | 79 | return data[pos++]; |
|
|
84 | 80 | } |
|
|
85 | 81 | } |
|
|
86 | 82 | |
|
|
87 | 83 | } |
| @@ -1,7 +1,17 | |||
|
|
1 | 1 | [versions] |
|
|
2 | 2 | immutables = "2.10.1" |
|
|
3 | 3 | jdt = "2.3.0" |
|
|
4 | jackson = "2.15.4" | |
|
|
4 | 5 | |
|
|
5 | 6 | [libraries] |
|
|
6 | 7 | immutables = { module = "org.immutables:value", version.ref = "immutables" } |
|
|
7 | 8 | jdt-annotations = { module = "org.eclipse.jdt:org.eclipse.jdt.annotation", version.ref = "jdt" } |
|
|
9 | ||
|
|
10 | #jackson | |
|
|
11 | jackson-core = { module = "com.fasterxml.jackson.core:jackson-core", version.ref = "jackson" } | |
|
|
12 | jackson-datatype-jdk8 = { module = "com.fasterxml.jackson.datatype:jackson-datatype-jdk8", version.ref = "jackson" } | |
|
|
13 | jackson-datatype-jsr310 = { module = "com.fasterxml.jackson.datatype:jackson-datatype-jsr310", version.ref = "jackson" } | |
|
|
14 | ||
|
|
15 | [bundles] | |
|
|
16 | ||
|
|
17 | jackson = ["jackson-core", "jackson-datatype-jdk8", "jackson-datatype-jsr310"] No newline at end of file | |
General Comments 0
You need to be logged in to leave comments.
Login now
