Skip to content

Commit

Permalink
Switched flag retrieval to using Integer indexes instead of qnames.
Browse files Browse the repository at this point in the history
  • Loading branch information
david-waltermire committed Nov 22, 2024
1 parent d15884e commit 585ff2f
Show file tree
Hide file tree
Showing 26 changed files with 72 additions and 75 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
package gov.nist.secauto.metaschema.core.model;

import gov.nist.secauto.metaschema.core.model.impl.DefaultContainerFlagSupport;
import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
import gov.nist.secauto.metaschema.core.qname.EQNameFactory;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.CustomCollectors;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
Expand All @@ -26,20 +26,20 @@ public class FlagContainerBuilder<T extends IFlagInstance> implements IFlagConta
private static final Logger LOGGER = LogManager.getLogger(FlagContainerBuilder.class);

@Nullable
private final IEnhancedQName jsonKeyName;
private final Integer jsonKeyIndex;
@NonNull
private final List<T> flags;

/**
* Construct a new flag container using the provided flag qualified name as the
* JSON key.
*
* @param jsonKeyName
* the qualified name of the JSON key or {@code null} if no JSON key is
* configured
* @param jsonKeyIndex
* the qualified name index of the JSON key or {@code null} if no JSON
* key is configured
*/
public FlagContainerBuilder(@Nullable IEnhancedQName jsonKeyName) {
this.jsonKeyName = jsonKeyName;
public FlagContainerBuilder(@Nullable Integer jsonKeyIndex) {
this.jsonKeyIndex = jsonKeyIndex;
this.flags = new LinkedList<>();
}

Expand All @@ -56,29 +56,29 @@ public IContainerFlagSupport<T> build() {
if (flags.isEmpty()) {
retval = IContainerFlagSupport.empty();
} else {
Map<IEnhancedQName, T> flagMap = CollectionUtil.unmodifiableMap(ObjectUtils.notNull(flags.stream()
Map<Integer, T> flagMap = CollectionUtil.unmodifiableMap(ObjectUtils.notNull(flags.stream()
.collect(
CustomCollectors.toMap(
INamed::getQName,
flag -> flag.getQName().getIndexPosition(),
CustomCollectors.identity(),
FlagContainerBuilder::handleShadowedInstances,
LinkedHashMap::new))));

T jsonKey = jsonKeyName == null ? null : flagMap.get(jsonKeyName);
T jsonKey = jsonKeyIndex == null ? null : flagMap.get(jsonKeyIndex);
retval = new DefaultContainerFlagSupport<>(flagMap, jsonKey);
}
return retval;
}

private static <INSTANCE extends IFlagInstance> INSTANCE handleShadowedInstances(
@NonNull IEnhancedQName key,
@NonNull Integer keyIndex,
@NonNull INSTANCE shadowed,
@NonNull INSTANCE shadowing) {
if (!shadowed.equals(shadowing) && LOGGER.isErrorEnabled()) {
IModelDefinition owningDefinition = shadowing.getContainingDefinition();
IModule module = owningDefinition.getContainingModule();
LOGGER.error("Unexpected duplicate flag instance named '%s' in definition '%s' in module name '%s' at '%s'",
key,
LOGGER.error("Unexpected duplicate flag instance name '%s' in definition '%s' in module name '%s' at '%s'",
EQNameFactory.instance().get(keyIndex),
owningDefinition.getDefinitionQName(),
module.getShortName(),
module.getLocation());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@
package gov.nist.secauto.metaschema.core.model;

import gov.nist.secauto.metaschema.core.model.impl.EmptyFlagContainer;
import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;

import java.util.Map;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;

/**
* Provides support for managing the flag contents of a
* {@link IModelDefinition}.
* <p>
* This interface provides the underlying data used by
* {@link IFeatureContainerFlag}.
*
* @param <FI>
* the Java type of the managed flag instance data
*/
public interface IContainerFlagSupport<FI extends IFlagInstance> {
/**
* Provides an empty instance.
Expand Down Expand Up @@ -50,7 +59,7 @@ static <T extends IFlagInstance> IFlagContainerBuilder<T> builder() {
* @return the flag container
*/
@NonNull
static <T extends IFlagInstance> IFlagContainerBuilder<T> builder(@NonNull IEnhancedQName jsonKey) {
static <T extends IFlagInstance> IFlagContainerBuilder<T> builder(@NonNull Integer jsonKey) {
return new FlagContainerBuilder<>(jsonKey);
}

Expand All @@ -60,7 +69,7 @@ static <T extends IFlagInstance> IFlagContainerBuilder<T> builder(@NonNull IEnha
* @return the mapping of flag effective name to flag instance
*/
@NonNull
Map<IEnhancedQName, FI> getFlagInstanceMap();
Map<Integer, FI> getFlagInstanceMap();

/**
* Get the JSON key flag instance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

package gov.nist.secauto.metaschema.core.model;

import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;

import java.util.Collection;
Expand All @@ -30,7 +29,7 @@ public interface IFeatureContainerFlag<FI extends IFlagInstance> extends IModelD

@Override
@Nullable
default FI getFlagInstanceByName(IEnhancedQName name) {
default FI getFlagInstanceByName(Integer name) {
return getFlagContainer().getFlagInstanceMap().get(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@

package gov.nist.secauto.metaschema.core.model;

import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;

import java.util.Collection;
import java.util.function.Predicate;

import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;

/**
* Represents a Metaschema definition for a complex object that may contain
* flags.
*/
public interface IModelDefinition extends IDefinition, IContainer {
/**
* Tests if the provided definition represents complex data. The data is complex
Expand Down Expand Up @@ -52,7 +54,7 @@ default boolean hasChildren() {
* matching the specified name
*/
@Nullable
IFlagInstance getFlagInstanceByName(@NonNull IEnhancedQName index);
IFlagInstance getFlagInstanceByName(@NonNull Integer index);

/**
* Retrieves the flag instances for all flags defined on the containing
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ default IFlagInstance getJsonKey() {
return name == null
? null
: ObjectUtils.requireNonNull(
getDefinition().getFlagInstanceByName(ModuleUtils.parseFlagName(getContainingModule(), name)));
getDefinition().getFlagInstanceByName(
ModuleUtils.parseFlagName(getContainingModule(), name).getIndexPosition()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport;
import gov.nist.secauto.metaschema.core.model.IFlagInstance;
import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;

import java.util.Map;

Expand All @@ -16,7 +15,7 @@

public class DefaultContainerFlagSupport<FI extends IFlagInstance> implements IContainerFlagSupport<FI> {
@NonNull
private final Map<IEnhancedQName, FI> instances;
private final Map<Integer, FI> instances;
@Nullable
private final FI jsonKey;

Expand All @@ -30,14 +29,14 @@ public class DefaultContainerFlagSupport<FI extends IFlagInstance> implements IC
* configured
*/
public DefaultContainerFlagSupport(
@NonNull Map<IEnhancedQName, FI> instances,
@NonNull Map<Integer, FI> instances,
@Nullable FI jsonKey) {
this.instances = instances;
this.jsonKey = jsonKey;
}

@Override
public Map<IEnhancedQName, FI> getFlagInstanceMap() {
public Map<Integer, FI> getFlagInstanceMap() {
return instances;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport;
import gov.nist.secauto.metaschema.core.model.IFlagInstance;
import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;

import java.util.Map;
Expand All @@ -19,7 +18,7 @@ public class EmptyFlagContainer<FI extends IFlagInstance> implements IContainerF
public static final EmptyFlagContainer<?> EMPTY = new EmptyFlagContainer<>();

@Override
public Map<IEnhancedQName, FI> getFlagInstanceMap() {
public Map<Integer, FI> getFlagInstanceMap() {
return CollectionUtil.emptyMap();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,9 @@ private static IContainerFlagSupport<IFlagInstance> buildFlagContainer(
@NonNull IModelDefinition parent) {
IFlagContainerBuilder<IFlagInstance> builder = jsonKeyFlagRef == null
? IContainerFlagSupport.builder()
: IContainerFlagSupport.builder(ModuleUtils.parseFlagName(parent.getContainingModule(), jsonKeyFlagRef));
: IContainerFlagSupport.builder(ModuleUtils.parseFlagName(
parent.getContainingModule(),
jsonKeyFlagRef).getIndexPosition());
// handle flags
XML_MODEL_PARSER.parse(
parent.getContainingModule().getSource(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,7 @@ public IFlagInstance getJsonValueKeyFlagInstance() {
String namespace = getQName().getNamespace();
String name = ObjectUtils.notNull(getXmlObject().getJsonValueKeyFlag().getFlagRef());

retval = getFlagInstanceByName(
namespace.isEmpty()
? IEnhancedQName.of(name)
: IEnhancedQName.of(namespace, name));
retval = getFlagInstanceByName(IEnhancedQName.of(namespace, name).getIndexPosition());
}
return retval;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,7 @@ public IFlagInstance getJsonValueKeyFlagInstance() {
String namespace = getQName().getNamespace();
String name = ObjectUtils.notNull(getXmlObject().getJsonValueKeyFlag().getFlagRef());

retval = getFlagInstanceByName(
namespace.isEmpty()
? IEnhancedQName.of(name)
: IEnhancedQName.of(namespace, name));
retval = getFlagInstanceByName(IEnhancedQName.of(namespace, name).getIndexPosition());
}
return retval;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,7 @@ public IFlagInstance getJsonValueKeyFlagInstance() {
String namespace = getQName().getNamespace();
String name = ObjectUtils.notNull(getXmlObject().getJsonValueKeyFlag().getFlagRef());

retval = getFlagInstanceByName(
namespace.isEmpty()
? IEnhancedQName.of(name)
: IEnhancedQName.of(namespace, name));
retval = getFlagInstanceByName(IEnhancedQName.of(namespace, name).getIndexPosition());
}
return retval;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ static IEnhancedQName of(@NonNull String localName) {
@SuppressWarnings("PMD.ShortMethodName")
@NonNull
static IEnhancedQName of(@NonNull String namespace, @NonNull String localName) {
return EQNameFactory.instance().newQName(namespace, localName);
return namespace.isEmpty()
? EQNameFactory.instance().newQName(localName)
: EQNameFactory.instance().newQName(namespace, localName);
}

@SuppressWarnings("PMD.ShortMethodName")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void testGenerateFlags() {
// setup the value calls
getContext().checking(new Expectations() {
{ // NOPMD - intentional
allowing(fieldInstance.getDefinition().getFlagInstanceByName(IEnhancedQName.of(NS, "flag1")))
allowing(fieldInstance.getDefinition().getFlagInstanceByName(IEnhancedQName.of(NS, "flag1").getIndexPosition()))
.getValue(fieldValue);
will(returnValue("flag1 value"));
}
Expand Down Expand Up @@ -88,7 +88,8 @@ void testGenerateModelItems() {
// Setup the value calls
getContext().checking(new Expectations() {
{ // NOPMD - intentional
allowing(assembly.getFlagInstanceByName(IEnhancedQName.of(NS, "flag1"))).getValue(assemblyValue);
allowing(assembly.getFlagInstanceByName(IEnhancedQName.of(NS, "flag1").getIndexPosition()))
.getValue(assemblyValue);
will(returnValue(flagValue));
allowing(assembly.getNamedModelInstanceByName(IEnhancedQName.of(NS, "field1").getIndexPosition()))
.getValue(assemblyValue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
public abstract class AbstractModelBuilder<T extends AbstractModelBuilder<T>>
extends MockFactory {

private String namespace;
private String namespace = "";
private String name;

/**
Expand Down Expand Up @@ -136,9 +136,7 @@ protected <DEF extends IDefinition> void applyNamedInstance(
getContext().checking(new Expectations() {
{
allowing(instance).getQName();
will(returnValue(namespace == null
? IEnhancedQName.of(ObjectUtils.notNull(name))
: IEnhancedQName.of(ObjectUtils.notNull(namespace), ObjectUtils.notNull(name))));
will(returnValue(IEnhancedQName.of(ObjectUtils.notNull(namespace), ObjectUtils.notNull(name))));
allowing(instance).getDefinition();
will(returnValue(definition));
allowing(instance).getContainingDefinition();
Expand All @@ -164,9 +162,7 @@ protected void applyNamed(@NonNull INamedModelElement element) {
allowing(element).getUseName();
will(returnValue(null));
allowing(element).getQName();
will(returnValue(namespace == null
? IEnhancedQName.of(ObjectUtils.notNull(name))
: IEnhancedQName.of(ObjectUtils.notNull(namespace), ObjectUtils.notNull(name))));
will(returnValue(IEnhancedQName.of(ObjectUtils.notNull(namespace), ObjectUtils.notNull(name))));
allowing(element).getEffectiveName();
will(returnValue(name));
allowing(element).getFormalName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public IAssemblyDefinition toDefinition() {
allowing(retval).getFlagInstances();
will(returnValue(flags.values()));
flags.forEach((key, value) -> {
allowing(retval).getFlagInstanceByName(with(key));
allowing(retval).getFlagInstanceByName(with(key.getIndexPosition()));
will(returnValue(value));
});
allowing(retval).getModelInstances();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ public IFieldInstanceAbsolute toInstance(
*
* @return the new mocked definition
*/
@SuppressWarnings("null")
@NonNull
public IFieldDefinition toDefinition() {
validate();
Expand All @@ -158,7 +157,7 @@ public IFieldDefinition toDefinition() {
allowing(retval).getFlagInstances();
will(returnValue(flags.values()));
flags.forEach((key, value) -> {
allowing(retval).getFlagInstanceByName(with(key));
allowing(retval).getFlagInstanceByName(with(key.getIndexPosition()));
will(returnValue(value));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ default IBoundInstanceFlag getJsonKey() {
return name == null
? null
: ObjectUtils.requireNonNull(
getDefinition().getFlagInstanceByName(ModuleUtils.parseFlagName(getContainingModule(), name)));
getDefinition().getFlagInstanceByName(
ModuleUtils.parseFlagName(getContainingModule(), name).getIndexPosition()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package gov.nist.secauto.metaschema.databind.model.impl;

import gov.nist.secauto.metaschema.core.model.IContainerFlagSupport;
import gov.nist.secauto.metaschema.core.qname.IEnhancedQName;
import gov.nist.secauto.metaschema.core.util.CollectionUtil;
import gov.nist.secauto.metaschema.core.util.ObjectUtils;
import gov.nist.secauto.metaschema.databind.model.IBoundDefinitionModelComplex;
Expand All @@ -32,7 +31,7 @@

public class FlagContainerSupport implements IContainerFlagSupport<IBoundInstanceFlag> {
@NonNull
private final Map<IEnhancedQName, IBoundInstanceFlag> flagInstances;
private final Map<Integer, IBoundInstanceFlag> flagInstances;
@Nullable
private IBoundInstanceFlag jsonKeyFlag;

Expand Down Expand Up @@ -63,7 +62,7 @@ public FlagContainerSupport(
this.flagInstances = CollectionUtil.unmodifiableMap(ObjectUtils.notNull(instances
.peek(intermediate)
.collect(Collectors.toMap(
IBoundInstanceFlag::getQName,
flag -> flag.getQName().getIndexPosition(),
Function.identity(),
(v1, v2) -> v2,
LinkedHashMap::new))));
Expand Down Expand Up @@ -114,7 +113,7 @@ protected void handleFlagInstance(IBoundInstanceFlag instance) {

@Override
@NonNull
public Map<IEnhancedQName, IBoundInstanceFlag> getFlagInstanceMap() {
public Map<Integer, IBoundInstanceFlag> getFlagInstanceMap() {
return flagInstances;
}

Expand Down
Loading

0 comments on commit 585ff2f

Please sign in to comment.