Skip to content

Commit

Permalink
replace ConditionEvent.addInvertedTo(..) by invert()
Browse files Browse the repository at this point in the history
I do not see the advantage of encapsulating adding the event inside the `ConditionEvent`. On the other hand it provides a more flexible API if it is possible to simply retrieve the inverted event from a `ConditionEvent` and then decide as the caller what to do with it.

Signed-off-by: Peter Gafert <peter.gafert@tngtech.com>
  • Loading branch information
codecholeric committed Jun 25, 2022
1 parent c60df25 commit 64ddbe4
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@

/**
* An event that occurred while checking an {@link ArchCondition}. This can either be a {@link #isViolation() violation}
* or be allowed. An event that is allowed will turn into a violation if it is
* {@link #addInvertedTo(ConditionEvents) added inverted} to {@link ConditionEvents} (e.g. for negation of the rule).
* or be allowed. An event that is allowed will turn into a violation if it is {@link #invert() inverted}
* (e.g. for negation of the rule).
*/
@PublicAPI(usage = INHERITANCE)
public interface ConditionEvent {
Expand All @@ -38,15 +38,13 @@ public interface ConditionEvent {
boolean isViolation();

/**
* Adds the 'opposite' of the event. <br>
* E.g. <i>The event is a violation, if some conditions A and B are both true?</i>
* <br> {@literal ->} <i>The 'inverted' event is a violation if either A or B (or both) are not true</i><br>
* In the most simple case, this is just an equivalent event evaluating {@link #isViolation()}
* inverted.
*
* @param events The events to add the 'inverted self' to
* @return the 'opposite' of the event. <br>
* Assume e.g. <i>The event is a violation, if some conditions A and B are both true</i>
* <br> {@literal =>} <i>The 'inverted' event is a violation if either A or B (or both) are not true</i><br>
* In the most simple case, this is just an equivalent event evaluating {@link #isViolation()}
* inverted.
*/
void addInvertedTo(ConditionEvents events);
ConditionEvent invert();

/**
* @return A textual description of this event as a list of lines
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,8 @@ public boolean isViolation() {
}

@Override
public void addInvertedTo(ConditionEvents events) {
delegate.addInvertedTo(events);
public ConditionEvent invert() {
return new FilteredEvent(delegate.invert(), linePredicate);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public boolean isViolation() {
}

@Override
public void addInvertedTo(ConditionEvents events) {
events.add(new SimpleConditionEvent(correspondingObject, !conditionSatisfied, message));
public ConditionEvent invert() {
return new SimpleConditionEvent(correspondingObject, !conditionSatisfied, message);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public boolean isViolation() {
}

@Override
public void addInvertedTo(ConditionEvents events) {
events.add(new OrConditionEvent<>(correspondingObject, invert(evaluatedConditions)));
public ConditionEvent invert() {
return new OrConditionEvent<>(correspondingObject, invert(evaluatedConditions));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public boolean isViolation() {
}

@Override
public void addInvertedTo(ConditionEvents events) {
events.add(new OnlyConditionEvent(correspondingObjects, violating, allowed));
public ConditionEvent invert() {
return new OnlyConditionEvent(correspondingObjects, violating, allowed);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ public boolean isViolation() {
}

@Override
public void addInvertedTo(ConditionEvents events) {
events.add(new AnyConditionEvent(correspondingObjects, violating, allowed));
public ConditionEvent invert() {
return new AnyConditionEvent(correspondingObjects, violating, allowed);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ private ConditionWithEvents<T> invert(ConditionWithEvents<T> evaluation) {
Stream.concat(
evaluation.events.getAllowed().stream(),
evaluation.events.getViolating().stream()
).forEach(event -> event.addInvertedTo(invertedEvents));
).forEach(event -> invertedEvents.add(event.invert()));
return new ConditionWithEvents<>(evaluation.condition, invertedEvents);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private static class InvertingConditionEvents extends DelegatingConditionEvents

@Override
public void add(ConditionEvent event) {
event.addInvertedTo(delegate);
delegate.add(event.invert());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ConditionEvent;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.conditions.AndCondition.AndConditionEvent;

import static java.util.Collections.singleton;

Expand All @@ -45,8 +47,8 @@ public boolean isViolation() {
}

@Override
public void addInvertedTo(ConditionEvents events) {
events.add(new AndCondition.AndConditionEvent<>(correspondingObject, invert(evaluatedConditions)));
public ConditionEvent invert() {
return new AndConditionEvent<>(correspondingObject, invert(evaluatedConditions));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public boolean isViolation() {
}

@Override
public void addInvertedTo(ConditionEvents events) {
public ConditionEvent invert() {
throw new UnsupportedOperationException("Implement me");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ public boolean isViolation() {
}

@Override
public void addInvertedTo(ConditionEvents events) {
public ConditionEvent invert() {
throw new UnsupportedOperationException("Implement me");
}

Expand Down

0 comments on commit 64ddbe4

Please sign in to comment.