diff --git a/crux-jackson/src/main/java/com/fizzed/crux/jackson/CruxUtilModule.java b/crux-jackson/src/main/java/com/fizzed/crux/jackson/CruxUtilModule.java index 74b579a..61371b7 100644 --- a/crux-jackson/src/main/java/com/fizzed/crux/jackson/CruxUtilModule.java +++ b/crux-jackson/src/main/java/com/fizzed/crux/jackson/CruxUtilModule.java @@ -8,9 +8,8 @@ import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.deser.std.StdDeserializer; import com.fasterxml.jackson.databind.ser.std.StdSerializer; -import com.fizzed.crux.util.SecureCode; -import com.fizzed.crux.util.TimeDuration; -import com.fizzed.crux.util.TimeUUID; +import com.fizzed.crux.util.*; + import java.io.IOException; public class CruxUtilModule extends SimpleModule { @@ -80,7 +79,6 @@ public TimeDuration deserialize(JsonParser jp, DeserializationContext ctxt) thro if (value == null || value.isEmpty()) { return null; } - System.out.println("'"+value+"'"); return TimeDuration.parse(value); } }); @@ -95,6 +93,76 @@ public void serialize(TimeDuration value, JsonGenerator jgen, SerializerProvider } } }); + + // + // com.fizzed.crux.util.MutableInteger + // + + this.addDeserializer(MutableInteger.class, new StdDeserializer(MutableInteger.class) { + @Override + public MutableInteger deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + final int value = jp.getIntValue(); + return new MutableInteger(value); + } + }); + + this.addSerializer(MutableInteger.class, new StdSerializer(MutableInteger.class) { + @Override + public void serialize(MutableInteger value, JsonGenerator jgen, SerializerProvider provider) throws IOException { + if (value == null) { + jgen.writeNull(); + } else { + jgen.writeNumber(value.value()); + } + } + }); + + // + // com.fizzed.crux.util.MutableLong + // + + this.addDeserializer(MutableLong.class, new StdDeserializer(MutableLong.class) { + @Override + public MutableLong deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + final long value = jp.getLongValue(); + return new MutableLong(value); + } + }); + + this.addSerializer(MutableLong.class, new StdSerializer(MutableLong.class) { + @Override + public void serialize(MutableLong value, JsonGenerator jgen, SerializerProvider provider) throws IOException { + if (value == null) { + jgen.writeNull(); + } else { + jgen.writeNumber(value.value()); + } + } + }); + + + // + // com.fizzed.crux.util.MutableDouble + // + + this.addDeserializer(MutableDouble.class, new StdDeserializer(MutableDouble.class) { + @Override + public MutableDouble deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { + final double value = jp.getDoubleValue(); + return new MutableDouble(value); + } + }); + + this.addSerializer(MutableDouble.class, new StdSerializer(MutableDouble.class) { + @Override + public void serialize(MutableDouble value, JsonGenerator jgen, SerializerProvider provider) throws IOException { + if (value == null) { + jgen.writeNull(); + } else { + jgen.writeNumber(value.value()); + } + } + }); } } \ No newline at end of file diff --git a/crux-jackson/src/test/java/com/fizzed/crux/jackson/CruxUtilModuleTest.java b/crux-jackson/src/test/java/com/fizzed/crux/jackson/CruxUtilModuleTest.java index 0806a36..6bf48d2 100644 --- a/crux-jackson/src/test/java/com/fizzed/crux/jackson/CruxUtilModuleTest.java +++ b/crux-jackson/src/test/java/com/fizzed/crux/jackson/CruxUtilModuleTest.java @@ -1,12 +1,12 @@ package com.fizzed.crux.jackson; import com.fasterxml.jackson.databind.ObjectMapper; -import com.fizzed.crux.util.SecureCode; -import com.fizzed.crux.util.TimeDuration; -import com.fizzed.crux.util.TimeUUID; +import com.fizzed.crux.util.*; + import java.io.IOException; import java.util.concurrent.TimeUnit; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; import static org.junit.Assert.assertThat; import org.junit.Test; @@ -56,5 +56,68 @@ public void timeDuration() throws IOException { assertThat(objectMapper.readValue(json, TimeDuration.class), is(td)); assertThat(objectMapper.writeValueAsString(td), is(json)); } - + + @Test + public void mutableInteger() throws IOException { + ObjectMapper objectMapper = new ObjectMapper() + .registerModule(new CruxUtilModule()); + + String json; + MutableInteger mi; + + json = "15"; + mi = new MutableInteger(15); + + assertThat(objectMapper.readValue(json, MutableInteger.class), is(mi)); + assertThat(objectMapper.writeValueAsString(mi), is(json)); + + json = "null"; + mi = null; + + assertThat(objectMapper.readValue(json, MutableInteger.class), is(nullValue())); + assertThat(objectMapper.writeValueAsString(mi), is(json)); + } + + @Test + public void mutableLong() throws IOException { + ObjectMapper objectMapper = new ObjectMapper() + .registerModule(new CruxUtilModule()); + + String json; + MutableLong mi; + + json = "15"; + mi = new MutableLong(15); + + assertThat(objectMapper.readValue(json, MutableLong.class), is(mi)); + assertThat(objectMapper.writeValueAsString(mi), is(json)); + + json = "null"; + mi = null; + + assertThat(objectMapper.readValue(json, MutableLong.class), is(nullValue())); + assertThat(objectMapper.writeValueAsString(mi), is(json)); + } + + @Test + public void mutableDouble() throws IOException { + ObjectMapper objectMapper = new ObjectMapper() + .registerModule(new CruxUtilModule()); + + String json; + MutableDouble mi; + + json = "15.2"; + mi = new MutableDouble(15.2); + + assertThat(objectMapper.readValue(json, MutableDouble.class), is(mi)); + assertThat(objectMapper.writeValueAsString(mi), is(json)); + + json = "null"; + mi = null; + + assertThat(objectMapper.readValue(json, MutableDouble.class), is(nullValue())); + assertThat(objectMapper.writeValueAsString(mi), is(json)); + } + } \ No newline at end of file diff --git a/crux-util/src/main/java/com/fizzed/crux/util/MutableDouble.java b/crux-util/src/main/java/com/fizzed/crux/util/MutableDouble.java new file mode 100644 index 0000000..2281000 --- /dev/null +++ b/crux-util/src/main/java/com/fizzed/crux/util/MutableDouble.java @@ -0,0 +1,77 @@ +package com.fizzed.crux.util; + +import java.util.Objects; + +public class MutableDouble implements Comparable { + + private double value; + + public MutableDouble() { + this.value = 0; + } + + public MutableDouble(double value) { + this.value = value; + } + + public double value() { + return value; + } + + public void value(double value) { + this.value = value; + } + + public double increment() { + this.value++; + return this.value; + } + + public double decrement() { + this.value--; + return this.value; + } + + public double add(double value) { + this.value += value; + return this.value; + } + + public double subtract(double value) { + this.value -= value; + return this.value; + } + + public String toString() { + return Double.toString(this.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } + + @Override + public int compareTo(MutableDouble o) { + if (o == null) { + return 1; + } + return Double.compare(this.value, o.value); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + // compare to other mutable integers + if (o instanceof MutableDouble) { + MutableDouble that = (MutableDouble) o; + return value == that.value; + } + if (o instanceof Number) { + Number that = (Number) o; + return value == that.doubleValue(); + } + return false; + } + +} \ No newline at end of file diff --git a/crux-util/src/main/java/com/fizzed/crux/util/MutableInteger.java b/crux-util/src/main/java/com/fizzed/crux/util/MutableInteger.java new file mode 100644 index 0000000..525cfd7 --- /dev/null +++ b/crux-util/src/main/java/com/fizzed/crux/util/MutableInteger.java @@ -0,0 +1,78 @@ +package com.fizzed.crux.util; + +import java.util.Comparator; +import java.util.Objects; + +public class MutableInteger implements Comparable { + + private int value; + + public MutableInteger() { + this.value = 0; + } + + public MutableInteger(int value) { + this.value = value; + } + + public int value() { + return value; + } + + public void value(int value) { + this.value = value; + } + + public int increment() { + this.value++; + return this.value; + } + + public int decrement() { + this.value--; + return this.value; + } + + public int add(int value) { + this.value += value; + return this.value; + } + + public int subtract(int value) { + this.value -= value; + return this.value; + } + + public String toString() { + return Integer.toString(this.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } + + @Override + public int compareTo(MutableInteger o) { + if (o == null) { + return 1; + } + return Integer.compare(this.value, o.value); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + // compare to other mutable integers + if (o instanceof MutableInteger) { + MutableInteger that = (MutableInteger) o; + return value == that.value; + } + if (o instanceof Number) { + Number that = (Number) o; + return value == that.intValue(); + } + return false; + } + +} \ No newline at end of file diff --git a/crux-util/src/main/java/com/fizzed/crux/util/MutableLong.java b/crux-util/src/main/java/com/fizzed/crux/util/MutableLong.java new file mode 100644 index 0000000..d0de8bb --- /dev/null +++ b/crux-util/src/main/java/com/fizzed/crux/util/MutableLong.java @@ -0,0 +1,77 @@ +package com.fizzed.crux.util; + +import java.util.Objects; + +public class MutableLong implements Comparable { + + private long value; + + public MutableLong() { + this.value = 0; + } + + public MutableLong(long value) { + this.value = value; + } + + public long value() { + return value; + } + + public void value(int value) { + this.value = value; + } + + public long increment() { + this.value++; + return this.value; + } + + public long decrement() { + this.value--; + return this.value; + } + + public long add(long value) { + this.value += value; + return this.value; + } + + public long subtract(long value) { + this.value -= value; + return this.value; + } + + public String toString() { + return Long.toString(this.value); + } + + @Override + public int hashCode() { + return Objects.hash(value); + } + + @Override + public int compareTo(MutableLong o) { + if (o == null) { + return 1; + } + return Long.compare(this.value, o.value); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + // compare to other mutable integers + if (o instanceof MutableLong) { + MutableLong that = (MutableLong)o; + return value == that.value; + } + if (o instanceof Number) { + Number that = (Number) o; + return value == that.longValue(); + } + return false; + } + +} \ No newline at end of file diff --git a/crux-util/src/test/java/com/fizzed/crux/util/MutableDoubleTest.java b/crux-util/src/test/java/com/fizzed/crux/util/MutableDoubleTest.java new file mode 100644 index 0000000..c686ba3 --- /dev/null +++ b/crux-util/src/test/java/com/fizzed/crux/util/MutableDoubleTest.java @@ -0,0 +1,66 @@ +package com.fizzed.crux.util; + +import org.junit.Test; + +import java.util.TreeSet; + +import static java.util.Arrays.asList; +import static java.util.stream.Collectors.toList; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.assertThat; + +public class MutableDoubleTest { + + @Test + public void ops() { + MutableDouble mi = new MutableDouble(); + + assertThat(mi.value(), is(0d)); + + mi.increment(); + + assertThat(mi.value(), is(1d)); + + mi.decrement(); + + assertThat(mi.value(), is(0d)); + } + + @Test + public void testEquals() { + MutableDouble mi = new MutableDouble(15d); + + assertThat(mi, is(15d)); + assertThat(mi, is(15)); + assertThat(mi, is(15L)); + assertThat(mi, is((byte)15)); + assertThat(mi, is(not(16d))); + assertThat(mi, is(new MutableDouble(15))); + assertThat(mi, is(not(new MutableDouble(16)))); + } + + @Test + public void compareTo() { + MutableDouble m1 = new MutableDouble(15); + MutableDouble m2 = new MutableDouble(16); + + assertThat(m1.compareTo(m2), is(-1)); + assertThat(m1.compareTo(null), is(1)); + assertThat(m2.compareTo(m1), is(1)); + assertThat(m2.compareTo(m2), is(0)); + } + + @Test + public void treeSet() { + TreeSet mints = new TreeSet<>(); + + mints.add(new MutableDouble(11)); + mints.add(new MutableDouble(9)); + mints.add(new MutableDouble(9)); // should be de-duped + mints.add(new MutableDouble(10)); + + assertThat(mints.stream().collect(toList()), is(asList(new MutableDouble(9), new MutableDouble(10), new MutableDouble(11)))); + } + +} \ No newline at end of file diff --git a/crux-util/src/test/java/com/fizzed/crux/util/MutableIntegerTest.java b/crux-util/src/test/java/com/fizzed/crux/util/MutableIntegerTest.java new file mode 100644 index 0000000..7b52cf4 --- /dev/null +++ b/crux-util/src/test/java/com/fizzed/crux/util/MutableIntegerTest.java @@ -0,0 +1,67 @@ +package com.fizzed.crux.util; + +import org.junit.Test; + +import java.util.TreeSet; +import java.util.stream.Collectors; + +import static java.util.Arrays.asList; +import static java.util.stream.Collectors.toList; +import static org.hamcrest.CoreMatchers.hasItems; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.*; + +public class MutableIntegerTest { + + @Test + public void ops() { + MutableInteger mi = new MutableInteger(); + + assertThat(mi.value(), is(0)); + + mi.increment(); + + assertThat(mi.value(), is(1)); + + mi.decrement(); + + assertThat(mi.value(), is(0)); + } + + @Test + public void testEquals() { + MutableInteger mi = new MutableInteger(15); + + assertThat(mi, is(15)); + assertThat(mi, is(15L)); + assertThat(mi, is((byte)15)); + assertThat(mi, is(not(16))); + assertThat(mi, is(new MutableInteger(15))); + assertThat(mi, is(not(new MutableInteger(16)))); + } + + @Test + public void compareTo() { + MutableInteger m1 = new MutableInteger(15); + MutableInteger m2 = new MutableInteger(16); + + assertThat(m1.compareTo(m2), is(-1)); + assertThat(m1.compareTo(null), is(1)); + assertThat(m2.compareTo(m1), is(1)); + assertThat(m2.compareTo(m2), is(0)); + } + + @Test + public void treeSet() { + TreeSet mints = new TreeSet<>(); + + mints.add(new MutableInteger(11)); + mints.add(new MutableInteger(9)); + mints.add(new MutableInteger(9)); // should be de-duped + mints.add(new MutableInteger(10)); + + assertThat(mints.stream().collect(toList()), is(asList(new MutableInteger(9), new MutableInteger(10), new MutableInteger(11)))); + } + +} \ No newline at end of file diff --git a/crux-util/src/test/java/com/fizzed/crux/util/MutableLongTest.java b/crux-util/src/test/java/com/fizzed/crux/util/MutableLongTest.java new file mode 100644 index 0000000..0a4ba6d --- /dev/null +++ b/crux-util/src/test/java/com/fizzed/crux/util/MutableLongTest.java @@ -0,0 +1,67 @@ +package com.fizzed.crux.util; + +import org.junit.Test; + +import java.util.TreeSet; + +import static java.util.Arrays.asList; +import static java.util.stream.Collectors.toList; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.Matchers.not; +import static org.junit.Assert.*; + +public class MutableLongTest { + + @Test + public void ops() { + MutableLong mi = new MutableLong(); + + assertThat(mi.value(), is(0L)); + + mi.increment(); + + assertThat(mi.value(), is(1L)); + + mi.decrement(); + + assertThat(mi.value(), is(0L)); + } + + @Test + public void testEquals() { + MutableLong mi = new MutableLong(15); + + assertThat(mi, is(15)); + assertThat(mi, is(15L)); + assertThat(mi, is((byte)15)); + assertThat(mi, is((short)15)); + assertThat(mi, is((double)15)); + assertThat(mi, is(not(16L))); + assertThat(mi, is(new MutableLong(15L))); + assertThat(mi, is(not(new MutableLong(16L)))); + } + + @Test + public void compareTo() { + MutableLong m1 = new MutableLong(15); + MutableLong m2 = new MutableLong(16); + + assertThat(m1.compareTo(m2), is(-1)); + assertThat(m1.compareTo(null), is(1)); + assertThat(m2.compareTo(m1), is(1)); + assertThat(m2.compareTo(m2), is(0)); + } + + @Test + public void treeSet() { + TreeSet mints = new TreeSet<>(); + + mints.add(new MutableLong(11)); + mints.add(new MutableLong(9)); + mints.add(new MutableLong(9)); // should be de-duped + mints.add(new MutableLong(10)); + + assertThat(mints.stream().collect(toList()), is(asList(new MutableLong(9), new MutableLong(10), new MutableLong(11)))); + } + +} \ No newline at end of file