001/*
002 * Portions of this software was developed by employees of the National Institute
003 * of Standards and Technology (NIST), an agency of the Federal Government and is
004 * being made available as a public service. Pursuant to title 17 United States
005 * Code Section 105, works of NIST employees are not subject to copyright
006 * protection in the United States. This software may be subject to foreign
007 * copyright. Permission in the United States and in foreign countries, to the
008 * extent that NIST may hold copyright, to use, copy, modify, create derivative
009 * works, and distribute this software and its documentation without fee is hereby
010 * granted on a non-exclusive basis, provided that this notice and disclaimer
011 * of warranty appears in all copies.
012 *
013 * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER
014 * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY
015 * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF
016 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM
017 * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE
018 * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE.  IN NO EVENT
019 * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT,
020 * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM,
021 * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY,
022 * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR
023 * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT
024 * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER.
025 */
026
027package gov.nist.secauto.oscal.lib.profile.resolver.support;
028
029import gov.nist.secauto.metaschema.model.common.metapath.item.IRequiredValueModelNodeItem;
030import gov.nist.secauto.metaschema.model.common.util.ObjectUtils;
031
032import java.net.URI;
033
034import edu.umd.cs.findbugs.annotations.NonNull;
035
036public abstract class AbstractEntityItem implements IEntityItem {
037
038  @NonNull
039  private final String originalIdentifier;
040  @NonNull
041  private IRequiredValueModelNodeItem instance;
042  @NonNull
043  private final ItemType itemType;
044  @NonNull
045  private final URI source;
046  private int referenceCount; // 0 by default
047  // private boolean resolved; // false by default
048
049  protected AbstractEntityItem(@NonNull Builder builder) {
050    this.itemType = ObjectUtils.requireNonNull(builder.itemType, "itemType");
051    this.originalIdentifier = ObjectUtils.requireNonNull(builder.originalIdentifier, "originalIdentifier");
052    this.instance = ObjectUtils.requireNonNull(builder.instance, "instance");
053    this.source = ObjectUtils.requireNonNull(builder.source, "source");
054  }
055
056  @Override
057  @NonNull
058  public String getOriginalIdentifier() {
059    return originalIdentifier;
060  }
061
062  @Override
063  @NonNull
064  public abstract String getIdentifier();
065
066  // @NonNull
067  // public String getIdentifier() {
068  // final String checkedReassignedIdentifier = reassignedIdentifier;
069  // return checkedReassignedIdentifier == null ? originalIdentifier :
070  // checkedReassignedIdentifier;
071  // }
072
073  @Override
074  @NonNull
075  public IRequiredValueModelNodeItem getInstance() {
076    return instance;
077  }
078
079  @Override
080  public void setInstance(IRequiredValueModelNodeItem item) {
081    instance = item;
082  }
083
084  @Override
085  @NonNull
086  @SuppressWarnings("unchecked")
087  public <T> T getInstanceValue() {
088    return (T) getInstance().getValue();
089  }
090
091  @Override
092  @NonNull
093  public ItemType getItemType() {
094    return itemType;
095  }
096
097  @Override
098  @NonNull
099  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}