-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Fix issue with hash code in the apache preset * Add tests
- Loading branch information
Showing
12 changed files
with
136 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,5 @@ build/ | |
target/ | ||
*.iml | ||
*rebel.xml | ||
src/generated/ | ||
src/generated/ | ||
key.gpg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/jparams/verifier/tostring/HashCodeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.jparams.verifier.tostring; | ||
|
||
/** | ||
* Hash code provider | ||
*/ | ||
@FunctionalInterface | ||
public interface HashCodeProvider | ||
{ | ||
/** | ||
* Provide object hash code | ||
* | ||
* @param obj object | ||
* @return hash code | ||
*/ | ||
int provide(Object obj); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/jparams/verifier/tostring/ObjectHashCodeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.jparams.verifier.tostring; | ||
|
||
/** | ||
* Provides the hash code by calling {@link Object#hashCode()} | ||
*/ | ||
public class ObjectHashCodeProvider implements HashCodeProvider | ||
{ | ||
@Override | ||
public int provide(final Object obj) | ||
{ | ||
return obj.hashCode(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/jparams/verifier/tostring/SystemIdentityHashCodeProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.jparams.verifier.tostring; | ||
|
||
/** | ||
* Provides the System identity hash code by calling {@link System#identityHashCode(Object)} | ||
*/ | ||
public class SystemIdentityHashCodeProvider implements HashCodeProvider | ||
{ | ||
@Override | ||
public int provide(final Object obj) | ||
{ | ||
return System.identityHashCode(obj); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/test/java/com/jparams/verifier/tostring/ObjectHashCodeProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jparams.verifier.tostring; | ||
|
||
import com.jparams.verifier.tostring.pojo.Person; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class ObjectHashCodeProviderTest | ||
{ | ||
@Test | ||
public void testProvider() | ||
{ | ||
final Person person = new Person(1, "1", "1"); | ||
final int hashCode = new ObjectHashCodeProvider().provide(person); | ||
assertThat(hashCode).isEqualTo(person.hashCode()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
src/test/java/com/jparams/verifier/tostring/SystemIdentityHashCodeProviderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.jparams.verifier.tostring; | ||
|
||
import com.jparams.verifier.tostring.pojo.Person; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class SystemIdentityHashCodeProviderTest | ||
{ | ||
@Test | ||
public void testProvider() | ||
{ | ||
final Person person = new Person(1, "1", "1"); | ||
final int hashCode = new SystemIdentityHashCodeProvider().provide(person); | ||
assertThat(hashCode).isEqualTo(System.identityHashCode(person)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/java/com/jparams/verifier/tostring/preset/AbstractDataTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,40 @@ | ||
package com.jparams.verifier.tostring.preset; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
abstract class AbstractDataTest | ||
{ | ||
String str; | ||
List<Integer> list; | ||
Map<String, BigDecimal> map; | ||
String[] ary; | ||
|
||
@Override | ||
public boolean equals(final Object other) | ||
{ | ||
if (this == other) | ||
{ | ||
return true; | ||
} | ||
|
||
if (other == null || getClass() != other.getClass()) | ||
{ | ||
return false; | ||
} | ||
|
||
final AbstractDataTest that = (AbstractDataTest) other; | ||
return Objects.equals(str, that.str) && Objects.equals(list, that.list) && Objects.equals(map, that.map) && Arrays.equals(ary, that.ary); | ||
} | ||
|
||
@Override | ||
public int hashCode() | ||
{ | ||
int result = Objects.hash(str, list, map); | ||
result = 31 * result + Arrays.hashCode(ary); | ||
return result; | ||
} | ||
} |