Commit 891b12de authored by Nico Mack's avatar Nico Mack
Browse files

Refactored Couple Class into Pair class

parent c8ff5923
......@@ -30,13 +30,13 @@ import java.util.Objects;
// * Class Definition *
// ***************************************************************************
public class Couple<T extends Comparable> implements Comparable<Couple<T>> {
public class Pair<T extends Comparable> implements Comparable<Pair<T>> {
private T first;
private T second;
boolean commutative;
boolean ordered;
public static boolean COMMUTATIVE = true;
public static boolean NON_COMMUTATIVE = false;
public static boolean UNORDERED = false;
public static boolean ORDERED = true;
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -44,18 +44,18 @@ public class Couple<T extends Comparable> implements Comparable<Couple<T>> {
// ***************************************************************************
// ---------------------------------------------------------------------------
public Couple(T first, T second) {
public Pair(T first, T second) {
this.first = first;
this.second = second;
this.commutative = true;
this.ordered = UNORDERED;
}
// ---------------------------------------------------------------------------
public Couple(T first, T second, boolean commutative) {
public Pair(T first, T second, boolean ordered) {
this.first = first;
this.second = second;
this.commutative = commutative;
this.ordered = ordered;
}
// ---------------------------------------------------------------------------
// ***************************************************************************
......@@ -71,11 +71,11 @@ public class Couple<T extends Comparable> implements Comparable<Couple<T>> {
return false;
if (getClass() != object.getClass())
return false;
Couple<T> other = (Couple<T>) object;
Pair<T> other = (Pair<T>) object;
boolean match = Objects.equals(first, other.first) &&
Objects.equals(second, other.second);
if (!match && this.commutative) {
if (!match && !this.ordered) {
match |= Objects.equals(second, other.first) &&
Objects.equals(first, other.second);
}
......@@ -88,7 +88,7 @@ public class Couple<T extends Comparable> implements Comparable<Couple<T>> {
@Override
public int hashCode() {
int hash = Objects.hash(first, second);
if (commutative) {
if (!ordered) {
hash *= Objects.hash(second, first);
}
......@@ -99,13 +99,13 @@ public class Couple<T extends Comparable> implements Comparable<Couple<T>> {
/** {@inheritDoc} */
@Override
public int compareTo(Couple<T> o) {
public int compareTo(Pair<T> o) {
int comparison = first.compareTo(o.first);
if (comparison >= 0) {
comparison = second.compareTo(o.second);
}
if (this.commutative && (comparison != 0)) {
if (!this.ordered && (comparison != 0)) {
comparison = first.compareTo(o.second);
if (comparison >= 0) {
comparison = second.compareTo(o.first);
......@@ -116,26 +116,68 @@ public class Couple<T extends Comparable> implements Comparable<Couple<T>> {
// ---------------------------------------------------------------------------
/**
* Simple getter method for first.
* Simple getter method for first. For commutative couples (default), method will always return
* the smaller partner.
*
* @return The value of first.
*/
// ---------------------------------------------------------------------------
public T getFirst() {
return first;
return (ordered) ? first : (first.compareTo(second) > 0) ? second : first;
}
// ---------------------------------------------------------------------------
/**
* Simple getter method for second.
* Simple getter method for second. For commutative couples (default), method will always return
* the larger partner.
*
* @return The value of second.
*/
// ---------------------------------------------------------------------------
public T getSecond() {
return second;
return (ordered) ? second : (second.compareTo(first) >= 0) ? second : first;
}
// ---------------------------------------------------------------------------
/**
* @param value
* @return
*/
// ---------------------------------------------------------------------------
public boolean contains(T value) {
return ((this.first != null) && this.first.equals(value)) || ((this.second != null) && this.second.equals(value));
}
// ---------------------------------------------------------------------------
/**
* @param value
* @return
*/
// ---------------------------------------------------------------------------
public T getOther(T value) {
if ((this.first != null) && this.first.equals(value)) {
return this.second;
}
if ((this.second != null) && this.second.equals(value)) {
return this.first;
}
return null;
}
// ---------------------------------------------------------------------------
@Override
public String toString() {
StringBuilder formatted = new StringBuilder((ordered) ? "(" : "{"); //$NON-NLS-1$ //$NON-NLS-2$
formatted.append(this.getFirst()).append(',').append(this.getSecond()).append((ordered) ? ')' : '}');
return formatted.toString();
}
}
......@@ -34,26 +34,26 @@ import java.util.List;
* @version [major].[minor].[micro]
*/
public class CoupleTests {
Couple<String> A_co;
Couple<String> B_co;
Couple<String> C_co;
Pair<String> A_co;
Pair<String> B_co;
Pair<String> C_co;
Couple<String> A_nc;
Couple<String> B_nc;
Couple<String> C_nc;
Pair<String> A_nc;
Pair<String> B_nc;
Pair<String> C_nc;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
A_co = new Couple<>("Apple", "Pear");
B_co = new Couple<>("Pear", "Apple");
C_co = new Couple<>("Pear", "Cherry");
A_co = new Pair<>("Apple", "Pear");
B_co = new Pair<>("Pear", "Apple");
C_co = new Pair<>("Pear", "Cherry");
A_nc = new Couple<>("Apple", "Pear", Couple.NON_COMMUTATIVE);
B_nc = new Couple<>("Pear", "Apple", Couple.NON_COMMUTATIVE);
C_nc = new Couple<>("Pear", "Cherry", Couple.NON_COMMUTATIVE);
A_nc = new Pair<>("Apple", "Pear", Pair.ORDERED);
B_nc = new Pair<>("Pear", "Apple", Pair.ORDERED);
C_nc = new Pair<>("Pear", "Cherry", Pair.ORDERED);
}
......@@ -84,7 +84,7 @@ public class CoupleTests {
@Test
public void testList() {
List<Couple<String>> alreadySeen = new ArrayList<>();
List<Pair<String>> alreadySeen = new ArrayList<>();
alreadySeen.add(A_nc);
assertTrue(alreadySeen.contains(A_co));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment