Skip to content

Commit

Permalink
Generify the Comparable type in the Range class #478
Browse files Browse the repository at this point in the history
  • Loading branch information
anders-steen-dige-madsen authored and vladmihalcea committed Sep 5, 2022
1 parent c6ab395 commit 355c353
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 149 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author Edgar Asatryan
* @author Vlad Mihalcea
*/
public final class Range<T extends Comparable> implements Serializable {
public final class Range<T extends Comparable<? super T>> implements Serializable {

public static final int LOWER_INCLUSIVE = 1 << 1;
public static final int LOWER_EXCLUSIVE = 1 << 2;
Expand Down Expand Up @@ -67,7 +67,7 @@ private Range(T lower, T upper, int mask, Class<T> clazz) {
* @return The closed range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> closed(T lower, T upper) {
public static <T extends Comparable<? super T>> Range<T> closed(T lower, T upper) {
Objects.requireNonNull(lower);
Objects.requireNonNull(upper);
return new Range<T>(lower, upper, LOWER_INCLUSIVE | UPPER_INCLUSIVE, (Class<T>) lower.getClass());
Expand All @@ -88,7 +88,7 @@ public static <T extends Comparable<?>> Range<T> closed(T lower, T upper) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> open(T lower, T upper) {
public static <T extends Comparable<? super T>> Range<T> open(T lower, T upper) {
Objects.requireNonNull(lower);
Objects.requireNonNull(upper);
return new Range<T>(lower, upper, LOWER_EXCLUSIVE | UPPER_EXCLUSIVE, (Class<T>) lower.getClass());
Expand All @@ -109,7 +109,7 @@ public static <T extends Comparable<?>> Range<T> open(T lower, T upper) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> openClosed(T lower, T upper) {
public static <T extends Comparable<? super T>> Range<T> openClosed(T lower, T upper) {
Objects.requireNonNull(lower);
Objects.requireNonNull(upper);
return new Range<T>(lower, upper, LOWER_EXCLUSIVE | UPPER_INCLUSIVE, (Class<T>) lower.getClass());
Expand All @@ -130,7 +130,7 @@ public static <T extends Comparable<?>> Range<T> openClosed(T lower, T upper) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> closedOpen(T lower, T upper) {
public static <T extends Comparable<? super T>> Range<T> closedOpen(T lower, T upper) {
Objects.requireNonNull(lower);
Objects.requireNonNull(upper);
return new Range<T>(lower, upper, LOWER_INCLUSIVE | UPPER_EXCLUSIVE, (Class<T>) lower.getClass());
Expand All @@ -150,7 +150,7 @@ public static <T extends Comparable<?>> Range<T> closedOpen(T lower, T upper) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> openInfinite(T lower) {
public static <T extends Comparable<? super T>> Range<T> openInfinite(T lower) {
Objects.requireNonNull(lower);
return new Range<T>(lower, null, LOWER_EXCLUSIVE | UPPER_INFINITE, (Class<T>) lower.getClass());
}
Expand All @@ -169,7 +169,7 @@ public static <T extends Comparable<?>> Range<T> openInfinite(T lower) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> closedInfinite(T lower) {
public static <T extends Comparable<? super T>> Range<T> closedInfinite(T lower) {
Objects.requireNonNull(lower);
return new Range(lower, null, LOWER_INCLUSIVE | UPPER_INFINITE, lower.getClass());
}
Expand All @@ -188,7 +188,7 @@ public static <T extends Comparable<?>> Range<T> closedInfinite(T lower) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> infiniteOpen(T upper) {
public static <T extends Comparable<? super T>> Range<T> infiniteOpen(T upper) {
Objects.requireNonNull(upper);
return new Range<T>(null, upper, UPPER_EXCLUSIVE | LOWER_INFINITE, (Class<T>) upper.getClass());
}
Expand All @@ -207,7 +207,7 @@ public static <T extends Comparable<?>> Range<T> infiniteOpen(T upper) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> infiniteClosed(T upper) {
public static <T extends Comparable<? super T>> Range<T> infiniteClosed(T upper) {
Objects.requireNonNull(upper);
return new Range<T>(null, upper, UPPER_INCLUSIVE | LOWER_INFINITE, (Class<T>) upper.getClass());
}
Expand All @@ -226,12 +226,12 @@ public static <T extends Comparable<?>> Range<T> infiniteClosed(T upper) {
* @return The infinite range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> infinite(Class<T> cls) {
public static <T extends Comparable<? super T>> Range<T> infinite(Class<T> cls) {
return new Range<T>(null, null, LOWER_INFINITE | UPPER_INFINITE, cls);
}

@SuppressWarnings("unchecked")
public static <T extends Comparable> Range<T> ofString(String str, Function<String, T> converter, Class<T> clazz) {
public static <T extends Comparable<? super T>> Range<T> ofString(String str, Function<String, T> converter, Class<T> clazz) {
if(str.equals(EMPTY)) {
return emptyRange(clazz);
}
Expand Down Expand Up @@ -514,7 +514,7 @@ public interface Function<T, R> {
R apply(T t);
}

public static <R extends Comparable<R>> Range<R> emptyRange(Class<R> clazz) {
public static <R extends Comparable<? super R>> Range<R> emptyRange(Class<R> clazz) {
return new Range<R>(
null,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author Edgar Asatryan
* @author Vlad Mihalcea
*/
public final class Range<T extends Comparable> implements Serializable {
public final class Range<T extends Comparable<? super T>> implements Serializable {

public static final int LOWER_INCLUSIVE = 1 << 1;
public static final int LOWER_EXCLUSIVE = 1 << 2;
Expand Down Expand Up @@ -67,7 +67,7 @@ private Range(T lower, T upper, int mask, Class<T> clazz) {
* @return The closed range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> closed(T lower, T upper) {
public static <T extends Comparable<? super T>> Range<T> closed(T lower, T upper) {
Objects.requireNonNull(lower);
Objects.requireNonNull(upper);
return new Range<T>(lower, upper, LOWER_INCLUSIVE | UPPER_INCLUSIVE, (Class<T>) lower.getClass());
Expand All @@ -88,7 +88,7 @@ public static <T extends Comparable<?>> Range<T> closed(T lower, T upper) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> open(T lower, T upper) {
public static <T extends Comparable<? super T>> Range<T> open(T lower, T upper) {
Objects.requireNonNull(lower);
Objects.requireNonNull(upper);
return new Range<T>(lower, upper, LOWER_EXCLUSIVE | UPPER_EXCLUSIVE, (Class<T>) lower.getClass());
Expand All @@ -109,7 +109,7 @@ public static <T extends Comparable<?>> Range<T> open(T lower, T upper) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> openClosed(T lower, T upper) {
public static <T extends Comparable<? super T>> Range<T> openClosed(T lower, T upper) {
Objects.requireNonNull(lower);
Objects.requireNonNull(upper);
return new Range<T>(lower, upper, LOWER_EXCLUSIVE | UPPER_INCLUSIVE, (Class<T>) lower.getClass());
Expand All @@ -130,7 +130,7 @@ public static <T extends Comparable<?>> Range<T> openClosed(T lower, T upper) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> closedOpen(T lower, T upper) {
public static <T extends Comparable<? super T>> Range<T> closedOpen(T lower, T upper) {
Objects.requireNonNull(lower);
Objects.requireNonNull(upper);
return new Range<T>(lower, upper, LOWER_INCLUSIVE | UPPER_EXCLUSIVE, (Class<T>) lower.getClass());
Expand All @@ -150,7 +150,7 @@ public static <T extends Comparable<?>> Range<T> closedOpen(T lower, T upper) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> openInfinite(T lower) {
public static <T extends Comparable<? super T>> Range<T> openInfinite(T lower) {
Objects.requireNonNull(lower);
return new Range<T>(lower, null, LOWER_EXCLUSIVE | UPPER_INFINITE, (Class<T>) lower.getClass());
}
Expand All @@ -169,7 +169,7 @@ public static <T extends Comparable<?>> Range<T> openInfinite(T lower) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> closedInfinite(T lower) {
public static <T extends Comparable<? super T>> Range<T> closedInfinite(T lower) {
Objects.requireNonNull(lower);
return new Range(lower, null, LOWER_INCLUSIVE | UPPER_INFINITE, lower.getClass());
}
Expand All @@ -188,7 +188,7 @@ public static <T extends Comparable<?>> Range<T> closedInfinite(T lower) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> infiniteOpen(T upper) {
public static <T extends Comparable<? super T>> Range<T> infiniteOpen(T upper) {
Objects.requireNonNull(upper);
return new Range<T>(null, upper, UPPER_EXCLUSIVE | LOWER_INFINITE, (Class<T>) upper.getClass());
}
Expand All @@ -207,7 +207,7 @@ public static <T extends Comparable<?>> Range<T> infiniteOpen(T upper) {
* @return The range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> infiniteClosed(T upper) {
public static <T extends Comparable<? super T>> Range<T> infiniteClosed(T upper) {
Objects.requireNonNull(upper);
return new Range<T>(null, upper, UPPER_INCLUSIVE | LOWER_INFINITE, (Class<T>) upper.getClass());
}
Expand All @@ -226,12 +226,12 @@ public static <T extends Comparable<?>> Range<T> infiniteClosed(T upper) {
* @return The infinite range.
*/
@SuppressWarnings("unchecked")
public static <T extends Comparable<?>> Range<T> infinite(Class<T> cls) {
public static <T extends Comparable<? super T>> Range<T> infinite(Class<T> cls) {
return new Range<T>(null, null, LOWER_INFINITE | UPPER_INFINITE, cls);
}

@SuppressWarnings("unchecked")
public static <T extends Comparable> Range<T> ofString(String str, Function<String, T> converter, Class<T> clazz) {
public static <T extends Comparable<? super T>> Range<T> ofString(String str, Function<String, T> converter, Class<T> clazz) {
if(str.equals(EMPTY)) {
return emptyRange(clazz);
}
Expand Down Expand Up @@ -367,9 +367,9 @@ public boolean equals(Object o) {
if (!(o instanceof Range)) return false;
Range<?> range = (Range<?>) o;
return mask == range.mask &&
Objects.equals(lower, range.lower) &&
Objects.equals(upper, range.upper) &&
Objects.equals(clazz, range.clazz);
Objects.equals(lower, range.lower) &&
Objects.equals(upper, range.upper) &&
Objects.equals(clazz, range.clazz);
}

@Override
Expand All @@ -380,22 +380,22 @@ public int hashCode() {
@Override
public String toString() {
return "Range{" + "lower=" + lower +
", upper=" + upper +
", mask=" + mask +
", clazz=" + clazz +
'}';
", upper=" + upper +
", mask=" + mask +
", clazz=" + clazz +
'}';
}

public boolean hasMask(int flag) {
return (mask & flag) == flag;
}

public boolean isLowerBoundClosed() {
return hasLowerBound() && hasMask(LOWER_INCLUSIVE);
return hasLowerBound() && hasMask(LOWER_INCLUSIVE);
}

public boolean isUpperBoundClosed() {
return hasUpperBound() && hasMask(UPPER_INCLUSIVE);
return hasUpperBound() && hasMask(UPPER_INCLUSIVE);
}

public boolean hasLowerBound() {
Expand Down Expand Up @@ -488,10 +488,10 @@ public String asString() {
StringBuilder sb = new StringBuilder();

sb.append(hasMask(LOWER_INCLUSIVE) ? '[' : '(')
.append(hasLowerBound() ? boundToString().apply(lower) : "")
.append(",")
.append(hasUpperBound() ? boundToString().apply(upper) : "")
.append(hasMask(UPPER_INCLUSIVE) ? ']' : ')');
.append(hasLowerBound() ? boundToString().apply(lower) : "")
.append(",")
.append(hasUpperBound() ? boundToString().apply(upper) : "")
.append(hasMask(UPPER_INCLUSIVE) ? ']' : ')');

return sb.toString();
}
Expand All @@ -510,11 +510,11 @@ Class<T> getClazz() {
}

public interface Function<T, R> {

R apply(T t);
}

public static <R extends Comparable<R>> Range<R> emptyRange(Class<R> clazz) {
public static <R extends Comparable<? super R>> Range<R> emptyRange(Class<R> clazz) {
return new Range<R>(
null,
null,
Expand Down
Loading

0 comments on commit 355c353

Please sign in to comment.