Skip to content

Commit c535364

Browse files
committed
fix (core): Merge lists in ThingMerger
1 parent c01a4b2 commit c535364

File tree

5 files changed

+85
-16
lines changed

5 files changed

+85
-16
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# Copyright 2023 The Enola <https://enola.dev> Authors
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
load("@rules_java//java:defs.bzl", "java_library")
18+
19+
java_library(
20+
name = "collect",
21+
srcs = glob(
22+
["*.java"],
23+
exclude = ["*Test.java"],
24+
),
25+
visibility = ["//:__subpackages__"],
26+
deps = [
27+
"@maven//:com_google_errorprone_error_prone_annotations",
28+
"@maven//:com_google_guava_guava",
29+
"@maven//:org_jspecify_jspecify",
30+
],
31+
)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* Copyright 2024 The Enola <https://enola.dev> Authors
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package dev.enola.common.collect;
19+
20+
import com.google.common.collect.ImmutableList;
21+
22+
import java.util.List;
23+
24+
public final class Immutables {
25+
26+
public static <T> ImmutableList<T> join(List<T> a, List<T> b) {
27+
var builder = ImmutableList.<T>builderWithExpectedSize(a.size() + b.size());
28+
return builder.addAll(a).addAll(b).build();
29+
}
30+
31+
private Immutables() {}
32+
}

java/dev/enola/thing/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ java_library(
7171
deps = [
7272
":thing_java_proto",
7373
"//java/dev/enola/common",
74+
"//java/dev/enola/common/collect",
7475
"//java/dev/enola/common/convert",
7576
"//java/dev/enola/common/function",
7677
"//java/dev/enola/common/io",

java/dev/enola/thing/io/Loader.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.slf4j.LoggerFactory;
2929

3030
import java.io.IOException;
31+
import java.util.List;
3132
import java.util.stream.Stream;
3233

3334
public class Loader implements ConverterInto<Stream<ReadableResource>, Store<?, Thing>> {
@@ -58,7 +59,7 @@ private void load(ReadableResource resource, Store<?, Thing> store) {
5859
things.get()
5960
.forEach(
6061
thingBuilder -> {
61-
thingBuilder.set(KIRI.E.ORIGIN, resource.uri());
62+
thingBuilder.set(KIRI.E.ORIGIN, List.of(resource.uri()));
6263
var thing = thingBuilder.build();
6364
store.merge(thing);
6465
});

java/dev/enola/thing/repo/ThingMerger.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,42 @@
1717
*/
1818
package dev.enola.thing.repo;
1919

20+
import static dev.enola.common.collect.Immutables.join;
21+
2022
import dev.enola.thing.KIRI;
2123
import dev.enola.thing.Thing;
2224

25+
import java.util.List;
26+
2327
class ThingMerger {
2428
// TODO Implement missing ThingMergerTest coverage!
2529

26-
public static Thing merge(Thing existing, Thing update) {
27-
if (!existing.iri().equals(update.iri())) throw new IllegalArgumentException();
30+
public static Thing merge(Thing t1, Thing t2) {
31+
if (!t1.iri().equals(t2.iri())) throw new IllegalArgumentException();
2832

29-
if (existing.predicateIRIs().isEmpty()) return update;
30-
if (update.predicateIRIs().isEmpty()) return existing;
33+
if (t1.predicateIRIs().isEmpty()) return t2;
34+
if (t2.predicateIRIs().isEmpty()) return t1;
3135

32-
var merged = existing.copy();
33-
var properties = update.properties();
36+
var merged = t1.copy();
37+
var properties = t2.properties();
3438
properties.forEach(
35-
(predicate, value) -> {
36-
var old = existing.get(predicate);
37-
if (old == null) merged.set(predicate, value, update.datatype(predicate));
38-
else if (old.equals(value)) {
39+
(predicate, t2obj) -> {
40+
var t1obj = t1.get(predicate);
41+
if (t1obj == null) merged.set(predicate, t2obj, t2.datatype(predicate));
42+
else if (t1obj.equals(t2obj)) {
3943
// That's fine!
40-
} else if (predicate.equals(KIRI.E.ORIGIN)) {
41-
// TODO Implement merging both into a List, with test coverage!
44+
} else if (t1obj instanceof List t1list && t2obj instanceof List t2list) {
45+
merged.set(predicate, join(t1list, t2list));
4246
} else
4347
throw new IllegalStateException(
4448
"Cannot merge "
4549
+ predicate
4650
+ " of an "
47-
+ existing.iri()
51+
+ t1.iri()
4852
+ " from "
49-
+ existing.getString(KIRI.E.ORIGIN)
53+
+ t1.getString(KIRI.E.ORIGIN)
5054
+ " and "
51-
+ update.getString(KIRI.E.ORIGIN));
55+
+ t2.getString(KIRI.E.ORIGIN));
5256
});
5357
return merged.build();
5458
}

0 commit comments

Comments
 (0)