Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix checkstyle and javadocs in com.fauna.response #171

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
289 changes: 203 additions & 86 deletions src/main/java/com/fauna/response/ConstraintFailure.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,71 +9,45 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

public class ConstraintFailure {
public final class ConstraintFailure {
private final String message;

private final String name;

private final PathElement[][] paths;

public ConstraintFailure(String message, String name,
PathElement[][] paths) {
/**
* Initialize a new ConstraintFailure instance. Queries that fail a <a
* href="https://docs.fauna.com/fauna/current/reference/fsl/check/">check</a>
* or <a
* href="https://docs.fauna.com/fauna/current/reference/fsl/unique/">unique
* constraint> return a constraint failure.
*
* @param message Human-readable description of the constraint failure.
* @param name Name of the failed constraint.
* @param paths A list of paths where the constraint failure occurred.
*/
public ConstraintFailure(
final String message,
final String name,
final PathElement[][] paths) {
this.message = message;
this.name = name;
this.paths = paths;
}

public static class PathElement {
private String sVal = null;
private Integer iVal = null;

public PathElement(String sVal) {
this.sVal = sVal;
}

public PathElement(Integer iVal) {
this.iVal = iVal;
}

public boolean isString() {
return sVal != null;
}

/**
* Note that this parse method does not advance the parser.
*
* @param parser A JsonParser instance.
* @return A new PathElement.
* @throws IOException Can be thrown if e.g. stream ends.
*/
public static PathElement parse(JsonParser parser) throws IOException {
if (parser.currentToken().isNumeric()) {
return new PathElement(parser.getValueAsInt());
} else {
return new PathElement(parser.getText());
}
}

@Override
public boolean equals(Object o) {
if (o instanceof PathElement) {
PathElement other = (PathElement) o;
return other.isString() == this.isString() &&
other.toString().equals(this.toString());
} else {
return false;
}
}

public String toString() {
return sVal == null ? String.valueOf(iVal) : sVal;
}
}

public static PathElement[] createPath(Object... elements) {
/**
* Constructs a PathElement[] from the provided objects. Supported types
* are String and Integer.
*
* @param elements The String objects or Integer objects to use.
* @return A array of PathElement instances.
*/
public static PathElement[] createPath(final Object... elements) {
List<PathElement> path = new ArrayList<>();
for (Object element : elements) {
if (element instanceof String) {
Expand All @@ -88,41 +62,24 @@ public static PathElement[] createPath(Object... elements) {
return path.toArray(new PathElement[0]);
}


public static class Builder {
private String message = null;
private String name = null;
private final List<PathElement[]> paths = new ArrayList<>();

public Builder message(String message) {
this.message = message;
return this;
}

public Builder name(String name) {
this.name = name;
return this;
}

public Builder path(PathElement[] path) {
this.paths.add(path);
return this;
}

public ConstraintFailure build() {
PathElement[][] paths =
this.paths.toArray(new PathElement[this.paths.size()][]);
return new ConstraintFailure(this.message, this.name,
this.paths.isEmpty() ? null : paths);
}

}

/**
* Initializes a new empty Builder.
*
* @return A new Builder
*/
public static Builder builder() {
return new Builder();
}

public static ConstraintFailure parse(JsonParser parser)
/**
* Builds a ConstraintFailure instance from the provided JsonParser.
*
* @param parser The JsonParser to consume.
* @return A new ConstraintFailure instance.
* @throws IOException Thrown if an error is encountered while reading the
* parser.
*/
public static ConstraintFailure parse(final JsonParser parser)
throws IOException {
if (parser.currentToken() != JsonToken.START_OBJECT &&
parser.nextToken() != JsonToken.START_OBJECT) {
Expand Down Expand Up @@ -152,8 +109,8 @@ public static ConstraintFailure parse(JsonParser parser)
}
} else if (firstPathToken != JsonToken.VALUE_NULL) {
throw new ClientResponseException(
"Constraint failure path should be array or null, got: " +
firstPathToken.toString());
"Constraint failure path should be array or null, got: "
+ firstPathToken.toString());
}
paths.forEach(builder::path);
break;
Expand All @@ -163,18 +120,38 @@ public static ConstraintFailure parse(JsonParser parser)

}

/**
* Gets the constraint failure message.
*
* @return A string representation of the message.
*/
public String getMessage() {
return this.message;
}

/**
* Gets the constraint failure name.
*
* @return A string representation of the name.
*/
public Optional<String> getName() {
return Optional.ofNullable(this.name);
}

/**
* Gets an optional path elements related to the constraint failure.
*
* @return An array of arrays of PathElements.
*/
public Optional<PathElement[][]> getPaths() {
return Optional.ofNullable(paths);
}

/**
* Gets a list of string representations of the constraint failure paths.
*
* @return A list of string representations of constraint failure paths.
*/
public Optional<List<String>> getPathStrings() {
if (paths == null) {
return Optional.empty();
Expand All @@ -187,15 +164,22 @@ public Optional<List<String>> getPathStrings() {
}
}

public boolean pathsAreEqual(ConstraintFailure otherFailure) {
/**
* Tests path equality with another ConstraintFailure.
*
* @param otherFailure The other ConstraintFailure.
* @return True if the paths are equal.
*/
public boolean pathsAreEqual(final ConstraintFailure otherFailure) {
PathElement[][] thisArray =
this.getPaths().orElse(new PathElement[0][]);
PathElement[][] otherArray =
otherFailure.getPaths().orElse(new PathElement[0][]);
return Arrays.deepEquals(thisArray, otherArray);
}

public boolean equals(Object other) {
@Override
public boolean equals(final Object other) {
if (other instanceof ConstraintFailure) {
ConstraintFailure otherFailure = (ConstraintFailure) other;
return this.getMessage().equals(otherFailure.getMessage())
Expand All @@ -206,4 +190,137 @@ public boolean equals(Object other) {
}
}

@Override
public int hashCode() {
return Objects.hash(
this.name,
this.message,
Arrays.deepHashCode(this.paths));
}

public static final class PathElement {
private String sVal = null;
private Integer iVal = null;

/**
* Initializes a PathElement with a string value.
*
* @param sVal The string value.
*/
public PathElement(final String sVal) {
this.sVal = sVal;
}

/**
* Initializes a PathElement with an integer value.
*
* @param iVal The integer value.
*/
public PathElement(final Integer iVal) {
this.iVal = iVal;
}

/**
* Note that this parse method does not advance the parser.
*
* @param parser A JsonParser instance.
* @return A new PathElement.
* @throws IOException Can be thrown if e.g. stream ends.
*/
public static PathElement parse(final JsonParser parser)
throws IOException {
if (parser.currentToken().isNumeric()) {
return new PathElement(parser.getValueAsInt());
} else {
return new PathElement(parser.getText());
}
}

/**
* Tests whether the PathElement stores a string or an integer.
*
* @return If it's a string, true. Otherwise, false.
*/
public boolean isString() {
return sVal != null;
}

@Override
public boolean equals(final Object o) {
if (o instanceof PathElement) {
PathElement other = (PathElement) o;
return other.isString() == this.isString() &&
other.toString().equals(this.toString());
} else {
return false;
}
}

@Override
public int hashCode() {
return toString().hashCode();
}

/**
* Converts the PathElement to a string.
*
* @return A string representation of the PathElement.
*/
public String toString() {
return sVal == null ? String.valueOf(iVal) : sVal;
}
}

public static class Builder {
private final List<PathElement[]> paths = new ArrayList<>();
private String message = null;
private String name = null;

/**
* Sets a message on the builder.
*
* @param message The message to set.
* @return this.
*/
public Builder message(final String message) {
this.message = message;
return this;
}

/**
* Sets a name on the builder.
*
* @param name The name to set.
* @return this.
*/
public Builder name(final String name) {
this.name = name;
return this;
}

/**
* Sets a path on the builder.
*
* @param path The path to set.
* @return this.
*/
public Builder path(final PathElement[] path) {
this.paths.add(path);
return this;
}

/**
* Builds a ConstraintFailure instance from the current builder.
*
* @return A ConstraintFailure instance.
*/
public ConstraintFailure build() {
PathElement[][] paths =
this.paths.toArray(new PathElement[this.paths.size()][]);
return new ConstraintFailure(this.message, this.name,
this.paths.isEmpty() ? null : paths);
}

}

}
Loading
Loading