1   /*
2    * SPDX-FileCopyrightText: none
3    * SPDX-License-Identifier: CC0-1.0
4    */
5   
6   package gov.nist.secauto.metaschema.databind.io;
7   
8   import gov.nist.secauto.metaschema.core.configuration.DefaultConfiguration;
9   import gov.nist.secauto.metaschema.core.configuration.IConfigurationFeature;
10  import gov.nist.secauto.metaschema.core.configuration.IMutableConfiguration;
11  import gov.nist.secauto.metaschema.databind.IBindingContext;
12  import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelAssembly;
13  
14  import java.util.Map;
15  
16  import edu.umd.cs.findbugs.annotations.NonNull;
17  
18  @SuppressWarnings("PMD.ReplaceVectorWithList") // false positive
19  abstract class AbstractSerializationBase<T extends IConfigurationFeature<?>>
20      implements IMutableConfiguration<T> {
21    @NonNull
22    private final IBoundDefinitionModelAssembly definition;
23    @NonNull
24    private final DefaultConfiguration<T> configuration;
25  
26    protected AbstractSerializationBase(@NonNull IBoundDefinitionModelAssembly definition) {
27      this.definition = definition;
28      this.configuration = new DefaultConfiguration<>();
29    }
30  
31    /**
32     * Retrieve the binding context associated with the serializer.
33     *
34     * @return the binding context
35     */
36    @NonNull
37    protected IBindingContext getBindingContext() {
38      return getDefinition().getBindingContext();
39    }
40  
41    /**
42     * Retrieve the bound class information associated with the assembly that the
43     * serializer/deserializer will write/read data from.
44     *
45     * @return the class binding for the Module assembly
46     */
47    @NonNull
48    protected IBoundDefinitionModelAssembly getDefinition() {
49      return definition;
50    }
51  
52    @SuppressWarnings("unused")
53    protected void configurationChanged(@NonNull IMutableConfiguration<T> config) {
54      // do nothing by default. Methods can override this to deal with factory caching
55    }
56  
57    /**
58     * Get the current configuration of the serializer/deserializer.
59     *
60     * @return the configuration
61     */
62    @NonNull
63    protected IMutableConfiguration<T> getConfiguration() {
64      return configuration;
65    }
66  
67    @Override
68    public boolean isFeatureEnabled(T feature) {
69      return configuration.isFeatureEnabled(feature);
70    }
71  
72    @Override
73    public Map<T, Object> getFeatureValues() {
74      return configuration.getFeatureValues();
75    }
76  
77    @Override
78    public <V> V get(T feature) {
79      return configuration.get(feature);
80    }
81  
82  }