Skip to content

Commit

Permalink
Add support for double arrays vladmihalcea#183
Browse files Browse the repository at this point in the history
  • Loading branch information
wlievens authored and vladmihalcea committed Feb 20, 2020
1 parent c057437 commit e74b49d
Show file tree
Hide file tree
Showing 24 changed files with 393 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.vladmihalcea.hibernate.type.array;

import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayType;
import com.vladmihalcea.hibernate.type.array.internal.DoubleArrayTypeDescriptor;
import com.vladmihalcea.hibernate.type.util.Configuration;

/**
* Maps an {@code double[]} array on a PostgreSQL ARRAY type. Multidimensional arrays are supported as well, as explained in <a href="https://vladmihalcea.com/multidimensional-array-jpa-hibernate/">this article</a>.
* <p>
* For more details about how to use it, check out <a href=
* "https://vladmihalcea.com/how-to-map-java-and-sql-arrays-with-jpa-and-hibernate/">this
* article</a> on <a href="https://vladmihalcea.com/">vladmihalcea.com</a>.
*
* @author Vlad Mihalcea
*/
public class DoubleArrayType extends AbstractArrayType<double[]> {

public static final DoubleArrayType INSTANCE = new DoubleArrayType();

public DoubleArrayType() {
super(
new DoubleArrayTypeDescriptor()
);
}

public DoubleArrayType(Configuration configuration) {
super(
new DoubleArrayTypeDescriptor(),
configuration
);
}

@Override
public String getName() {
return "double-array";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.vladmihalcea.hibernate.type.array.internal;

/**
* @author Vlad Mihalcea
*/
public class DoubleArrayTypeDescriptor
extends AbstractArrayTypeDescriptor<double[]> {

public DoubleArrayTypeDescriptor() {
super(double[].class);
}

@Override
protected String getSqlArrayType() {
return "float8";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public void setParameterValues(Properties parameters) {
sqlArrayType = "integer";
} else if (Long.class.isAssignableFrom(arrayElementClass)) {
sqlArrayType = "bigint";
} else if (Double.class.isAssignableFrom(arrayElementClass)) {
sqlArrayType = "float8";
} else if (String.class.isAssignableFrom(arrayElementClass)) {
sqlArrayType = "text";
} else if (UUID.class.isAssignableFrom(arrayElementClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;

/**
* @author Vlad Mihalcea
Expand Down Expand Up @@ -116,6 +112,7 @@ public Void apply(EntityManager entityManager) {
event.setSensorNames(new String[]{"Temperature", "Pressure"});
event.setSensorValues(new int[]{12, 756});
event.setSensorLongValues(new long[]{42L, 9223372036854775800L});
event.setSensorDoubleValues(new double[]{0.123, 456.789});
event.setSensorStates(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN});
event.setDateValues(new Date[]{date1, date2});
event.setTimestampValues(new Date[]{date1, date2});
Expand All @@ -135,6 +132,7 @@ public Void apply(EntityManager entityManager) {
assertArrayEquals(new String[]{"Temperature", "Pressure"}, event.getSensorNames());
assertArrayEquals(new int[]{12, 756}, event.getSensorValues());
assertArrayEquals(new long[]{42L, 9223372036854775800L}, event.getSensorLongValues());
assertArrayEquals(new double[]{0.123, 456.789}, event.getSensorDoubleValues(), 0.01);
assertArrayEquals(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}, event.getSensorStates());
assertEquals(date1.getTime(), event.getDateValues()[0].getTime());
assertEquals(date2.getTime(), event.getDateValues()[1].getTime());
Expand Down Expand Up @@ -203,6 +201,10 @@ public static class Event extends BaseEntity {
@Column(name = "sensor_long_values", columnDefinition = "bigint[]")
private long[] sensorLongValues;

@Type(type = "double-array")
@Column(name = "sensor_double_values", columnDefinition = "float8[]")
private double[] sensorDoubleValues;

@Type(type = "date-array")
@Column(name = "date_values", columnDefinition = "date[]")
private Date[] dateValues;
Expand Down Expand Up @@ -247,6 +249,14 @@ public void setSensorLongValues(long[] sensorLongValues) {
this.sensorLongValues = sensorLongValues;
}

public double[] getSensorDoubleValues() {
return sensorDoubleValues;
}

public void setSensorDoubleValues(double[] sensorDoubleValues) {
this.sensorDoubleValues = sensorDoubleValues;
}

public SensorState[] getSensorStates() {
return sensorStates;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public Void apply(EntityManager entityManager) {
event.setSensorNames(Arrays.asList("Temperature", "Pressure"));
event.setSensorValues(Arrays.asList(12, 756));
event.setSensorLongValues(Arrays.asList(42L, 9223372036854775800L));
event.setSensorDoubleValues(Arrays.asList(0.123D, 456.789D));
event.setSensorStates(Arrays.asList(SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN));
event.setDateValues(Arrays.asList(date1, date2));
event.setTimestampValues(Arrays.asList(date1, date2));
Expand All @@ -130,6 +131,10 @@ public Void apply(EntityManager entityManager) {
assertArrayEquals(new String[]{"Temperature", "Pressure"}, event.getSensorNames().toArray());
assertArrayEquals(new Integer[]{12, 756}, event.getSensorValues().toArray());
assertArrayEquals(new Long[]{42L, 9223372036854775800L}, event.getSensorLongValues().toArray());
assertEquals(
Arrays.asList(0.123D, 456.789D),
event.getSensorDoubleValues()
);
assertArrayEquals(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}, event.getSensorStates().toArray());
assertEquals(date1.getTime(), event.getDateValues().get(0).getTime());
assertEquals(date2.getTime(), event.getDateValues().get(1).getTime());
Expand Down Expand Up @@ -184,6 +189,7 @@ public Void apply(EntityManager entityManager) {
event.setSensorNames(Arrays.asList("Temperature", null));
event.setSensorValues(Arrays.asList(null, 756));
event.setSensorLongValues(Arrays.asList(null, 9223372036854775800L));
event.setSensorDoubleValues(Arrays.asList(null, 456.789D));
event.setSensorStates(Arrays.asList(null, SensorState.OFFLINE, SensorState.ONLINE, null));
event.setDateValues(Arrays.asList(null, date));
event.setTimestampValues(Arrays.asList(null, date));
Expand All @@ -202,6 +208,7 @@ public Void apply(EntityManager entityManager) {
assertArrayEquals(new String[]{"Temperature", null}, event.getSensorNames().toArray());
assertArrayEquals(new Integer[]{null, 756}, event.getSensorValues().toArray());
assertArrayEquals(new Long[]{null, 9223372036854775800L}, event.getSensorLongValues().toArray());
assertArrayEquals(new Double[]{null, 456.789D}, event.getSensorDoubleValues().toArray());
assertArrayEquals(new SensorState[]{null, SensorState.OFFLINE, SensorState.ONLINE, null}, event.getSensorStates().toArray());

assertNull(event.getDateValues().get(0));
Expand Down Expand Up @@ -234,6 +241,7 @@ public Void apply(EntityManager entityManager) {
event.setSensorNames(Arrays.<String>asList(null, null));
event.setSensorValues(Arrays.<Integer>asList(null, null));
event.setSensorLongValues(Arrays.<Long>asList(null, null));
event.setSensorDoubleValues(Arrays.<Double>asList(null, null));
event.setSensorStates(Arrays.<SensorState>asList(null, null));
event.setDateValues(Arrays.<Date>asList(null, null));
event.setTimestampValues(Arrays.<Date>asList(null, null));
Expand All @@ -252,6 +260,7 @@ public Void apply(EntityManager entityManager) {
assertArrayEquals(new String[]{null, null}, event.getSensorNames().toArray());
assertArrayEquals(new Integer[]{null, null}, event.getSensorValues().toArray());
assertArrayEquals(new Long[]{null, null}, event.getSensorLongValues().toArray());
assertArrayEquals(new Double[]{null, null}, event.getSensorDoubleValues().toArray());
assertArrayEquals(new SensorState[]{null, null}, event.getSensorStates().toArray());
assertArrayEquals(new Date[]{null, null}, event.getDateValues().toArray());
assertArrayEquals(new Date[]{null, null}, event.getTimestampValues().toArray());
Expand All @@ -277,6 +286,7 @@ public Void apply(EntityManager entityManager) {
event.setSensorNames(Collections.<String>emptyList());
event.setSensorValues(Collections.<Integer>emptyList());
event.setSensorLongValues(Collections.<Long>emptyList());
event.setSensorDoubleValues(Collections.<Double>emptyList());
event.setSensorStates(Collections.<SensorState>emptyList());
event.setDateValues(Collections.<Date>emptyList());
event.setTimestampValues(Collections.<Date>emptyList());
Expand All @@ -295,6 +305,7 @@ public Void apply(EntityManager entityManager) {
assertArrayEquals(new String[]{}, event.getSensorNames().toArray());
assertArrayEquals(new Integer[]{}, event.getSensorValues().toArray());
assertArrayEquals(new Long[]{}, event.getSensorLongValues().toArray());
assertArrayEquals(new Double[]{}, event.getSensorDoubleValues().toArray());
assertArrayEquals(new SensorState[]{}, event.getSensorStates().toArray());
assertArrayEquals(new Date[]{}, event.getDateValues().toArray());
assertArrayEquals(new Date[]{}, event.getTimestampValues().toArray());
Expand Down Expand Up @@ -344,6 +355,7 @@ public Void apply(EntityManager entityManager) {
@TypeDef(name = "string-list-array", typeClass = ListArrayType.class),
@TypeDef(name = "int-list-array", typeClass = ListArrayType.class),
@TypeDef(name = "long-list-array", typeClass = ListArrayType.class),
@TypeDef(name = "double-list-array", typeClass = ListArrayType.class),
@TypeDef(name = "date-list-array", typeClass = ListArrayType.class),
@TypeDef(name = "timestamp-list-array", typeClass = ListArrayType.class),
@TypeDef(name = "sensor-state-array", typeClass = ListArrayType.class, parameters = {
Expand All @@ -368,6 +380,13 @@ public static class Event extends BaseEntity {
@Column(name = "sensor_long_values", columnDefinition = "bigint[]")
private List<Long> sensorLongValues;

@Type(type = "double-list-array")
@Column(
name = "sensor_double_values",
columnDefinition = "float8[]"
)
private List<Double> sensorDoubleValues;

@Type(type = "sensor-state-array")
@Column(name = "sensor_states", columnDefinition = "sensor_state[]")
private List<SensorState> sensorStates;
Expand Down Expand Up @@ -412,6 +431,14 @@ public void setSensorLongValues(List<Long> sensorLongValues) {
this.sensorLongValues = sensorLongValues;
}

public List<Double> getSensorDoubleValues() {
return sensorDoubleValues;
}

public void setSensorDoubleValues(List<Double> sensorDoubleValues) {
this.sensorDoubleValues = sensorDoubleValues;
}

public List<SensorState> getSensorStates() {
return sensorStates;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
@TypeDef(name = "string-array", typeClass = StringArrayType.class),
@TypeDef(name = "int-array", typeClass = IntArrayType.class),
@TypeDef(name = "long-array", typeClass = LongArrayType.class),
@TypeDef(name = "double-array", typeClass = DoubleArrayType.class),
@TypeDef(name = "date-array", typeClass = DateArrayType.class),
@TypeDef(name = "timestamp-array", typeClass = TimestampArrayType.class),
@TypeDef(name = "jsonb-node", typeClass = JsonNodeBinaryType.class),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.vladmihalcea.hibernate.type.array;

import com.vladmihalcea.hibernate.type.array.internal.AbstractArrayType;
import com.vladmihalcea.hibernate.type.array.internal.DoubleArrayTypeDescriptor;
import com.vladmihalcea.hibernate.type.util.Configuration;

/**
* Maps an {@code double[]} array on a PostgreSQL ARRAY type. Multidimensional arrays are supported as well, as explained in <a href="https://vladmihalcea.com/multidimensional-array-jpa-hibernate/">this article</a>.
* <p>
* For more details about how to use it, check out <a href=
* "https://vladmihalcea.com/how-to-map-java-and-sql-arrays-with-jpa-and-hibernate/">this
* article</a> on <a href="https://vladmihalcea.com/">vladmihalcea.com</a>.
*
* @author Vlad Mihalcea
*/
public class DoubleArrayType extends AbstractArrayType<double[]> {

public static final DoubleArrayType INSTANCE = new DoubleArrayType();

public DoubleArrayType() {
super(
new DoubleArrayTypeDescriptor()
);
}

public DoubleArrayType(Configuration configuration) {
super(
new DoubleArrayTypeDescriptor(),
configuration
);
}

@Override
public String getName() {
return "double-array";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.vladmihalcea.hibernate.type.array.internal;

/**
* @author Vlad Mihalcea
*/
public class DoubleArrayTypeDescriptor
extends AbstractArrayTypeDescriptor<double[]> {

public DoubleArrayTypeDescriptor() {
super(double[].class);
}

@Override
protected String getSqlArrayType() {
return "float8";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public void setParameterValues(Properties parameters) {
sqlArrayType = "integer";
} else if (Long.class.isAssignableFrom(arrayElementClass)) {
sqlArrayType = "bigint";
} else if (Double.class.isAssignableFrom(arrayElementClass)) {
sqlArrayType = "float8";
} else if (String.class.isAssignableFrom(arrayElementClass)) {
sqlArrayType = "text";
} else if (UUID.class.isAssignableFrom(arrayElementClass)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,11 @@
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.UUID;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.junit.Assert.*;

/**
* @author Vlad Mihalcea
Expand Down Expand Up @@ -116,6 +112,7 @@ public Void apply(EntityManager entityManager) {
event.setSensorNames(new String[]{"Temperature", "Pressure"});
event.setSensorValues(new int[]{12, 756});
event.setSensorLongValues(new long[]{42L, 9223372036854775800L});
event.setSensorDoubleValues(new double[]{0.123, 456.789});
event.setSensorStates(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN});
event.setDateValues(new Date[]{date1, date2});
event.setTimestampValues(new Date[]{date1, date2});
Expand All @@ -135,6 +132,7 @@ public Void apply(EntityManager entityManager) {
assertArrayEquals(new String[]{"Temperature", "Pressure"}, event.getSensorNames());
assertArrayEquals(new int[]{12, 756}, event.getSensorValues());
assertArrayEquals(new long[]{42L, 9223372036854775800L}, event.getSensorLongValues());
assertArrayEquals(new double[]{0.123, 456.789}, event.getSensorDoubleValues(), 0.01);
assertArrayEquals(new SensorState[]{SensorState.ONLINE, SensorState.OFFLINE, SensorState.ONLINE, SensorState.UNKNOWN}, event.getSensorStates());
assertEquals(date1.getTime(), event.getDateValues()[0].getTime());
assertEquals(date2.getTime(), event.getDateValues()[1].getTime());
Expand Down Expand Up @@ -203,6 +201,10 @@ public static class Event extends BaseEntity {
@Column(name = "sensor_long_values", columnDefinition = "bigint[]")
private long[] sensorLongValues;

@Type(type = "double-array")
@Column(name = "sensor_double_values", columnDefinition = "float8[]")
private double[] sensorDoubleValues;

@Type(type = "date-array")
@Column(name = "date_values", columnDefinition = "date[]")
private Date[] dateValues;
Expand Down Expand Up @@ -247,6 +249,14 @@ public void setSensorLongValues(long[] sensorLongValues) {
this.sensorLongValues = sensorLongValues;
}

public double[] getSensorDoubleValues() {
return sensorDoubleValues;
}

public void setSensorDoubleValues(double[] sensorDoubleValues) {
this.sensorDoubleValues = sensorDoubleValues;
}

public SensorState[] getSensorStates() {
return sensorStates;
}
Expand Down
Loading

0 comments on commit e74b49d

Please sign in to comment.