Skip to content

Commit

Permalink
Add support for mutable ints, longs, and doubles
Browse files Browse the repository at this point in the history
  • Loading branch information
jjlauer committed Sep 28, 2023
1 parent b181287 commit 2472218
Show file tree
Hide file tree
Showing 8 changed files with 571 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
}
});
Expand All @@ -95,6 +93,76 @@ public void serialize(TimeDuration value, JsonGenerator jgen, SerializerProvider
}
}
});

//
// com.fizzed.crux.util.MutableInteger
//

this.addDeserializer(MutableInteger.class, new StdDeserializer<MutableInteger>(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>(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>(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>(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>(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>(MutableDouble.class) {
@Override
public void serialize(MutableDouble value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
if (value == null) {
jgen.writeNull();
} else {
jgen.writeNumber(value.value());
}
}
});
}

}
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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));
}

}
77 changes: 77 additions & 0 deletions crux-util/src/main/java/com/fizzed/crux/util/MutableDouble.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package com.fizzed.crux.util;

import java.util.Objects;

public class MutableDouble implements Comparable<MutableDouble> {

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;
}

}
78 changes: 78 additions & 0 deletions crux-util/src/main/java/com/fizzed/crux/util/MutableInteger.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package com.fizzed.crux.util;

import java.util.Comparator;
import java.util.Objects;

public class MutableInteger implements Comparable<MutableInteger> {

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;
}

}
Loading

0 comments on commit 2472218

Please sign in to comment.