| @@ -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 | plugins { |
|
1 | plugins { | |
| 2 | id "java-library" |
|
2 | id "java-library" | |
| 3 | id "ivy-publish" |
|
3 | id "ivy-publish" | |
| 4 | } |
|
4 | } | |
| 5 |
|
5 | |||
| 6 | java { |
|
6 | java { | |
| 7 | withJavadocJar() |
|
7 | withJavadocJar() | |
| 8 | withSourcesJar() |
|
8 | withSourcesJar() | |
| 9 | toolchain { |
|
9 | toolchain { | |
| 10 |
languageVersion = JavaLanguageVersion.of(1 |
|
10 | languageVersion = JavaLanguageVersion.of(21) | |
| 11 | } |
|
11 | } | |
| 12 | } |
|
12 | } | |
| 13 |
|
13 | |||
| 14 | dependencies { |
|
14 | dependencies { | |
| 15 | compileOnly libs.jdt.annotations |
|
15 | compileOnly libs.jdt.annotations | |
| 16 |
|
16 | |||
|
|
17 | implementation libs.bundles.jackson | |||
|
|
18 | ||||
| 17 | api gradleApi() |
|
19 | api gradleApi() | |
| 18 | } |
|
20 | } | |
| 19 |
|
21 | |||
| 20 | task printVersion{ |
|
22 | task printVersion{ | |
| 21 | doLast { |
|
23 | doLast { | |
| 22 | println "project: $project.group:$project.name:$project.version" |
|
24 | println "project: $project.group:$project.name:$project.version" | |
| 23 | println "jar: ${->jar.archiveFileName.get()}" |
|
25 | println "jar: ${->jar.archiveFileName.get()}" | |
| 24 | } |
|
26 | } | |
| 25 | } |
|
27 | } | |
| 26 |
|
28 | |||
| 27 | publishing { |
|
29 | publishing { | |
| 28 | repositories { |
|
30 | repositories { | |
| 29 | ivy { |
|
31 | ivy { | |
| 30 | url "${System.properties["user.home"]}/ivy-repo" |
|
32 | url "${System.properties["user.home"]}/ivy-repo" | |
| 31 | } |
|
33 | } | |
| 32 | } |
|
34 | } | |
| 33 | publications { |
|
35 | publications { | |
| 34 | ivy(IvyPublication) { |
|
36 | ivy(IvyPublication) { | |
| 35 | from components.java |
|
37 | from components.java | |
| 36 | descriptor.description { |
|
38 | descriptor.description { | |
| 37 | text = providers.provider({ description }) |
|
39 | text = providers.provider({ description }) | |
| 38 | } |
|
40 | } | |
| 39 | } |
|
41 | } | |
| 40 | } |
|
42 | } | |
| 41 | } No newline at end of file |
|
43 | } | |
| @@ -1,63 +1,51 | |||||
| 1 | package org.implab.gradle.common.utils; |
|
1 | package org.implab.gradle.common.utils; | |
| 2 |
|
2 | |||
| 3 | import java.util.HashMap; |
|
3 | import java.util.HashMap; | |
| 4 | import java.util.Map; |
|
4 | import java.util.Map; | |
| 5 | import java.util.function.Function; |
|
5 | import java.util.function.Function; | |
| 6 |
|
6 | |||
| 7 | import org.gradle.api.Action; |
|
7 | import org.gradle.api.Action; | |
| 8 | import org.gradle.api.provider.ListProperty; |
|
8 | import org.gradle.api.provider.ListProperty; | |
| 9 | import org.gradle.api.provider.MapProperty; |
|
9 | import org.gradle.api.provider.MapProperty; | |
| 10 | import org.gradle.api.provider.Provider; |
|
10 | import org.gradle.api.provider.Provider; | |
| 11 |
|
11 | |||
| 12 | public final class Properties { |
|
12 | public final class Properties { | |
| 13 | private Properties() { |
|
13 | private Properties() { | |
| 14 | } |
|
14 | } | |
| 15 |
|
15 | |||
| 16 | public static <K> void mergeMap(MapProperty<K, Object> property, Map<K, Object> map) { |
|
16 | public static <K> void mergeMap(MapProperty<K, Object> property, Map<K, Object> map) { | |
| 17 | map.forEach((k, v) -> { |
|
17 | mergeMap(property, map, Function.identity()); | |
| 18 | if (v instanceof Provider<?>) |
|
|||
| 19 | property.put(k, (Provider<?>) v); |
|
|||
| 20 | else |
|
|||
| 21 | property.put(k, v); |
|
|||
| 22 | }); |
|
|||
| 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 | map.forEach((k, v) -> { |
|
21 | map.forEach((k, v) -> { | |
| 27 | if (v instanceof Provider<?>) |
|
22 | if (v instanceof Provider<?>) | |
| 28 | property.put(k, ((Provider<?>) v).map(mapper::apply)); |
|
23 | property.put(k, ((Provider<?>) v).map(mapper::apply)); | |
| 29 | else |
|
24 | else | |
| 30 | property.put(k, mapper.apply(v)); |
|
25 | property.put(k, mapper.apply(v)); | |
| 31 | }); |
|
26 | }); | |
| 32 | } |
|
27 | } | |
| 33 |
|
28 | |||
| 34 | public static void mergeList(ListProperty<Object> property, Iterable<Object> values) { |
|
29 | public static void mergeList(ListProperty<Object> property, Iterable<Object> values) { | |
| 35 | values.forEach(v -> { |
|
30 | mergeList(property, values, Function.identity()); | |
| 36 | if (v instanceof Provider<?>) |
|
|||
| 37 | property.add((Provider<?>) v); |
|
|||
| 38 | else |
|
|||
| 39 | property.add(v); |
|
|||
| 40 | }); |
|
|||
| 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 | values.forEach(v -> { |
|
34 | values.forEach(v -> { | |
| 45 | if (v instanceof Provider<?>) |
|
35 | if (v instanceof Provider<?>) | |
| 46 | property.add(((Provider<?>) v).map(mapper::apply)); |
|
36 | property.add(((Provider<?>) v).map(mapper::apply)); | |
| 47 | else |
|
37 | else | |
| 48 | property.add(mapper.apply(v)); |
|
38 | property.add(mapper.apply(v)); | |
| 49 | }); |
|
39 | }); | |
| 50 | } |
|
40 | } | |
| 51 |
|
41 | |||
| 52 | public static <K> void configureMap(MapProperty<K, Object> prop, Action<Map<K, Object>> configure) { |
|
42 | public static <K> void configureMap(MapProperty<K, Object> prop, Action<Map<K, Object>> configure) { | |
| 53 | var map = new HashMap<K, Object>(); |
|
43 | configureMap(prop, configure, Function.identity()); | |
| 54 | configure.execute(map); |
|
|||
| 55 | mergeMap(prop, map); |
|
|||
| 56 | } |
|
44 | } | |
| 57 |
|
45 | |||
| 58 | public static <K, V> void configureMap(MapProperty<K, V> prop, Action<Map<K, Object>> configure, Function<Object, V> mapper) { |
|
46 | public static <K, V> void configureMap(MapProperty<K, V> prop, Action<Map<K, Object>> configure, Function<Object, V> mapper) { | |
| 59 | var map = new HashMap<K, Object>(); |
|
47 | var map = new HashMap<K, Object>(); | |
| 60 | configure.execute(map); |
|
48 | configure.execute(map); | |
| 61 | mergeMap(prop, map, mapper); |
|
49 | mergeMap(prop, map, mapper); | |
| 62 | } |
|
50 | } | |
| 63 | } |
|
51 | } | |
| @@ -1,87 +1,83 | |||||
| 1 | package org.implab.gradle.common.utils; |
|
1 | package org.implab.gradle.common.utils; | |
| 2 |
|
2 | |||
| 3 | import java.text.MessageFormat; |
|
3 | import java.text.MessageFormat; | |
| 4 | import java.util.Iterator; |
|
4 | import java.util.Iterator; | |
| 5 | import java.util.Spliterators; |
|
5 | import java.util.Spliterators; | |
| 6 | import java.util.Optional; |
|
6 | import java.util.Optional; | |
| 7 | import java.util.Set; |
|
7 | import java.util.Set; | |
| 8 | import java.util.function.Function; |
|
8 | import java.util.function.Function; | |
| 9 | import java.util.function.Supplier; |
|
9 | import java.util.function.Supplier; | |
| 10 | import java.util.stream.Collectors; |
|
10 | import java.util.stream.Collectors; | |
| 11 | import java.util.stream.Stream; |
|
11 | import java.util.stream.Stream; | |
| 12 | import java.util.stream.StreamSupport; |
|
12 | import java.util.stream.StreamSupport; | |
| 13 |
|
13 | |||
| 14 | import org.gradle.api.provider.Provider; |
|
14 | import org.gradle.api.provider.Provider; | |
| 15 |
|
15 | |||
| 16 | public final class Values { |
|
16 | public final class Values { | |
| 17 |
|
17 | |||
| 18 | private Values() { |
|
18 | private Values() { | |
| 19 | } |
|
19 | } | |
| 20 |
|
20 | |||
| 21 | /** |
|
21 | /** | |
| 22 | * Converts the supplied value to a string. |
|
22 | * Converts the supplied value to a string. | |
| 23 | */ |
|
23 | */ | |
| 24 | public static String toString(Object value) { |
|
24 | public static String toString(Object value) { | |
| 25 | if (value == null) { |
|
25 | if (value == null) { | |
| 26 | return null; |
|
26 | return null; | |
| 27 | } else if (value instanceof String string) { |
|
27 | } else if (value instanceof String string) { | |
| 28 | return string; |
|
28 | return string; | |
| 29 | } else if (value instanceof Provider<?> provider) { |
|
29 | } else if (value instanceof Provider<?> provider) { | |
| 30 | return toString(provider.getOrNull()); |
|
30 | return toString(provider.getOrNull()); | |
| 31 | } else if (value instanceof Supplier<?> supplier) { |
|
31 | } else if (value instanceof Supplier<?> supplier) { | |
| 32 | return toString(supplier.get()); |
|
32 | return toString(supplier.get()); | |
| 33 | } else { |
|
33 | } else { | |
| 34 | return value.toString(); |
|
34 | return value.toString(); | |
| 35 | } |
|
35 | } | |
| 36 | } |
|
36 | } | |
| 37 |
|
37 | |||
| 38 | public static <T> Stream<T> stream(Iterator<T> remaining) { |
|
38 | public static <T> Stream<T> stream(Iterator<T> remaining) { | |
| 39 | return StreamSupport.stream( |
|
39 | return StreamSupport.stream( | |
| 40 | Spliterators.spliteratorUnknownSize(remaining, 0), |
|
40 | Spliterators.spliteratorUnknownSize(remaining, 0), | |
| 41 | false); |
|
41 | false); | |
| 42 | } |
|
42 | } | |
| 43 |
|
43 | |||
| 44 | public static <T> Optional<T> take(Iterator<T> iterator) { |
|
44 | public static <T> Optional<T> take(Iterator<T> iterator) { | |
| 45 | return iterator.hasNext() ? Optional.of(iterator.next()) : Optional.empty(); |
|
45 | return iterator.hasNext() ? Optional.of(iterator.next()) : Optional.empty(); | |
| 46 | } |
|
46 | } | |
| 47 |
|
47 | |||
| 48 | public static <T> Iterable<T> iterable(T[] values) { |
|
48 | public static <T> Iterable<T> iterable(T[] values) { | |
| 49 | return () -> new ArrayIterator<>(values); |
|
49 | return () -> new ArrayIterator<>(values); | |
| 50 | } |
|
50 | } | |
| 51 |
|
51 | |||
| 52 | public static <T> Optional<T> optional(Provider<T> provider) { |
|
52 | public static <T> Optional<T> optional(Provider<T> provider) { | |
| 53 | return provider.isPresent() ? Optional.of(provider.get()) : Optional.empty(); |
|
53 | return provider.isPresent() ? Optional.of(provider.get()) : Optional.empty(); | |
| 54 | } |
|
54 | } | |
| 55 |
|
55 | |||
| 56 | public static <T> T required(Provider<T> provider, String providerName) { |
|
56 | public static <T> T required(Provider<T> provider, String providerName) { | |
| 57 | if (!provider.isPresent()) |
|
57 | if (!provider.isPresent()) | |
| 58 | throw new IllegalStateException( |
|
58 | throw new IllegalStateException( | |
| 59 | MessageFormat.format("The value for the '{0}' provider must be specified", providerName)); |
|
59 | MessageFormat.format("The value for the '{0}' provider must be specified", providerName)); | |
| 60 | return provider.get(); |
|
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 | private static class ArrayIterator<T> implements Iterator<T> { |
|
63 | private static class ArrayIterator<T> implements Iterator<T> { | |
| 68 | private final T[] data; |
|
64 | private final T[] data; | |
| 69 |
|
65 | |||
| 70 | private int pos = 0; |
|
66 | private int pos = 0; | |
| 71 |
|
67 | |||
| 72 | ArrayIterator(T[] data) { |
|
68 | ArrayIterator(T[] data) { | |
| 73 | this.data = data; |
|
69 | this.data = data; | |
| 74 | } |
|
70 | } | |
| 75 |
|
71 | |||
| 76 | @Override |
|
72 | @Override | |
| 77 | public boolean hasNext() { |
|
73 | public boolean hasNext() { | |
| 78 | return pos < data.length; |
|
74 | return pos < data.length; | |
| 79 | } |
|
75 | } | |
| 80 |
|
76 | |||
| 81 | @Override |
|
77 | @Override | |
| 82 | public T next() { |
|
78 | public T next() { | |
| 83 | return data[pos++]; |
|
79 | return data[pos++]; | |
| 84 | } |
|
80 | } | |
| 85 | } |
|
81 | } | |
| 86 |
|
82 | |||
| 87 | } |
|
83 | } | |
| @@ -1,7 +1,17 | |||||
| 1 | [versions] |
|
1 | [versions] | |
| 2 | immutables = "2.10.1" |
|
2 | immutables = "2.10.1" | |
| 3 | jdt = "2.3.0" |
|
3 | jdt = "2.3.0" | |
|
|
4 | jackson = "2.15.4" | |||
| 4 |
|
5 | |||
| 5 | [libraries] |
|
6 | [libraries] | |
| 6 | immutables = { module = "org.immutables:value", version.ref = "immutables" } |
|
7 | immutables = { module = "org.immutables:value", version.ref = "immutables" } | |
| 7 | jdt-annotations = { module = "org.eclipse.jdt:org.eclipse.jdt.annotation", version.ref = "jdt" } |
|
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
