View Javadoc
1   /*
2    * Portions of this software was developed by employees of the National Institute
3    * of Standards and Technology (NIST), an agency of the Federal Government and is
4    * being made available as a public service. Pursuant to title 17 United States
5    * Code Section 105, works of NIST employees are not subject to copyright
6    * protection in the United States. This software may be subject to foreign
7    * copyright. Permission in the United States and in foreign countries, to the
8    * extent that NIST may hold copyright, to use, copy, modify, create derivative
9    * works, and distribute this software and its documentation without fee is hereby
10   * granted on a non-exclusive basis, provided that this notice and disclaimer
11   * of warranty appears in all copies.
12   *
13   * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
14   * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
15   * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
17   * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
18   * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
19   * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
20   * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
21   * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
22   * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
23   * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
24   * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
25   */
26  
27  package gov.nist.secauto.oscal.lib.profile.resolver.support;
28  
29  import gov.nist.secauto.metaschema.model.common.metapath.item.IRequiredValueModelNodeItem;
30  import gov.nist.secauto.metaschema.model.common.util.ObjectUtils;
31  
32  import java.net.URI;
33  
34  import edu.umd.cs.findbugs.annotations.NonNull;
35  
36  public abstract class AbstractEntityItem implements IEntityItem {
37  
38    @NonNull
39    private final String originalIdentifier;
40    @NonNull
41    private IRequiredValueModelNodeItem instance;
42    @NonNull
43    private final ItemType itemType;
44    @NonNull
45    private final URI source;
46    private int referenceCount; // 0 by default
47    // private boolean resolved; // false by default
48  
49    protected AbstractEntityItem(@NonNull Builder builder) {
50      this.itemType = ObjectUtils.requireNonNull(builder.itemType, "itemType");
51      this.originalIdentifier = ObjectUtils.requireNonNull(builder.originalIdentifier, "originalIdentifier");
52      this.instance = ObjectUtils.requireNonNull(builder.instance, "instance");
53      this.source = ObjectUtils.requireNonNull(builder.source, "source");
54    }
55  
56    @Override
57    @NonNull
58    public String getOriginalIdentifier() {
59      return originalIdentifier;
60    }
61  
62    @Override
63    @NonNull
64    public abstract String getIdentifier();
65  
66    // @NonNull
67    // public String getIdentifier() {
68    // final String checkedReassignedIdentifier = reassignedIdentifier;
69    // return checkedReassignedIdentifier == null ? originalIdentifier :
70    // checkedReassignedIdentifier;
71    // }
72  
73    @Override
74    @NonNull
75    public IRequiredValueModelNodeItem getInstance() {
76      return instance;
77    }
78  
79    @Override
80    public void setInstance(IRequiredValueModelNodeItem item) {
81      instance = item;
82    }
83  
84    @Override
85    @NonNull
86    @SuppressWarnings("unchecked")
87    public <T> T getInstanceValue() {
88      return (T) getInstance().getValue();
89    }
90  
91    @Override
92    @NonNull
93    public ItemType getItemType() {
94      return itemType;
95    }
96  
97    @Override
98    @NonNull
99    public URI getSource() {
100     return source;
101   }
102 
103   @Override
104   public int getReferenceCount() {
105     return referenceCount;
106   }
107 
108   // public boolean isResolved() {
109   // return resolved;
110   // }
111   //
112   // public void markResolved() {
113   // resolved = true;
114   // }
115 
116   @Override
117   public void incrementReferenceCount() {
118     referenceCount += 1;
119   }
120 
121   @Override
122   public int resetReferenceCount() {
123     int retval = referenceCount;
124     referenceCount = 0;
125     return retval;
126   }
127 
128   static final class Builder {
129     private String originalIdentifier;
130     private String reassignedIdentifier;
131     private IRequiredValueModelNodeItem instance;
132     private ItemType itemType;
133     private URI source;
134 
135     @NonNull
136     public Builder instance(@NonNull IRequiredValueModelNodeItem item, @NonNull ItemType itemType) {
137       this.instance = item;
138       this.itemType = itemType;
139       return this;
140     }
141 
142     // @NonNull
143     // public Builder reassignedIdentifier(@NonNull UUID identifier) {
144     // // no need to normalize, since UUIDs are formatted lower case
145     // return reassignedIdentifier(identifier.toString());
146     // }
147 
148     @NonNull
149     public Builder reassignedIdentifier(@NonNull String identifier) {
150       this.reassignedIdentifier = identifier;
151       return this;
152     }
153     //
154     // @NonNull
155     // public Builder originalIdentifier(@NonNull UUID identifier) {
156     // // no need to normalize, since UUIDs are formatted lower case
157     // return originalIdentifier(identifier.toString());
158     // }
159 
160     @NonNull
161     public Builder originalIdentifier(@NonNull String identifier) {
162       this.originalIdentifier = identifier;
163       return this;
164     }
165 
166     @NonNull
167     public Builder source(@NonNull URI source) {
168       this.source = source;
169       return this;
170     }
171 
172     @NonNull
173     public IEntityItem build() {
174       return reassignedIdentifier == null ? new OriginalEntityItem(this) : new ReassignedEntityItem(this);
175     }
176   }
177 
178   static final class OriginalEntityItem
179       extends AbstractEntityItem {
180 
181     protected OriginalEntityItem(@NonNull Builder builder) {
182       super(builder);
183     }
184 
185     @Override
186     public String getIdentifier() {
187       return getOriginalIdentifier();
188     }
189 
190     @Override
191     public boolean isIdentifierReassigned() {
192       return false;
193     }
194   }
195 
196   static final class ReassignedEntityItem
197       extends AbstractEntityItem {
198     @NonNull
199     private final String reassignedIdentifier;
200 
201     protected ReassignedEntityItem(@NonNull Builder builder) {
202       super(builder);
203       this.reassignedIdentifier = ObjectUtils.requireNonNull(builder.reassignedIdentifier);
204     }
205 
206     @Override
207     public String getIdentifier() {
208       return reassignedIdentifier;
209     }
210 
211     @Override
212     public boolean isIdentifierReassigned() {
213       return true;
214     }
215   }
216 }