From 0e4085b8bb80b488841a978a7ca574e48dee7746 Mon Sep 17 00:00:00 2001 From: David Waltermire Date: Sun, 23 Jun 2024 21:49:13 -0400 Subject: [PATCH] Cleanup source implementations, and properly cache URI-based sources. --- .../constraint/impl/ExternalModelSource.java | 82 ------------------- .../model/constraint/impl/ExternalSource.java | 10 ++- .../constraint/impl/InternalModelSource.java | 12 +-- .../impl/UnknownInternalModelSource.java | 5 ++ 4 files changed, 18 insertions(+), 91 deletions(-) delete mode 100644 core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/ExternalModelSource.java diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/ExternalModelSource.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/ExternalModelSource.java deleted file mode 100644 index c95e3c05b..000000000 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/ExternalModelSource.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Portions of this software was developed by employees of the National Institute - * of Standards and Technology (NIST), an agency of the Federal Government and is - * being made available as a public service. Pursuant to title 17 United States - * Code Section 105, works of NIST employees are not subject to copyright - * protection in the United States. This software may be subject to foreign - * copyright. Permission in the United States and in foreign countries, to the - * extent that NIST may hold copyright, to use, copy, modify, create derivative - * works, and distribute this software and its documentation without fee is hereby - * granted on a non-exclusive basis, provided that this notice and disclaimer - * of warranty appears in all copies. - * - * THE SOFTWARE IS PROVIDED 'AS IS' WITHOUT ANY WARRANTY OF ANY KIND, EITHER - * EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, ANY WARRANTY - * THAT THE SOFTWARE WILL CONFORM TO SPECIFICATIONS, ANY IMPLIED WARRANTIES OF - * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, AND FREEDOM FROM - * INFRINGEMENT, AND ANY WARRANTY THAT THE DOCUMENTATION WILL CONFORM TO THE - * SOFTWARE, OR ANY WARRANTY THAT THE SOFTWARE WILL BE ERROR FREE. IN NO EVENT - * SHALL NIST BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, DIRECT, - * INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES, ARISING OUT OF, RESULTING FROM, - * OR IN ANY WAY CONNECTED WITH THIS SOFTWARE, WHETHER OR NOT BASED UPON WARRANTY, - * CONTRACT, TORT, OR OTHERWISE, WHETHER OR NOT INJURY WAS SUSTAINED BY PERSONS OR - * PROPERTY OR OTHERWISE, AND WHETHER OR NOT LOSS WAS SUSTAINED FROM, OR AROSE OUT - * OF THE RESULTS OF, OR USE OF, THE SOFTWARE OR SERVICES PROVIDED HEREUNDER. - */ - -package gov.nist.secauto.metaschema.core.model.constraint.impl; - -import gov.nist.secauto.metaschema.core.model.constraint.ISource; - -import java.net.URI; -import java.util.HashMap; -import java.util.Map; - -import edu.umd.cs.findbugs.annotations.NonNull; - -/** - * Implements a - * {@link gov.nist.secauto.metaschema.core.model.constraint.ISource.SourceType#MODEL} - * source with an associated resource. - */ -public final class ExternalModelSource implements ISource { - @NonNull - private static final Map sources = new HashMap<>(); // NOPMD - intentional - @NonNull - private final URI modelUri; - - /** - * Get a new instance of a model source associated with a resource - * {@code location}. - * - * @param location - * the resource location containing a constraint - * @return the source - */ - @NonNull - public static ISource instance(@NonNull URI location) { - ISource retval; - synchronized (sources) { - retval = sources.get(location); - if (retval == null) { - retval = new ExternalModelSource(location); - } - } - return retval; - } - - ExternalModelSource(@NonNull URI modelSource) { - this.modelUri = modelSource; - } - - @Override - public SourceType getSourceType() { - return SourceType.MODEL; - } - - @NonNull - @Override - public URI getSource() { - return modelUri; - } -} diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/ExternalSource.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/ExternalSource.java index 80bd54ef2..6c274c8ca 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/ExternalSource.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/ExternalSource.java @@ -58,10 +58,7 @@ public final class ExternalSource implements ISource { public static ISource instance(@NonNull URI location) { ISource retval; synchronized (sources) { - retval = sources.get(location); - if (retval == null) { - retval = new ExternalModelSource(location); - } + retval = sources.computeIfAbsent(location, (uri) -> new ExternalSource(uri)); } return retval; } @@ -80,4 +77,9 @@ public SourceType getSourceType() { public URI getSource() { return modelUri; } + + @Override + public String toString() { + return "external:" + modelUri; + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/InternalModelSource.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/InternalModelSource.java index 219f2c1e7..4a8ffa91a 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/InternalModelSource.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/InternalModelSource.java @@ -41,7 +41,7 @@ */ public final class InternalModelSource implements ISource { @NonNull - private static final Map sources = new HashMap<>(); // NOPMD - intentional + private static final Map sources = new HashMap<>(); // NOPMD - intentional @NonNull private final URI modelUri; @@ -57,10 +57,7 @@ public final class InternalModelSource implements ISource { public static ISource instance(@NonNull URI location) { ISource retval; synchronized (sources) { - retval = sources.get(location); - if (retval == null) { - retval = new InternalModelSource(location); - } + retval = sources.computeIfAbsent(location, (uri) -> new InternalModelSource(uri)); } return retval; } @@ -79,4 +76,9 @@ public SourceType getSourceType() { public URI getSource() { return modelUri; } + + @Override + public String toString() { + return "internal:" + modelUri; + } } diff --git a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/UnknownInternalModelSource.java b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/UnknownInternalModelSource.java index 06c4202ae..b7c503bde 100644 --- a/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/UnknownInternalModelSource.java +++ b/core/src/main/java/gov/nist/secauto/metaschema/core/model/constraint/impl/UnknownInternalModelSource.java @@ -68,4 +68,9 @@ public URI getSource() { // always null return null; } + + @Override + public String toString() { + return "internal"; + } }