Skip to content

Method: lambda$getFirstAvailable$1()

1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2024 Czech Technical University in Prague
4: *
5: * This library is free software; you can redistribute it and/or
6: * modify it under the terms of the GNU Lesser General Public
7: * License as published by the Free Software Foundation; either
8: * version 3.0 of the License, or (at your option) any later version.
9: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library.
17: */
18: package cz.cvut.kbss.jsonld.deserialization;
19:
20: import cz.cvut.kbss.jopa.model.MultilingualString;
21: import cz.cvut.kbss.jsonld.deserialization.util.LangString;
22:
23: import java.util.Collection;
24: import java.util.Map;
25:
26: class MultilingualStringCollectionContext<T extends Collection<MultilingualString>> extends InstanceContext<T> {
27:
28: MultilingualStringCollectionContext(T instance, Map<String, Object> knownInstances) {
29: super(instance, knownInstances);
30: }
31:
32: @Override
33: void addItem(Object item) {
34: assert item != null;
35: if (item instanceof LangString) {
36: final LangString value = (LangString) item;
37: final String language = value.getLanguage().orElse(null);
38: final MultilingualString element = getFirstAvailable(language);
39: element.set(language, value.getValue());
40: } else {
41: final MultilingualString element = getFirstAvailable(null);
42: element.set(item.toString());
43: }
44: }
45:
46: private MultilingualString getFirstAvailable(String language) {
47: return instance.stream().filter(ms -> !ms.contains(language)).findFirst().orElseGet(() -> {
48: final MultilingualString newOne = new MultilingualString();
49: instance.add(newOne);
50: return newOne;
51: });
52: }
53:
54: @Override
55: Class<?> getItemType() {
56: return MultilingualString.class;
57: }
58: }