Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisJehan committed Jun 28, 2024
1 parent e2c5f51 commit afe4881
Show file tree
Hide file tree
Showing 7 changed files with 522 additions and 126 deletions.
23 changes: 18 additions & 5 deletions src/main/java/com/github/alexisjehan/mender/dsv/DsvMender.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,13 @@ public DsvMender build() {
* {@link EstimationEvaluator}s is empty
* @since 1.0.0
*/
public DsvMender(final String delimiter, final int length, final int maxDepth, final Set<ConstraintEvaluator<String[]>> constraintEvaluators, final Set<EstimationEvaluator<String[]>> estimationEvaluators) {
public DsvMender(
final String delimiter,
final int length,
final int maxDepth,
final Set<ConstraintEvaluator<String[]>> constraintEvaluators,
final Set<EstimationEvaluator<String[]>> estimationEvaluators
) {
Ensure.notNullAndNotEmpty("delimiter", delimiter);
Ensure.greaterThanOrEqualTo("length", length, 2);
Ensure.greaterThanOrEqualTo("maxDepth", maxDepth, 1);
Expand Down Expand Up @@ -503,20 +509,27 @@ public String[] mend(final String... values) {
DsvMendCandidate bestCandidate = null;
for (final var child : children) {
final var optionalCandidateScore = DoubleStream.concat(
constraintEvaluators.stream().mapToDouble(constraintEvaluator -> constraintEvaluator.evaluate(child)),
estimationEvaluators.stream().mapToDouble(estimationEvaluator -> estimationEvaluator.evaluate(child))
constraintEvaluators.stream()
.mapToDouble(constraintEvaluator -> constraintEvaluator.evaluate(child)),
estimationEvaluators.stream()
.mapToDouble(estimationEvaluator -> estimationEvaluator.evaluate(child))
).average();
if (optionalCandidateScore.isPresent()) {
final var candidateScore = optionalCandidateScore.getAsDouble();
final var candidate = new DsvMendCandidate(child, candidateScore);
candidates.add(candidate);
if (!Double.isNaN(candidateScore) && (null == bestCandidate || bestCandidate.getScore() < candidateScore)) {
if (!Double.isNaN(candidateScore)
&& (null == bestCandidate || bestCandidate.getScore() < candidateScore)) {
bestCandidate = candidate;
}
}
}
if (null == bestCandidate) {
throw new MendException("No solution for values: " + ToString.toString(values) + " (consider using others constraints and estimations)");
throw new MendException(
"No solution for values: "
+ ToString.toString(values)
+ " (consider using others constraints and estimations)"
);
}
lastResult = new DsvMendResult(values, candidates, bestCandidate);
return bestCandidate.getValue();
Expand Down
78 changes: 66 additions & 12 deletions src/main/java/examples/ConcreteExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,65 @@ public final class ConcreteExample {
// Source: https://en.wikipedia.org/wiki/Java_version_history
private static final String DATA = String.join(
System.lineSeparator(),
String.join(DELIMITER, "Release", "Release date", "Highlights"),
String.join(DELIMITER, "Java SE 9", "2017-09-21", "Initial release"),
String.join(DELIMITER, "Java SE 9.0.1", "2017-10-17", "October 2017 security fixes and critical bug fixes"),
String.join(DELIMITER, "Java SE 9.0.4", "2018-01-16", "Final release for JDK 9; January 2018 security fixes and critical bug fixes"),
String.join(DELIMITER, "Java SE 10", "2018-03-20", "Initial release"),
String.join(DELIMITER, "Java SE 10.0.1", "2018-04-17", "Security fixes, 5 bug fixes"), // One value contains the delimiter
String.join(DELIMITER, "Java SE 11", "2018-09-25", "Initial release"),
String.join(DELIMITER, "Java SE 11.0.1", "2018-10-16", "Security & bug fixes"),
String.join(DELIMITER, "Java SE 11.0.2", "2019-01-15", "Security & bug fixes"),
String.join(DELIMITER, "Java SE 12", "Initial release") // Missing the release date value
String.join(
DELIMITER,
"Release",
"Release date",
"Highlights"
),
String.join(
DELIMITER,
"Java SE 9",
"2017-09-21",
"Initial release"
),
String.join(
DELIMITER,
"Java SE 9.0.1",
"2017-10-17",
"October 2017 security fixes and critical bug fixes"
),
String.join(
DELIMITER,
"Java SE 9.0.4",
"2018-01-16",
"Final release for JDK 9; January 2018 security fixes and critical bug fixes"
),
String.join(
DELIMITER,
"Java SE 10",
"2018-03-20",
"Initial release"
),
String.join(
DELIMITER,
"Java SE 10.0.1",
"2018-04-17",
"Security fixes, 5 bug fixes" // Contains the delimiter
),
String.join(
DELIMITER,
"Java SE 11",
"2018-09-25",
"Initial release"
),
String.join(
DELIMITER,
"Java SE 11.0.1",
"2018-10-16",
"Security & bug fixes"
),
String.join(
DELIMITER,
"Java SE 11.0.2",
"2019-01-15",
"Security & bug fixes"
),
String.join(
DELIMITER,
"Java SE 12",
"Initial release"
) // Missing the release date value
);

private ConcreteExample() {}
Expand All @@ -58,8 +107,13 @@ public static void main(final String... args) throws IOException {
final var mender = DsvMender.builder()
.withDelimiter(DELIMITER)
.withLength(LENGTH)
.withConstraint(value -> value.startsWith("Java SE"), 0) // values[0] must start with "Java SE"
.withConstraint(value -> value.isEmpty() || 10 == value.length(), 1)// values[1] must be empty or have a length of 10

// values[0] must start with "Java SE"
.withConstraint(value -> value.startsWith("Java SE"), 0)

// values[1] must be empty or have a length of 10
.withConstraint(value -> value.isEmpty() || 10 == value.length(), 1)

.build();
try (var reader = new BufferedReader(new StringReader(DATA))) {
printValues(Strings.split(mender.getDelimiter(), reader.readLine()).toArray(String[]::new)); // Header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ void testGetMessage() {

@Test
void testSerializable() {
assertThat(Serializables.<MendException>deserialize(Serializables.serialize(mendException))).hasSameClassAs(mendException);
assertThat(
Serializables.<MendException>deserialize(
Serializables.serialize(mendException)
)
).hasSameClassAs(mendException);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ void testConstructorImmutable() {

@Test
void testConstructorInvalid() {
assertThatNullPointerException().isThrownBy(() -> new DsvMendCandidate(null, SCORE));
assertThatIllegalArgumentException().isThrownBy(() -> new DsvMendCandidate(ObjectArrays.empty(String.class), SCORE));
assertThatNullPointerException().isThrownBy(() -> new DsvMendCandidate(ObjectArrays.of((String) null), SCORE));
assertThatIllegalArgumentException().isThrownBy(() -> new DsvMendCandidate(VALUE, -1.0d));
assertThatNullPointerException()
.isThrownBy(() -> new DsvMendCandidate(null, SCORE));
assertThatIllegalArgumentException()
.isThrownBy(() -> new DsvMendCandidate(ObjectArrays.empty(String.class), SCORE));
assertThatNullPointerException()
.isThrownBy(() -> new DsvMendCandidate(ObjectArrays.of((String) null), SCORE));
assertThatIllegalArgumentException()
.isThrownBy(() -> new DsvMendCandidate(VALUE, -1.0d));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,38 +57,72 @@ void testConstructorImmutable() {

@Test
void testConstructorInvalid() {
assertThatNullPointerException().isThrownBy(() -> new DsvMendResult(null, CANDIDATES, BEST_CANDIDATE));
assertThatIllegalArgumentException().isThrownBy(() -> new DsvMendResult(ObjectArrays.empty(String.class), CANDIDATES, BEST_CANDIDATE));
assertThatNullPointerException().isThrownBy(() -> new DsvMendResult(ObjectArrays.of((String) null), CANDIDATES, BEST_CANDIDATE));
assertThatNullPointerException().isThrownBy(() -> new DsvMendResult(VALUE, null, BEST_CANDIDATE));
assertThatIllegalArgumentException().isThrownBy(() -> new DsvMendResult(VALUE, Set.of(), BEST_CANDIDATE));
assertThatNullPointerException().isThrownBy(() -> new DsvMendResult(VALUE, Collections.singleton(null), BEST_CANDIDATE));
assertThatNullPointerException().isThrownBy(() -> new DsvMendResult(VALUE, CANDIDATES, null));
assertThatNullPointerException()
.isThrownBy(() -> new DsvMendResult(null, CANDIDATES, BEST_CANDIDATE));
assertThatIllegalArgumentException()
.isThrownBy(() -> new DsvMendResult(ObjectArrays.empty(String.class), CANDIDATES, BEST_CANDIDATE));
assertThatNullPointerException()
.isThrownBy(() -> new DsvMendResult(ObjectArrays.of((String) null), CANDIDATES, BEST_CANDIDATE));
assertThatNullPointerException()
.isThrownBy(() -> new DsvMendResult(VALUE, null, BEST_CANDIDATE));
assertThatIllegalArgumentException()
.isThrownBy(() -> new DsvMendResult(VALUE, Set.of(), BEST_CANDIDATE));
assertThatNullPointerException()
.isThrownBy(() -> new DsvMendResult(VALUE, Collections.singleton(null), BEST_CANDIDATE));
assertThatNullPointerException()
.isThrownBy(() -> new DsvMendResult(VALUE, CANDIDATES, null));
}

@Test
void testEqualsAndHashCodeAndToString() {
assertThat(mendResult.equals(mendResult)).isTrue();
assertThat(mendResult).isNotEqualTo(new Object());
assertThat(new DsvMendResult(VALUE, CANDIDATES, BEST_CANDIDATE)).satisfies(otherMendResult -> {
assertThat(
new DsvMendResult(
VALUE,
CANDIDATES,
BEST_CANDIDATE
)
).satisfies(otherMendResult -> {
assertThat(mendResult).isNotSameAs(otherMendResult);
assertThat(mendResult).isEqualTo(otherMendResult);
assertThat(mendResult).hasSameHashCodeAs(otherMendResult);
assertThat(mendResult).hasToString(otherMendResult.toString());
});
assertThat(new DsvMendResult(ObjectArrays.of("bar"), CANDIDATES, BEST_CANDIDATE)).satisfies(otherMendResult -> {
assertThat(
new DsvMendResult(
ObjectArrays.of("bar"),
CANDIDATES,
BEST_CANDIDATE
)
).satisfies(otherMendResult -> {
assertThat(mendResult).isNotSameAs(otherMendResult);
assertThat(mendResult).isNotEqualTo(otherMendResult);
assertThat(mendResult).doesNotHaveSameHashCodeAs(otherMendResult);
assertThat(mendResult).doesNotHaveToString(otherMendResult.toString());
});
assertThat(new DsvMendResult(VALUE, Set.of(new DsvMendCandidate(ObjectArrays.of("foo"), 1.0d), new DsvMendCandidate(ObjectArrays.of("bar"), 2.0d)), BEST_CANDIDATE)).satisfies(otherMendResult -> {
assertThat(
new DsvMendResult(
VALUE,
Set.of(
new DsvMendCandidate(ObjectArrays.of("foo"), 1.0d),
new DsvMendCandidate(ObjectArrays.of("bar"), 2.0d)
),
BEST_CANDIDATE
)
).satisfies(otherMendResult -> {
assertThat(mendResult).isNotSameAs(otherMendResult);
assertThat(mendResult).isNotEqualTo(otherMendResult);
assertThat(mendResult).doesNotHaveSameHashCodeAs(otherMendResult);
assertThat(mendResult).doesNotHaveToString(otherMendResult.toString());
});
assertThat(new DsvMendResult(VALUE, CANDIDATES, new DsvMendCandidate(ObjectArrays.of("bar"), 2.0d))).satisfies(otherMendResult -> {
assertThat(
new DsvMendResult(
VALUE,
CANDIDATES,
new DsvMendCandidate(ObjectArrays.of("bar"), 2.0d)
)
).satisfies(otherMendResult -> {
assertThat(mendResult).isNotSameAs(otherMendResult);
assertThat(mendResult).isNotEqualTo(otherMendResult);
assertThat(mendResult).doesNotHaveSameHashCodeAs(otherMendResult);
Expand Down
Loading

0 comments on commit afe4881

Please sign in to comment.