Skip to content

Commit

Permalink
Override equals for FilteredLog.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Nov 10, 2023
1 parent 8858d4f commit ce49eaa
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
41 changes: 39 additions & 2 deletions src/main/java/edu/hm/hafner/util/FilteredLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang3.StringUtils;
Expand All @@ -18,7 +19,7 @@
*
* @author Ullrich Hafner
*/
public class FilteredLog implements Serializable {
public final class FilteredLog implements Serializable {
private static final long serialVersionUID = -8552323621953159904L;

private static final int DEFAULT_MAX_LINES = 20;
Expand Down Expand Up @@ -69,7 +70,7 @@ public FilteredLog(final String title, final int maxLines) {
*
* @return this
*/
protected Object readResolve() {
private Object readResolve() {
lock = new ReentrantLock();

return this;
Expand Down Expand Up @@ -260,4 +261,40 @@ public void merge(final FilteredLog other) {
lock.unlock();
}
}

@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}

FilteredLog log = (FilteredLog) o;

Check warning on line 274 in src/main/java/edu/hm/hafner/util/FilteredLog.java

View workflow job for this annotation

GitHub Actions / Autograding results

Varifier

Consider using `var` here to avoid boilerplate.

Check warning on line 274 in src/main/java/edu/hm/hafner/util/FilteredLog.java

View check run for this annotation

Jenkins (hafner.hm.edu) / Error Prone

Varifier

NORMAL: Consider using `var` here to avoid boilerplate.
Raw output
Did you mean: <pre><code>var log = (FilteredLog) o;</code></pre><p><a href="https://errorprone.info/bugpattern/Varifier">See ErrorProne documentation.</a></p>

if (maxLines != log.maxLines) {
return false;
}
if (lines != log.lines) {
return false;
}
if (!Objects.equals(title, log.title)) {
return false;
}
if (!infoMessages.equals(log.infoMessages)) {
return false;
}
return errorMessages.equals(log.errorMessages);
}

@Override
public int hashCode() {
int result = title != null ? title.hashCode() : 0;

Check warning on line 293 in src/main/java/edu/hm/hafner/util/FilteredLog.java

View check run for this annotation

Jenkins (hafner.hm.edu) / JaCoCo Coverage

Partially covered line

Line 293 is only partially covered, one branch is missing
result = 31 * result + maxLines;
result = 31 * result + lines;
result = 31 * result + infoMessages.hashCode();
result = 31 * result + errorMessages.hashCode();
return result;
}
}
14 changes: 14 additions & 0 deletions src/test/java/edu/hm/hafner/util/FilteredLogTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package edu.hm.hafner.util;

import java.util.concurrent.locks.ReentrantLock;

import org.apache.commons.lang3.StringUtils;
import org.assertj.core.api.recursive.comparison.RecursiveComparisonConfiguration;
import org.junit.jupiter.api.Test;

import nl.jqno.equalsverifier.EqualsVerifier;
import nl.jqno.equalsverifier.Warning;

import static edu.hm.hafner.util.assertions.Assertions.*;

/**
Expand Down Expand Up @@ -121,6 +126,15 @@ void shouldLog20ErrorsByDefault() {
.contains("info24");
}

@Test
void shouldAdhereToEquals() {
EqualsVerifier.forClass(FilteredLog.class)
.withIgnoredFields("lock")
.withPrefabValues(ReentrantLock.class, new ReentrantLock(), new ReentrantLock())
.suppress(Warning.NONFINAL_FIELDS)
.verify();
}

@Override
protected void assertThatRestoredInstanceEqualsOriginalInstance(final FilteredLog original,
final FilteredLog restored) {
Expand Down

0 comments on commit ce49eaa

Please sign in to comment.