1
2
3
4
5
6 package gov.nist.secauto.oscal.lib.profile.resolver.support;
7
8 import gov.nist.secauto.metaschema.core.metapath.item.node.IModelNodeItem;
9 import gov.nist.secauto.metaschema.core.util.ObjectUtils;
10
11 import java.net.URI;
12
13 import edu.umd.cs.findbugs.annotations.NonNull;
14
15 public abstract class AbstractEntityItem implements IEntityItem {
16
17 @NonNull
18 private final String originalIdentifier;
19 @NonNull
20 private IModelNodeItem<?, ?> instance;
21 @NonNull
22 private final ItemType itemType;
23 @NonNull
24 private final URI source;
25 private int referenceCount;
26
27
28 protected AbstractEntityItem(@NonNull Builder builder) {
29 this.itemType = ObjectUtils.requireNonNull(builder.itemType, "itemType");
30 this.originalIdentifier = ObjectUtils.requireNonNull(builder.originalIdentifier, "originalIdentifier");
31 this.instance = ObjectUtils.requireNonNull(builder.instance, "instance");
32 this.source = ObjectUtils.requireNonNull(builder.source, "source");
33 }
34
35 @Override
36 @NonNull
37 public String getOriginalIdentifier() {
38 return originalIdentifier;
39 }
40
41 @Override
42 @NonNull
43 public abstract String getIdentifier();
44
45
46
47
48
49
50
51
52 @Override
53 @NonNull
54 public IModelNodeItem<?, ?> getInstance() {
55 return instance;
56 }
57
58 @Override
59 public void setInstance(IModelNodeItem<?, ?> item) {
60 instance = item;
61 }
62
63 @Override
64 @NonNull
65 @SuppressWarnings("unchecked")
66 public <T> T getInstanceValue() {
67 return ObjectUtils.notNull((T) getInstance().getValue());
68 }
69
70 @Override
71 @NonNull
72 public ItemType getItemType() {
73 return itemType;
74 }
75
76 @Override
77 @NonNull
78 public URI getSource() {
79 return source;
80 }
81
82 @Override
83 public int getReferenceCount() {
84 return referenceCount;
85 }
86
87
88
89
90
91
92
93
94
95 @Override
96 public void incrementReferenceCount() {
97 referenceCount += 1;
98 }
99
100 @Override
101 public int resetReferenceCount() {
102 int retval = referenceCount;
103 referenceCount = 0;
104 return retval;
105 }
106
107 static final class Builder {
108 private String originalIdentifier;
109 private String reassignedIdentifier;
110 private IModelNodeItem<?, ?> instance;
111 private ItemType itemType;
112 private URI source;
113
114 @NonNull
115 public Builder instance(@NonNull IModelNodeItem<?, ?> item, @NonNull ItemType itemType) {
116 this.instance = item;
117 this.itemType = itemType;
118 return this;
119 }
120
121
122
123
124
125
126
127 @NonNull
128 public Builder reassignedIdentifier(@NonNull String identifier) {
129 this.reassignedIdentifier = identifier;
130 return this;
131 }
132
133
134
135
136
137
138
139 @NonNull
140 public Builder originalIdentifier(@NonNull String identifier) {
141 this.originalIdentifier = identifier;
142 return this;
143 }
144
145 @NonNull
146 public Builder source(@NonNull URI source) {
147 this.source = source;
148 return this;
149 }
150
151 @NonNull
152 public IEntityItem build() {
153 return reassignedIdentifier == null ? new OriginalEntityItem(this) : new ReassignedEntityItem(this);
154 }
155 }
156
157 static final class OriginalEntityItem
158 extends AbstractEntityItem {
159
160 protected OriginalEntityItem(@NonNull Builder builder) {
161 super(builder);
162 }
163
164 @Override
165 public String getIdentifier() {
166 return getOriginalIdentifier();
167 }
168
169 @Override
170 public boolean isIdentifierReassigned() {
171 return false;
172 }
173 }
174
175 static final class ReassignedEntityItem
176 extends AbstractEntityItem {
177 @NonNull
178 private final String reassignedIdentifier;
179
180 protected ReassignedEntityItem(@NonNull Builder builder) {
181 super(builder);
182 this.reassignedIdentifier = ObjectUtils.requireNonNull(builder.reassignedIdentifier);
183 }
184
185 @Override
186 public String getIdentifier() {
187 return reassignedIdentifier;
188 }
189
190 @Override
191 public boolean isIdentifierReassigned() {
192 return true;
193 }
194 }
195 }