1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.mal_lang.langspec;
18
19 import static java.util.Objects.requireNonNull;
20
21 import jakarta.json.Json;
22 import jakarta.json.JsonObject;
23 import java.util.Map;
24 import org.mal_lang.langspec.builders.AssociationBuilder;
25
26
27
28
29
30
31 public final class Association {
32 private final String name;
33 private final Meta meta;
34 private final Field leftField;
35 private final Field rightField;
36
37 private Association(String name, Meta meta, Field leftField, Field rightField) {
38 this.name = requireNonNull(name);
39 this.meta = requireNonNull(meta);
40 this.leftField = requireNonNull(leftField);
41 this.rightField = requireNonNull(rightField);
42 leftField.setAssociation(this);
43 rightField.setAssociation(this);
44 }
45
46
47
48
49
50
51
52 public String getName() {
53 return this.name;
54 }
55
56
57
58
59
60
61
62 public Meta getMeta() {
63 return this.meta;
64 }
65
66
67
68
69
70
71
72 public Field getLeftField() {
73 return this.leftField;
74 }
75
76
77
78
79
80
81
82 public Field getRightField() {
83 return this.rightField;
84 }
85
86 JsonObject toJson() {
87 return Json.createObjectBuilder()
88 .add("name", this.name)
89 .add("meta", this.meta.toJson())
90 .add("leftAsset", this.rightField.getAsset().getName())
91 .add("leftField", this.leftField.getName())
92 .add("leftMultiplicity", this.leftField.getMultiplicity().toJson())
93 .add("rightAsset", this.leftField.getAsset().getName())
94 .add("rightField", this.rightField.getName())
95 .add("rightMultiplicity", this.rightField.getMultiplicity().toJson())
96 .build();
97 }
98
99 static Association fromBuilder(AssociationBuilder builder, Map<String, Asset> assets) {
100 requireNonNull(builder);
101 requireNonNull(assets);
102 if (!assets.containsKey(builder.getLeftAsset())) {
103 throw new IllegalArgumentException(
104 String.format("Asset \"%s\" not found", builder.getLeftAsset()));
105 }
106 if (!assets.containsKey(builder.getRightAsset())) {
107 throw new IllegalArgumentException(
108 String.format("Asset \"%s\" not found", builder.getRightAsset()));
109 }
110 var leftField =
111 new Field(
112 builder.getLeftField(),
113 assets.get(builder.getRightAsset()),
114 builder.getLeftMultiplicity());
115 var rightField =
116 new Field(
117 builder.getRightField(),
118 assets.get(builder.getLeftAsset()),
119 builder.getRightMultiplicity());
120 leftField.setTarget(rightField);
121 rightField.setTarget(leftField);
122 return new Association(
123 builder.getName(), Meta.fromBuilder(builder.getMeta()), leftField, rightField);
124 }
125 }