Skip to content

Commit

Permalink
Code reformatting. Added a type method to IConstraint implementations…
Browse files Browse the repository at this point in the history
… to provide a human-readable name for the constraint to improve error messages.
  • Loading branch information
david-waltermire committed Dec 10, 2024
1 parent a3dba87 commit 8f3cd38
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import gov.nist.secauto.metaschema.core.metapath.type.IItemType;
import gov.nist.secauto.metaschema.core.metapath.type.IKindTest;

import java.net.URI;

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

/**
Expand Down Expand Up @@ -44,6 +46,10 @@ default IDocumentNodeItem getNodeItem() {
return this;
}

@Override
@NonNull
URI getDocumentUri();

/**
* Get the node item for the document root element.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ default URI getBaseUri() {

@Override
default @NonNull
String format(@NonNull IPathFormatter formatter) {
String format(@NonNull IPathFormatter formatter) {
return formatter.formatField(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ default IFlagNodeItem getFlagByName(@NonNull IEnhancedQName name) {
@SuppressWarnings("null")
@Override
default @NonNull
Stream<? extends IFlagNodeItem> flags() {
Stream<? extends IFlagNodeItem> flags() {
// a flag does not have flags
return Stream.empty();
}
Expand All @@ -112,7 +112,7 @@ Stream<? extends IFlagNodeItem> flags() {
@SuppressWarnings("null")
@Override
default @NonNull
Collection<? extends List<? extends IModelNodeItem<?, ?>>> getModelItems() {
Collection<? extends List<? extends IModelNodeItem<?, ?>>> getModelItems() {
// a flag does not have model items
return Collections.emptyList();
}
Expand Down Expand Up @@ -141,7 +141,7 @@ Stream<? extends IFlagNodeItem> flags() {

@Override
default @NonNull
String format(@NonNull IPathFormatter formatter) {
String format(@NonNull IPathFormatter formatter) {
return formatter.formatFlag(this);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ private static String toErrorMessage(
@NonNull Throwable ex) {
StringBuilder builder = new StringBuilder(128);
builder.append("A ")
.append(constraint.getClass().getName())
.append(constraint.getType().getName())
.append(" constraint");

String id = constraint.getId();
Expand All @@ -402,7 +402,7 @@ private static String toErrorMessage(

builder.append(", matching the item at path '")
.append(item.getMetapath())
.append("', resulted in an unexpected error. The error was: ")
.append("', resulted in an unexpected error. ")
.append(ex.getLocalizedMessage());
return ObjectUtils.notNull(builder.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ enum Extensible {
NONE;
}

@Override
default Type getType() {
return Type.ALLOWED_VALUES;
}

/**
* Get the collection allowed values associated with this constraint.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
* elements with a minimum and/or maximum occurrence.
*/
public interface ICardinalityConstraint extends IConfigurableMessageConstraint {
@Override
default Type getType() {
return Type.CARDINALITY;
}

/**
* Retrieve the required minimum occurrence of the target instance. If
* specified, this value must be less than or equal to the value of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,31 @@
* flag. Provides a common interface for all constraint definitions.
*/
public interface IConstraint extends IAttributable, IDescribable {
/**
* The type of constraint.
*/
enum Type {
ALLOWED_VALUES("allowed-values"),
CARDINALITY("cardinality"),
EXPECT("expect"),
INDEX("index"),
UNIQUE("unique"),
INDEX_HAS_KEY("index-has-key"),
MATCHES("matches");

@NonNull
private final String name;

Type(@NonNull String name) {
this.name = name;
}

@NonNull
public String getName() {
return name;
}
}

/**
* The degree to which a constraint violation is significant.
* <p>
Expand Down Expand Up @@ -91,6 +116,14 @@ static String getConstraintIdentity(@NonNull IConstraint constraint) {
return ObjectUtils.notNull(identity);
}

/**
* Get the constraint type.
*
* @return the constraint type
*/
@NonNull
Type getType();

/**
* Retrieve the unique identifier for the constraint.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
* A custom message can be used to indicate what a test failure signifies.
*/
public interface IExpectConstraint extends IConfigurableMessageConstraint {
@Override
default Type getType() {
return Type.EXPECT;
}

/**
* Get the test to use to validate selected nodes.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
* data objects using the {@link IIndexHasKeyConstraint}.
*/
public interface IIndexConstraint extends IKeyConstraint {
@Override
default Type getType() {
return Type.INDEX;
}

/**
* Get the name of the index, which is used to refer to the index by an
* {@link IIndexHasKeyConstraint}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
* {@link IIndexConstraint}.
*/
public interface IIndexHasKeyConstraint extends IKeyConstraint {
@Override
default Type getType() {
return Type.INDEX_HAS_KEY;
}

/**
* The name of the index used to verify cross references.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@
* and/or conform to an identified data type.
*/
public interface IMatchesConstraint extends IConfigurableMessageConstraint {
@Override
default Type getType() {
return Type.MATCHES;
}

/**
* Get the expected pattern.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
* generated, but this constraint type does not persist a named index.
*/
public interface IUniqueConstraint extends IKeyConstraint {
@Override
default Type getType() {
return Type.UNIQUE;
}

@Override
default <T, R> R accept(IConstraintVisitor<T, R> visitor, T state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ public static URI toUri(@NonNull String location, @NonNull URI baseUri) throws U
* @throws URISyntaxException
* if any of the URIs are malformed
*/
@NonNull
public static URI relativize(URI base, URI other, boolean prepend) throws URISyntaxException {
URI normBase = Objects.requireNonNull(base).normalize();
URI normOther = Objects.requireNonNull(other).normalize();
Expand Down

0 comments on commit 8f3cd38

Please sign in to comment.