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.metaschema.databind.model;
28  
29  import gov.nist.secauto.metaschema.core.model.IChoiceInstance;
30  import gov.nist.secauto.metaschema.core.model.IModelContainerSupport;
31  import gov.nist.secauto.metaschema.core.util.CollectionUtil;
32  import gov.nist.secauto.metaschema.core.util.CustomCollectors;
33  import gov.nist.secauto.metaschema.core.util.ObjectUtils;
34  import gov.nist.secauto.metaschema.databind.IBindingContext;
35  import gov.nist.secauto.metaschema.databind.model.annotations.BoundAssembly;
36  import gov.nist.secauto.metaschema.databind.model.annotations.BoundField;
37  import gov.nist.secauto.metaschema.databind.model.annotations.Ignore;
38  
39  import java.util.Arrays;
40  import java.util.Collection;
41  import java.util.LinkedHashMap;
42  import java.util.List;
43  import java.util.Map;
44  import java.util.Objects;
45  import java.util.function.Function;
46  import java.util.stream.Collectors;
47  import java.util.stream.Stream;
48  
49  import edu.umd.cs.findbugs.annotations.NonNull;
50  
51  class ClassBindingModelContainerSupport
52      implements IModelContainerSupport<IBoundNamedModelInstance, IBoundNamedModelInstance, IBoundFieldInstance,
53          IBoundAssemblyInstance, IChoiceInstance> {
54    @NonNull
55    private final Map<String, IBoundNamedModelInstance> modelInstances;
56    @NonNull
57    private final Map<String, IBoundFieldInstance> fieldInstances;
58    @NonNull
59    private final Map<String, IBoundAssemblyInstance> assemblyInstances;
60  
61    public ClassBindingModelContainerSupport(
62        @NonNull IAssemblyClassBinding classBinding) {
63      Class<?> clazz = classBinding.getBoundClass();
64      this.modelInstances = CollectionUtil.unmodifiableMap(ObjectUtils.notNull(
65          getModelInstanceFieldStream(classBinding, clazz)
66              .collect(Collectors.toMap(instance -> instance.getEffectiveName(), Function.identity(),
67                  CustomCollectors.useLastMapper(),
68                  LinkedHashMap::new))));
69  
70      this.fieldInstances = CollectionUtil.unmodifiableMap(ObjectUtils.notNull(
71          getNamedModelInstanceMap().values().stream()
72              .filter(instance -> instance instanceof IBoundFieldInstance)
73              .map(instance -> (IBoundFieldInstance) instance)
74              .map(ObjectUtils::notNull)
75              .collect(Collectors.toMap(IBoundFieldInstance::getEffectiveName, Function.identity(),
76                  CustomCollectors.useLastMapper(),
77                  LinkedHashMap::new))));
78  
79      this.assemblyInstances = CollectionUtil.unmodifiableMap(ObjectUtils.notNull(
80          getNamedModelInstanceMap().values().stream()
81              .filter(instance -> instance instanceof IBoundAssemblyInstance)
82              .map(instance -> (IBoundAssemblyInstance) instance)
83              .map(ObjectUtils::notNull)
84              .collect(Collectors.toMap(IBoundAssemblyInstance::getEffectiveName, Function.identity(),
85                  CustomCollectors.useLastMapper(),
86                  LinkedHashMap::new))));
87    }
88  
89    protected Stream<IBoundNamedModelInstance> getModelInstanceFieldStream(
90        @NonNull IAssemblyClassBinding classBinding,
91        @NonNull Class<?> clazz) {
92  
93      Stream<IBoundNamedModelInstance> superInstances;
94      Class<?> superClass = clazz.getSuperclass();
95      if (superClass == null) {
96        superInstances = Stream.empty();
97      } else {
98        // get instances from superclass
99        superInstances = getModelInstanceFieldStream(classBinding, superClass);
100     }
101 
102     IBindingContext bindingContext = classBinding.getBindingContext();
103     return Stream.concat(superInstances, Arrays.stream(clazz.getDeclaredFields())
104         // skip this field, since it is ignored
105         .filter(field -> !field.isAnnotationPresent(Ignore.class))
106         // skip fields that aren't a Module field or assembly instance
107         .filter(field -> field.isAnnotationPresent(BoundField.class) || field.isAnnotationPresent(BoundAssembly.class))
108         .map(field -> {
109           assert field != null;
110 
111           IBoundNamedModelInstance retval;
112           if (field.isAnnotationPresent(BoundAssembly.class)
113               && bindingContext.getClassBinding(IBoundNamedModelInstance.getItemType(field)) != null) {
114             retval = IBoundAssemblyInstance.newInstance(field, classBinding);
115           } else if (field.isAnnotationPresent(BoundField.class)) {
116             retval = IBoundFieldInstance.newInstance(field, classBinding);
117           } else {
118             throw new IllegalStateException(
119                 String.format("The field '%s' on class '%s' is not bound", field.getName(), clazz.getName()));
120           }
121           // TODO: handle choice
122           return retval;
123         })
124         .filter(Objects::nonNull)
125         .map(ObjectUtils::notNull));
126   }
127 
128   @Override
129   public Collection<IBoundNamedModelInstance> getModelInstances() {
130     return ObjectUtils.notNull(getNamedModelInstanceMap().values());
131   }
132 
133   @Override
134   public Map<String, IBoundNamedModelInstance> getNamedModelInstanceMap() {
135     return modelInstances;
136   }
137 
138   @Override
139   public Map<String, IBoundFieldInstance> getFieldInstanceMap() {
140     return fieldInstances;
141   }
142 
143   @Override
144   public Map<String, IBoundAssemblyInstance> getAssemblyInstanceMap() {
145     return assemblyInstances;
146   }
147 
148   @Override
149   public List<IChoiceInstance> getChoiceInstances() {
150     // choices are not exposed by this API
151     return CollectionUtil.emptyList();
152   }
153 }