Skip to content

Method: static {...}

1: /*
2: * JB4JSON-LD
3: * Copyright (C) 2023 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.reference;
19:
20: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
21: import cz.cvut.kbss.jsonld.exception.TargetTypeException;
22:
23: import java.lang.reflect.Field;
24: import java.util.Objects;
25:
26: /**
27: * Represents a singular pending reference.
28: * <p>
29: * That is, a singular attribute referencing an object.
30: */
31: public final class SingularPendingReference implements PendingReference {
32:
33: private final Object targetObject;
34:
35: private final Field targetField;
36:
37: public SingularPendingReference(Object targetObject, Field targetField) {
38: this.targetObject = Objects.requireNonNull(targetObject);
39: this.targetField = Objects.requireNonNull(targetField);
40: }
41:
42: @Override
43: public void apply(Object referencedObject) {
44: assert referencedObject != null;
45: if (!targetField.getType().isAssignableFrom(referencedObject.getClass())) {
46: throw new TargetTypeException(
47: "Cannot assign referenced object " + referencedObject + " of type " + referencedObject
48: .getClass() + " to field " + targetField);
49: }
50: BeanClassProcessor.setFieldValue(targetField, targetObject, referencedObject);
51: }
52:
53: @Override
54: public boolean equals(Object o) {
55: if (this == o) {
56: return true;
57: }
58: if (o == null || getClass() != o.getClass()) {
59: return false;
60: }
61: SingularPendingReference that = (SingularPendingReference) o;
62: return targetObject.equals(that.targetObject) && targetField.equals(that.targetField);
63: }
64:
65: @Override
66: public int hashCode() {
67: return Objects.hash(targetObject, targetField);
68: }
69: }