Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve VTLObject tests and coverage
  • Loading branch information
hadrienk committed Dec 18, 2018
1 parent f0ca451 commit 0ee8073
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 35 deletions.
12 changes: 11 additions & 1 deletion java-vtl-model/src/main/java/no/ssb/vtl/model/VTLNumber.java
Expand Up @@ -30,7 +30,17 @@ public abstract class VTLNumber<T extends Number> extends VTLObject<T> {

@Override
public abstract T get();


@Override
public int compareTo(Object o) {
if (o instanceof VTLNumber) {
VTLNumber that = ((VTLNumber) o);
return Double.compare(this.get().doubleValue(), that.get().doubleValue());
} else {
return super.compareTo(o);
}
}

public VTLNumber add(VTLNumber addend) {
return add(addend.get());
}
Expand Down
35 changes: 10 additions & 25 deletions java-vtl-model/src/main/java/no/ssb/vtl/model/VTLObject.java
Expand Up @@ -21,6 +21,7 @@
*/

import java.time.Instant;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.Supplier;

Expand All @@ -29,6 +30,11 @@
*/
public abstract class VTLObject<V> implements Supplier<V>, Comparable<Object> {

@SuppressWarnings("unchecked")
public static final Comparator<Comparable> NULLS_FIRST = Comparator.<Comparable>nullsFirst(Comparator.naturalOrder());
@SuppressWarnings("unchecked")
public static final Comparator<VTLObject> VTL_OBJECT_COMPARATOR = Comparator.comparing(vtlObject -> (Comparable) vtlObject.get(), NULLS_FIRST);

/**
* This method is marked as deprecated (Hadrien, 02-05-2017).
*
Expand Down Expand Up @@ -144,31 +150,10 @@ public String toString() {
*/
@Override
public int compareTo(Object o) {
Object value = this.get();
Object other;
if (o instanceof VTLObject) {
other = ((VTLObject) o).get();
} else {
other = o;
}

if (value == null) {
return (other == null) ? 0 : -1;
} else if (other == null) {
return 1;
if (!(o instanceof VTLObject)) {
throw new ClassCastException("could not cast to VTLObject");
}

// Compare numbers
if (Number.class.isAssignableFrom(other.getClass()) && Number.class.isAssignableFrom(value.getClass()))
return Double.compare(((Number) value).doubleValue(), ((Number) other).doubleValue());

// Compare comparable.
if (other.getClass() == value.getClass() && value instanceof Comparable)
return ((Comparable) value).compareTo(other);

throw new IllegalArgumentException(
String.format("Cannot compare %s of type %s with %s of type %s",
value, value.getClass(), other, other.getClass()));
return VTL_OBJECT_COMPARATOR.compare(this, (VTLObject) o);
}

@Override
Expand All @@ -179,7 +164,7 @@ public int hashCode() {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || !(o instanceof VTLObject)) return false;
if (!(o instanceof VTLObject)) return false;
VTLObject<?> value = (VTLObject<?>) o;
return Objects.equals(get(), value.get());
}
Expand Down
4 changes: 1 addition & 3 deletions java-vtl-model/src/main/java/no/ssb/vtl/model/VTLString.java
Expand Up @@ -33,11 +33,9 @@ private VTLString() {

public static VTLString of(String string) {
return new VTLString() {
/**
* Returns the value of the data point.
*/
@Override
public String get() {
// TODO: This is hot code. We should rather check in the comparator.
return "".equals(string) ? null : string;
}
};
Expand Down
Expand Up @@ -41,11 +41,6 @@
*/
public final class VtlOrdering implements Ordering, OrderingSpecification {

@SuppressWarnings("unchecked")
public static final Comparator<Comparable> NULLS_FIRST = Comparator.<Comparable>nullsFirst(Comparator.naturalOrder());
@SuppressWarnings("unchecked")
public static final Comparator<VTLObject> VTL_OBJECT_COMPARATOR = Comparator.comparing(vtlObject -> (Comparable) vtlObject.get(), NULLS_FIRST);

public static final Comparator<Map.Entry<String, Component>> BY_ROLE = Comparator.comparing(
entry -> entry.getValue().getRole(),
com.google.common.collect.Ordering.explicit(
Expand Down
55 changes: 55 additions & 0 deletions java-vtl-model/src/test/java/no/ssb/vtl/model/VTLObjectTest.java
Expand Up @@ -21,12 +21,15 @@
*/

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.assertj.core.api.AutoCloseableSoftAssertions;
import org.assertj.core.api.SoftAssertions;
import org.junit.Test;

import java.time.Instant;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.assertj.core.api.Java6Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -54,6 +57,8 @@ public void testGenericFactory() {
assertThat(VTLObject.of((Object) null)).isSameAs(VTLObject.NULL);

assertThat(VTLObject.of(VTLObject.NULL)).isSameAs(VTLObject.NULL);
assertThat(VTLObject.NULL.get()).isNull();
assertThat(VTLObject.NULL.toString()).isEqualTo("[NULL]");

assertThatThrownBy(() -> VTLObject.of(new HashSet<>()))
.isInstanceOf(IllegalArgumentException.class)
Expand All @@ -79,6 +84,56 @@ public void testString() {
assertThat(aFloat.getVTLType()).isEqualTo(VTLString.class);
}

@Test
public void testEquals() {
VTLInteger vtlInteger = VTLObject.of(1L);
assertThat(vtlInteger.equals(vtlInteger)).isTrue();
assertThat(vtlInteger.equals(VTLObject.of(1L))).isTrue();
assertThat(vtlInteger.equals(VTLObject.of(2L))).isFalse();
assertThat(vtlInteger.equals(null)).isFalse();
assertThat(vtlInteger.equals(new Object())).isFalse();
}

@Test
public void testCompare() {
SoftAssertions softly = new SoftAssertions();
Instant now = Instant.now();
Set<Object> values = ImmutableSet.builder().add("string",1.0D, 1.0F, 1L, 1, now, false).build();
Map<Object, Object> greater = ImmutableMap.builder()
.put("string", "zstring")
.put(1.0D, 2.0D)
.put(1.0F, 2.0F)
.put(1L, 2L)
.put(1, 2)
.put(now, now.plusSeconds(60))
.put(false, true)
.build();
Map<Object, Object> less = ImmutableMap.builder()
.put("string", "astring")
.put(1.0D, 0.5D)
.put(1.0F, 0.5F)
.put(1L, 0L)
.put(1, 0)
.put(now, now.minusSeconds(60))
.build();

for (Object value : values) {
VTLObject vtlObject = VTLObject.of(value);
if (greater.containsKey(value)) {
VTLObject greaterVtlObject = VTLObject.of(greater.get(value));
softly.assertThat(vtlObject.compareTo(greaterVtlObject)).isLessThan(0);
softly.assertThat(greaterVtlObject.compareTo(vtlObject)).isGreaterThan(0);
}
if (less.containsKey(value)) {
VTLObject lesserVtlObject = VTLObject.of(less.get(value));
softly.assertThat(vtlObject.compareTo(lesserVtlObject)).isGreaterThan(0);
softly.assertThat(lesserVtlObject.compareTo(vtlObject)).isLessThan(0);
}
}

softly.assertAll();
}

@Test
public void testIntegerCompare() throws Exception {

Expand Down
Expand Up @@ -26,7 +26,7 @@
import java.util.Comparator;
import java.util.Map;

import static no.ssb.vtl.model.VtlOrdering.VTL_OBJECT_COMPARATOR;
import static no.ssb.vtl.model.VTLObject.VTL_OBJECT_COMPARATOR;
import static no.ssb.vtl.model.Ordering.Direction.ASC;

public class DataPointMapComparator implements Comparator<DataPointMap.View> {
Expand Down

0 comments on commit 0ee8073

Please sign in to comment.