From 22d8b510706a34ee6e106b7dc3bfe181d10aff3c Mon Sep 17 00:00:00 2001 From: "Anosh D. Ullenius" Date: Sat, 27 Jun 2020 00:31:41 +0200 Subject: [PATCH] Implement Comparable interface --- .../java/dev/personnummer/Personnummer.java | 8 ++- src/test/java/PersonnummerEqualsHashcode.java | 69 ++++++++++++++++--- 2 files changed, 66 insertions(+), 11 deletions(-) diff --git a/src/main/java/dev/personnummer/Personnummer.java b/src/main/java/dev/personnummer/Personnummer.java index 1e6c26d..04c5b33 100644 --- a/src/main/java/dev/personnummer/Personnummer.java +++ b/src/main/java/dev/personnummer/Personnummer.java @@ -9,7 +9,7 @@ * * @author Johannes Tegnér */ -public final class Personnummer { +public final class Personnummer implements Comparable { private static final Pattern regexPattern; static { @@ -241,6 +241,10 @@ public boolean equals(Object obj) { Personnummer other = (Personnummer) obj; return Objects.equals(format(true), other.format(true)); } - + + @Override + public int compareTo(Personnummer other) { + return format(true).compareTo(other.format(true)); + } } diff --git a/src/test/java/PersonnummerEqualsHashcode.java b/src/test/java/PersonnummerEqualsHashcode.java index a4a6f9f..abdb34c 100644 --- a/src/test/java/PersonnummerEqualsHashcode.java +++ b/src/test/java/PersonnummerEqualsHashcode.java @@ -1,19 +1,25 @@ import static org.junit.jupiter.api.Assertions.*; -import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.*; import dev.personnummer.*; class PersonnummerEqualsHashcode { - + + private static final String YOUNG_LONG_FORMAT = "201701022384"; + @Test void testEquals() throws PersonnummerException { - assertEquals(new Personnummer("201701022384"), new Personnummer("201701022384")); - assertEquals(new Personnummer("201701022384"), new Personnummer("20170102-2384")); - assertEquals(new Personnummer("201701022384"), new Personnummer("20170102+2384")); - assertEquals(new Personnummer("201701022384"), new Personnummer("170102-2384")); + assertEquals(new Personnummer(YOUNG_LONG_FORMAT), new Personnummer(YOUNG_LONG_FORMAT)); + assertEquals(new Personnummer(YOUNG_LONG_FORMAT), new Personnummer("20170102-2384")); + assertEquals(new Personnummer(YOUNG_LONG_FORMAT), new Personnummer("20170102+2384")); + assertEquals(new Personnummer(YOUNG_LONG_FORMAT), new Personnummer("170102-2384")); assertNotEquals(new Personnummer("170102+2384"), new Personnummer("170102-2384")); - assertNotEquals(new Personnummer("201701022384"), new Personnummer("191701022384")); + assertNotEquals(new Personnummer(YOUNG_LONG_FORMAT), new Personnummer("191701022384")); assertNotEquals(new Personnummer("20170102-2384"), new Personnummer("19170102-2384")); assertNotEquals(new Personnummer("20170102+2384"), new Personnummer("19170102+2384")); @@ -22,7 +28,7 @@ void testEquals() throws PersonnummerException { @Test void testHashCode() throws PersonnummerException { - Personnummer young = new Personnummer("201701022384"); + Personnummer young = new Personnummer(YOUNG_LONG_FORMAT); Personnummer young2 = new Personnummer("1701022384"); Personnummer other = new Personnummer("194308239088"); @@ -34,9 +40,54 @@ void testHashCode() throws PersonnummerException { @Test void testToString() throws PersonnummerException { - Personnummer young = new Personnummer("201701022384"); + Personnummer young = new Personnummer(YOUNG_LONG_FORMAT); assertEquals("170102-2384", young.toString()); assertEquals(young.format(), young.toString()); } + @Test + void naturalOrderingWorks() throws PersonnummerException { + + List numbers = new ArrayList<>(); + + final Personnummer retiree = new Personnummer("194308239088"); + final Personnummer young = new Personnummer(YOUNG_LONG_FORMAT); + final Personnummer centenarian = new Personnummer("170102+2384"); + + + numbers.add(retiree); + numbers.add(young); + numbers.add(centenarian); + numbers.add(retiree); + assertEquals(4, numbers.size()); + + numbers.sort(null); + + assertEquals(centenarian, numbers.get(0)); + assertEquals(retiree, numbers.get(1)); + assertEquals(retiree, numbers.get(2)); + assertEquals(young, numbers.get(3)); + } + + @Test + void testLongFormat() throws PersonnummerException { + + final Personnummer centenarian = new Personnummer("170102+2384"); + assertEquals("191701022384", centenarian.format(true)); + } + + + @Test() + void disallowNullValuesForOrdering() throws PersonnummerException { + + final Personnummer young = new Personnummer(YOUNG_LONG_FORMAT); + + assertFalse(young.equals(null)); + assertThrows(NullPointerException.class, () -> { + young.compareTo(null); + }); + + + } + } \ No newline at end of file