Skip to content
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
48 changes: 48 additions & 0 deletions src/main/java/ru/ewc/decita/EqualsCondition.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* MIT License
*
* Copyright (c) 2023-2024 Eugene Terekhov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package ru.ewc.decita;

import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

/**
* I am a specific {@link Condition} that checks if my right part exactly equals my left one.
* @since 0.2
*/
public class EqualsCondition extends SingleCondition {
/**
* Ctor.
*
* @param left The {@link Coordinate} that is its left part.
* @param right The {@link Coordinate} that is its right part.
*/
public EqualsCondition(final Coordinate left, final Coordinate right) {
super(left, right);
}

@Override
protected final Matcher<Coordinate> comparisonFor() {
return Matchers.equalTo(this.rightPart());
}
}
48 changes: 20 additions & 28 deletions src/main/java/ru/ewc/decita/SingleCondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

import lombok.EqualsAndHashCode;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

/**
* I represent a simple {@link Condition} using two {@link Coordinate}s and some comparison
Expand All @@ -35,19 +34,12 @@
* @since 0.1
*/
@EqualsAndHashCode
public final class SingleCondition implements Condition {
// @todo #31 Create concrete Conditions classes for 1-Order and 2-Order Conditions.
// 1-Order - is a 'EqualsCondition', 2-Order - is a 'EqualsTrueCondition'
public abstract class SingleCondition implements Condition {
/**
* Left-side {@link Coordinate}.
*/
private final Coordinate left;

/**
* The comparison operation applied to both operands.
*/
private final String comparison;

/**
* Right-side {@link Coordinate}.
*/
Expand All @@ -57,52 +49,52 @@ public final class SingleCondition implements Condition {
* Constructor.
*
* @param left Left-side {@link Coordinate}.
* @param comparison The comparison operation applied to both operands.
* @param right Right-side {@link Coordinate}.
*/
public SingleCondition(final Coordinate left, final String comparison, final Coordinate right) {
protected SingleCondition(final Coordinate left, final Coordinate right) {
this.left = left;
this.comparison = comparison;
this.right = right;
}

@Override
public boolean evaluate(final ComputationContext context) throws DecitaException {
public final boolean evaluate(final ComputationContext context) throws DecitaException {
this.right.locateIn(context);
this.left.locateIn(context);
return this.isSatisfied();
}

@Override
public boolean isEvaluated() {
public final boolean isEvaluated() {
return this.right.isComputed() && this.left.isComputed();
}

@Override
public boolean isNotSatisfied() {
public final boolean isNotSatisfied() {
return this.isEvaluated() && !this.isSatisfied();
}

/**
* Checks if this {@link Condition} resolves to {@code true}.
* Creates a {@link Matcher} that corresponds to the given operation and {@link Coordinate}.
*
* @return True, if it does.
* @return The {@link Matcher} to use in this {@link Condition}.
*/
private boolean isSatisfied() {
return this.isEvaluated() && this.comparisonFor(this.right).matches(this.left);
protected abstract Matcher<Coordinate> comparisonFor();

/**
* Returns the right part of this {@link Condition}.
*
* @return The {@link Coordinate} that is its right part.
*/
protected final Coordinate rightPart() {
return this.right;
}

/**
* Creates a {@link Matcher} that corresponds to the given operation and {@link Coordinate}.
* Checks if this {@link Condition} resolves to {@code true}.
*
* @param coordinate The {@link Coordinate} to use with the {@link Matcher}.
* @return The {@link Matcher} to use in this {@link Condition}.
* @return True, if it does.
*/
private Matcher<?> comparisonFor(final Coordinate coordinate) {
Matcher<?> matcher = Matchers.not(Matchers.anything());
if ("=".equalsIgnoreCase(this.comparison)) {
matcher = Matchers.equalTo(coordinate);
}
return matcher;
private boolean isSatisfied() {
return this.isEvaluated() && this.comparisonFor().matches(this.left);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*
* @since 0.1
*/
class SingleConditionTest {
class EqualsConditionTest {
@Test
void testInstantiating() {
final Condition target = TestObjects.alwaysTrueConstantCondition();
Expand All @@ -54,8 +54,9 @@ void testEvaluatingToTrue() throws DecitaException {

@Test
void testNotComputedCondition() {
final Condition target = new SingleCondition(
TestObjects.alwaysTrueConditionCoordinate(), "=", TestObjects.valueTrue()
final Condition target = new EqualsCondition(
TestObjects.alwaysTrueConditionCoordinate(),
TestObjects.valueTrue()
);
MatcherAssert.assertThat(
target.isEvaluated(),
Expand Down
12 changes: 5 additions & 7 deletions src/test/java/ru/ewc/decita/TestObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ static ComputationContext contextWithLocators(final Map<String, Locator> locator
*
* @return A simple and always true {@link Condition}.
*/
static SingleCondition alwaysTrueConstantCondition() {
return new SingleCondition(valueTrue(), "=", valueTrue());
static Condition alwaysTrueConstantCondition() {
return new EqualsCondition(valueTrue(), valueTrue());
}

/**
Expand Down Expand Up @@ -114,9 +114,8 @@ static Rule alwaysTrueEqualsTrueRule() {
static Rule alwaysTrueEqualsFalseRule() {
return new Rule()
.withCondition(
new SingleCondition(
new EqualsCondition(
alwaysTrueConditionCoordinate(),
"=",
new Coordinate(Locator.CONSTANT_VALUES, "false")
)
)
Expand All @@ -143,10 +142,9 @@ static List<Rule> rulesList() {
* @param expected The name of the table's computation field.
* @return A {@link Condition} pointing to 'hello-world' table result.
*/
static SingleCondition helloWorldOutcomeIs(final String expected) {
return new SingleCondition(
static Condition helloWorldOutcomeIs(final String expected) {
return new EqualsCondition(
new Coordinate("hello-world", "outcome"),
"=",
new Coordinate(Locator.CONSTANT_VALUES, expected)
);
}
Expand Down