Skip to content

Commit

Permalink
Implement Comparable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ullenius authored and Johannestegner committed Jun 27, 2020
1 parent adca423 commit 22d8b51
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/main/java/dev/personnummer/Personnummer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*
* @author Johannes Tegnér
*/
public final class Personnummer {
public final class Personnummer implements Comparable<Personnummer> {
private static final Pattern regexPattern;

static {
Expand Down Expand Up @@ -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));
}

}
69 changes: 60 additions & 9 deletions src/test/java/PersonnummerEqualsHashcode.java
Original file line number Diff line number Diff line change
@@ -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"));

Expand All @@ -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");

Expand All @@ -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<Personnummer> 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);
});


}

}

0 comments on commit 22d8b51

Please sign in to comment.