-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Pair.java util for referencing 2 objects + hashing
- Loading branch information
Showing
3 changed files
with
88 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package org.minerift.ether.util.pair; | ||
|
||
import com.google.common.base.Objects; | ||
import com.google.common.base.Preconditions; | ||
|
||
public class Pair<F, S> { | ||
|
||
protected final F first; | ||
protected final S second; | ||
|
||
public Pair(F first, S second) { | ||
this.first = first; | ||
this.second = second; | ||
} | ||
|
||
public Pair(Object[] pair) { | ||
Preconditions.checkArgument(pair.length == 2, "Must only be 2 elements in pair!"); | ||
this.first = (F) pair[0]; | ||
this.second = (S) pair[1]; | ||
} | ||
|
||
public F getFirst() { | ||
return first; | ||
} | ||
|
||
public S getSecond() { | ||
return second; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (!(o instanceof Pair<?, ?> pair)) return false; | ||
return Objects.equal(first, pair.first) && Objects.equal(second, pair.second); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(first, second); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
main/src/main/java/org/minerift/ether/util/pair/SameTypePair.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,30 @@ | ||
package org.minerift.ether.util.pair; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.function.IntFunction; | ||
|
||
public class SameTypePair<T> extends Pair<T, T> { | ||
|
||
public SameTypePair(T first, T second) { | ||
super(first, second); | ||
} | ||
|
||
public SameTypePair(T[] pair) { | ||
super(pair); | ||
} | ||
|
||
private T[] fillArray(T[] emptyArray) { | ||
emptyArray[0] = first; | ||
emptyArray[1] = second; | ||
return emptyArray; | ||
} | ||
|
||
public T[] toArray(Class<T> clazz) { | ||
return fillArray((T[]) Array.newInstance(clazz, 2)); | ||
} | ||
|
||
// Preferred method override | ||
public T[] toArray(IntFunction<T[]> arrayCreator) { | ||
return fillArray(arrayCreator.apply(2)); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
main/src/main/java/org/minerift/ether/util/pair/UUIDPair.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,17 @@ | ||
package org.minerift.ether.util.pair; | ||
|
||
import java.util.UUID; | ||
|
||
public class UUIDPair extends SameTypePair<UUID> { | ||
public UUIDPair(UUID first, UUID second) { | ||
super(first, second); | ||
} | ||
|
||
public UUIDPair(UUID[] pair) { | ||
super(pair); | ||
} | ||
|
||
public UUID[] toArray() { | ||
return toArray(UUID[]::new); | ||
} | ||
} |