Skip to content

Commit

Permalink
Added safe Pair & Triple variants
Browse files Browse the repository at this point in the history
  • Loading branch information
xDec0de committed Jul 13, 2024
1 parent 0406baf commit 96a6ae5
Show file tree
Hide file tree
Showing 12 changed files with 556 additions and 87 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package net.codersky.mcutils.java.tuple;
package net.codersky.mcutils.java.tuple.pair;

import javax.annotation.Nullable;

/**
* An extension of {@link Pair} that doesn't allow the modification
* An implementation of {@link Pair} that doesn't allow the modification
* of its elements, so it doesn't contain setters as
* {@link MutablePair} does.
*
Expand All @@ -16,7 +16,7 @@
*
* @see MutablePair
*/
public class ImmutablePair<F, S> extends Pair<F, S> {
public class ImmutablePair<F, S> implements Pair<F, S> {

private final F first;
private final S second;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package net.codersky.mcutils.java.tuple;
package net.codersky.mcutils.java.tuple.pair;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
* An extension of {@link Pair} that allows changing the
* values of the stored elements, if you need this elements
* An implementation of {@link Pair} that allows changing the
* values of the stored elements, if you need these elements
* to be final, use {@link ImmutablePair}.
*
* @author xDec0de_
Expand All @@ -16,7 +17,7 @@
*
* @see ImmutablePair
*/
public class MutablePair<F, S> extends Pair<F, S> {
public class MutablePair<F, S> implements Pair<F, S> {

private F first;
private S second;
Expand All @@ -35,8 +36,8 @@ public MutablePair(@Nullable F first, @Nullable S second) {
this.second = second;
}

@Nonnull
@Override
@Nullable
public F getFirst() {
return first;
}
Expand All @@ -56,8 +57,8 @@ public F setFirst(@Nullable F first) {
return (this.first = first);
}

@Nonnull
@Override
@Nullable
public S getSecond() {
return second;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package net.codersky.mcutils.java.tuple;

import java.util.Objects;
package net.codersky.mcutils.java.tuple.pair;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
Expand All @@ -17,32 +15,36 @@
* @param <S> The type of the second element to store.
*
* @since MCUtils 1.0.0
*
* @see SafePair
* @see MutablePair
* @see ImmutablePair
*/
public abstract class Pair<F, S> {
public interface Pair<F, S> {

/**
* Gets the first element stored in this {@link Pair},
* which may be {@code null}.
* which may be allowed to be {@code null} or not depending
* on the implementation used.
*
* @return The first element stored in this {@link Pair},
* possibly {@code null}
* @return The first element stored in this {@link Pair}.
*
* @since MCUtils 1.0.0
*/
@Nullable
public abstract F getFirst();
F getFirst();

/**
* Gets the second element stored in this {@link Pair},
* which may be {@code null}.
* which may be allowed to be {@code null} or not depending
* on the implementation used.
*
* @return The second element stored in this {@link Pair},
* possibly {@code null}
* @return The second element stored in this {@link Pair}.
*
* @since MCUtils 1.0.0
*/
@Nullable
public abstract S getSecond();
S getSecond();

/**
* Compares the first element of <b>other</b> with the first
Expand All @@ -60,7 +62,7 @@ public abstract class Pair<F, S> {
*
* @see #getFirst()
*/
public boolean isFirstEqual(@Nonnull Pair<F, ?> other) {
default boolean isFirstEqual(@Nonnull Pair<F, ?> other) {
return getFirst() == null ? other.getFirst() == null : getFirst().equals(other.getFirst());
}

Expand All @@ -78,33 +80,9 @@ public boolean isFirstEqual(@Nonnull Pair<F, ?> other) {
*
* @since MCUtils 1.0.0
*
* @see #geSecond()
* @see #getSecond()
*/
public boolean isSecondEqual(@Nonnull Pair<?, S> other) {
default boolean isSecondEqual(@Nonnull Pair<?, S> other) {
return getSecond() == null ? other.getSecond() == null : getSecond().equals(other.getSecond());
}

/*
* Java
*/

@Override
public String toString() {
return "Pair[" + getFirst() + ", " + getSecond() + "]";
}

@Override
public boolean equals(Object obj) {
if (obj == null || !(obj instanceof Pair))
return false;
final Pair<?, ?> other = (Pair<?, ?>)obj;
boolean first = getFirst() == null ? other.getFirst() == null : getFirst().equals(other.getFirst());
boolean second = getSecond() == null ? other.getSecond() == null : getSecond().equals(other.getSecond());
return first && second;
}

@Override
public int hashCode() {
return Objects.hash(getFirst(), getSecond());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package net.codersky.mcutils.java.tuple.pair;

import javax.annotation.Nonnull;

import java.util.Objects;

/**
* An extension of {@link ImmutablePair} that implements the
* {@link SafePair} interface, meaning that the elements stored
* by this {@link Pair} cannot be {@code null}.
*
* @author xDec0de_
*
* @param <F> The type of the first element to store.
* @param <S> The type of the second element to store.
*
* @since MCUtils 1.0.0
*
* @see SafeMutablePair
*/
public class SafeImmutablePair<F, S> extends ImmutablePair<F, S> implements SafePair<F, S> {

/**
* Constructs a new {@link SafeImmutablePair} that contains
* two elements which can't be {@code null}.
*
* @param first the first element to store.
* @param second the second element to store.
*
* @throws NullPointerException if either {@code first}
* or {@code second} are {@code null}.
*
* @since MCUtils 1.0.0
*/
public SafeImmutablePair(@Nonnull F first, @Nonnull S second) {
super(Objects.requireNonNull(first), Objects.requireNonNull(second));
}

// Doc override //

/**
* Gets the first element stored in this {@link SafeImmutablePair},
* which will <b>never</b> be {@code null}.
*
* @return The first element stored in this {@link SafeImmutablePair},
* never {@code null}
*
* @since MCUtils 1.0.0
*/
@Nonnull
@Override
@SuppressWarnings("DataFlowIssue")
public F getFirst() {
return super.getFirst();
}

/**
* Gets the second element stored in this {@link SafeImmutablePair},
* which will <b>never</b> be {@code null}.
*
* @return The second element stored in this {@link SafeImmutablePair},
* never {@code null}
*
* @since MCUtils 1.0.0
*/
@Nonnull
@Override
@SuppressWarnings("DataFlowIssue")
public S getSecond() {
return super.getSecond();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package net.codersky.mcutils.java.tuple.pair;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import java.util.Objects;

/**
* An extension of {@link MutablePair} that implements the
* {@link SafePair} interface, meaning that the elements stored
* by this {@link Pair} cannot be {@code null}.
*
* @author xDec0de_
*
* @param <F> The type of the first element to store.
* @param <S> The type of the second element to store.
*
* @since MCUtils 1.0.0
*
* @see SafeImmutablePair
*/
public class SafeMutablePair<F, S> extends MutablePair<F, S> implements SafePair<F, S> {

/**
* Constructs a new {@link SafeMutablePair} that contains
* two elements which can't be {@code null}.
*
* @param first the first element to store.
* @param second the second element to store.
*
* @throws NullPointerException if either {@code first}
* or {@code second} are {@code null}.
*
* @since MCUtils 1.0.0
*/
public SafeMutablePair(@Nonnull F first, @Nonnull S second) {
super(Objects.requireNonNull(first), Objects.requireNonNull(second));
}

// Doc override //

/**
* Gets the first element stored in this {@link SafeMutablePair},
* which will <b>never</b> be {@code null}.
*
* @return The first element stored in this {@link SafeMutablePair},
* never {@code null}
*
* @since MCUtils 1.0.0
*/
@Nonnull
@Override
@SuppressWarnings("DataFlowIssue")
public F getFirst() {
return super.getFirst();
}

/**
* Sets the first element stored on this {@link SafeMutablePair}
* to {@code first}, which can't be {@code null}.
*
* @param first The new value of the first element.
*
* @return {@code first}, for convenience.
*
* @throws NullPointerException if {@code first} is {@code null}.
*
* @since MCUtils 1.0.0
*/
@Nullable
public F setFirst(@Nonnull F first) {
return super.setFirst(Objects.requireNonNull(first));
}

/**
* Gets the second element stored in this {@link SafeMutablePair},
* which will <b>never</b> be {@code null}.
*
* @return The second element stored in this {@link SafeMutablePair},
* never {@code null}
*
* @since MCUtils 1.0.0
*/
@Nonnull
@Override
@SuppressWarnings("DataFlowIssue")
public S getSecond() {
return super.getSecond();
}

/**
* Sets the second element stored on this {@link SafeMutablePair}
* to {@code second}, which can't be {@code null}.
*
* @param second The new value of the second element.
*
* @return {@code second}, for convenience.
*
* @throws NullPointerException if {@code second} is {@code null}.
*
* @since MCUtils 1.0.0
*/
@Nullable
public S setSecond(@Nullable S second) {
return super.setSecond(Objects.requireNonNull(second));
}
}
45 changes: 45 additions & 0 deletions src/main/java/net/codersky/mcutils/java/tuple/pair/SafePair.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package net.codersky.mcutils.java.tuple.pair;

import javax.annotation.Nonnull;

/**
* An extension of the {@link Pair} interface that requires
* stored elements to <b>never</b> be {@code null}.
*
* @author xDec0de_
*
* @param <F> The type of the first element to store.
* @param <S> The type of the second element to store.
*
* @since MCUtils 1.0.0
*
* @see Pair
* @see SafeMutablePair
* @see SafeImmutablePair
*/
public interface SafePair<F, S> extends Pair<F, S> {

/**
* Gets the first element stored in this {@link SafePair},
* which will <b>never</b> be {@code null}.
*
* @return The first element stored in this {@link SafePair},
* never {@code null}
*
* @since MCUtils 1.0.0
*/
@Nonnull
F getFirst();

/**
* Gets the second element stored in this {@link SafePair},
* which will <b>never</b> be {@code null}.
*
* @return The second element stored in this {@link SafePair},
* never {@code null}
*
* @since MCUtils 1.0.0
*/
@Nonnull
S getSecond();
}
Loading

0 comments on commit 96a6ae5

Please sign in to comment.