Skip to content

Commit

Permalink
F ComparableUtils for isLessThan
Browse files Browse the repository at this point in the history
  • Loading branch information
LarsEckart committed May 16, 2022
1 parent e153735 commit 0f19ea2
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.spun.util;

import org.junit.jupiter.api.Test;

import static com.spun.util.ComparableUtils.wrap;
import static org.junit.jupiter.api.Assertions.assertTrue;

class ComparableUtilsTest
{
@Test
void testWrapper()
{
Integer one = 1;
Integer two = 2;
assertTrue(wrap(one).isLessThan(two));
assertTrue(wrap(one).isLessThanOrEqual(two));
assertTrue(wrap(one).isEqualTo(one));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.spun.util;

public class ComparableUtils
{
public static <T extends Comparable<T>> boolean isAEqualToB(T a, T b)
{
return a.compareTo(b) == 0;
}
public static <T extends Comparable<T>> boolean isALessThanOrEqualToB(T a, T b)
{
return a.compareTo(b) <= 0;
}
public static <T extends Comparable<T>> boolean isALessThanB(T a, T b)
{
return a.compareTo(b) < 0;
}
public static <T extends Comparable<T>> ComparableWrapper<T> wrap(T object)
{
return new ComparableWrapper<>(object);
}
public static class ComparableWrapper<T extends Comparable<T>>
{
private final T object;
public ComparableWrapper(T object)
{
this.object = object;
}
public boolean isLessThan(T other)
{
return isALessThanB(object, other);
}
public boolean isEqualTo(T other)
{
return isAEqualToB(object, other);
}
public boolean isLessThanOrEqual(T other)
{
return isALessThanOrEqualToB(object, other);
}
}
}

0 comments on commit 0f19ea2

Please sign in to comment.