Skip to content

Commit

Permalink
refactor: Added more testcases to the PlcValueHandler tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdutz committed Sep 26, 2024
1 parent 69e78c6 commit 3c0e4ea
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,106 @@
*/
package org.apache.plc4x.java.spi.values;

import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.api.types.PlcValueType;
import org.apache.plc4x.java.spi.codegen.WithOption;
import org.apache.plc4x.java.spi.generation.SerializationException;
import org.apache.plc4x.java.spi.generation.WriteBuffer;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;

public class PlcDATE_AND_TIME extends PlcIECValue<LocalDateTime> {

public static PlcDATE_AND_TIME of(Object value) {
if (value instanceof LocalDateTime) {
return new PlcDATE_AND_TIME((LocalDateTime) value);
} else if (value instanceof Byte) {
return new PlcDATE_AND_TIME((Byte) value);
} else if (value instanceof Short) {
return new PlcDATE_AND_TIME((Short) value);
} else if (value instanceof Integer) {
return new PlcDATE_AND_TIME((Integer) value);
} else if (value instanceof Long) {
return new PlcDATE_AND_TIME(LocalDateTime.ofInstant(
Instant.ofEpochSecond((long) value), ZoneOffset.UTC));
return new PlcDATE_AND_TIME((Long) value);
} else if (value instanceof Float) {
return new PlcDATE_AND_TIME((Float) value);
} else if (value instanceof Double) {
return new PlcDATE_AND_TIME((Double) value);
} else if (value instanceof BigInteger) {
return new PlcDATE_AND_TIME((BigInteger) value);
} else if (value instanceof BigDecimal) {
return new PlcDATE_AND_TIME((BigDecimal) value);
} else {
return new PlcDATE_AND_TIME(LocalDateTime.parse(value.toString()));
}
throw new PlcRuntimeException("Invalid value type");
}

public static PlcDATE_AND_TIME ofSecondsSinceEpoch(long secondsSinceEpoch) {
return new PlcDATE_AND_TIME(LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0,
ZoneOffset.UTC));
OffsetDateTime.now().getOffset()));
}

public static PlcDATE_AND_TIME ofSegments(int year, int month, int day, int hour, int minutes, int seconds, int nanoseconds) {
return new PlcDATE_AND_TIME(LocalDateTime.of(year, month, day, hour, minutes, seconds, nanoseconds));
}

public PlcDATE_AND_TIME(LocalDateTime value) {
this.value = value;
public PlcDATE_AND_TIME(Byte secondsSinceEpoch) {
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0,
OffsetDateTime.now().getOffset());
this.isNullable = false;
}

public PlcDATE_AND_TIME(Short secondsSinceEpoch) {
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0,
OffsetDateTime.now().getOffset());
this.isNullable = false;
}

public PlcDATE_AND_TIME(long secondsSinceEpoch) {
public PlcDATE_AND_TIME(Integer secondsSinceEpoch) {
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0,
ZoneOffset.UTC);
OffsetDateTime.now().getOffset());
this.isNullable = false;
}

public PlcDATE_AND_TIME(Long secondsSinceEpoch) {
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0,
OffsetDateTime.now().getOffset());
this.isNullable = false;
}

public PlcDATE_AND_TIME(Float secondsSinceEpoch) {
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0,
OffsetDateTime.now().getOffset());
this.isNullable = false;
}

public PlcDATE_AND_TIME(Double secondsSinceEpoch) {
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0,
OffsetDateTime.now().getOffset());
this.isNullable = false;
}

public PlcDATE_AND_TIME(BigInteger secondsSinceEpoch) {
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0,
OffsetDateTime.now().getOffset());
this.isNullable = false;
}

public PlcDATE_AND_TIME(BigDecimal secondsSinceEpoch) {
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0,
OffsetDateTime.now().getOffset());
this.isNullable = false;
}

public PlcDATE_AND_TIME(LocalDateTime value) {
this.value = value;
this.isNullable = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,56 +73,56 @@ public PlcLDATE(LocalDate value) {
public PlcLDATE(Byte secondsSinceEpoch) {
// REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9"
this.value = LocalDateTime.ofEpochSecond((long) secondsSinceEpoch, 0,
OffsetDateTime.now().getOffset() ).toLocalDate();
OffsetDateTime.now().getOffset()).toLocalDate();
this.isNullable = false;
}

public PlcLDATE(Short secondsSinceEpoch) {
// REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9"
this.value = LocalDateTime.ofEpochSecond((long) secondsSinceEpoch, 0,
OffsetDateTime.now().getOffset() ).toLocalDate();
OffsetDateTime.now().getOffset()).toLocalDate();
this.isNullable = false;
}

public PlcLDATE(Integer secondsSinceEpoch) {
// REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9"
this.value = LocalDateTime.ofEpochSecond((long) secondsSinceEpoch, 0,
OffsetDateTime.now().getOffset() ).toLocalDate();
OffsetDateTime.now().getOffset()).toLocalDate();
this.isNullable = false;
}

public PlcLDATE(Long secondsSinceEpoch) {
// REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9"
this.value = LocalDateTime.ofEpochSecond((long) secondsSinceEpoch, 0,
OffsetDateTime.now().getOffset() ).toLocalDate();
OffsetDateTime.now().getOffset()).toLocalDate();
this.isNullable = false;
}

public PlcLDATE(Float secondsSinceEpoch) {
// REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9"
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0,
OffsetDateTime.now().getOffset() ).toLocalDate();
OffsetDateTime.now().getOffset()).toLocalDate();
this.isNullable = false;
}

public PlcLDATE(Double secondsSinceEpoch) {
// REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9"
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0,
OffsetDateTime.now().getOffset() ).toLocalDate();
OffsetDateTime.now().getOffset()).toLocalDate();
this.isNullable = false;
}

public PlcLDATE(BigInteger secondsSinceEpoch) {
// REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9"
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0,
OffsetDateTime.now().getOffset() ).toLocalDate();
OffsetDateTime.now().getOffset()).toLocalDate();
this.isNullable = false;
}

public PlcLDATE(BigDecimal secondsSinceEpoch) {
// REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9"
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch.longValue(), 0,
OffsetDateTime.now().getOffset() ).toLocalDate();
OffsetDateTime.now().getOffset()).toLocalDate();
this.isNullable = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@
*/
package org.apache.plc4x.java.spi.values;

import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.api.types.PlcValueType;
import org.apache.plc4x.java.spi.codegen.WithOption;
import org.apache.plc4x.java.spi.generation.SerializationException;
import org.apache.plc4x.java.spi.generation.WriteBuffer;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.LocalTime;
Expand All @@ -33,33 +33,74 @@ public class PlcLTIME_OF_DAY extends PlcIECValue<LocalTime> {
public static PlcLTIME_OF_DAY of(Object value) {
if (value instanceof LocalTime) {
return new PlcLTIME_OF_DAY((LocalTime) value);
} else if (value instanceof Byte) {
return new PlcLTIME_OF_DAY((Byte) value);
} else if (value instanceof Short) {
return new PlcLTIME_OF_DAY((Short) value);
} else if (value instanceof Integer) {
return new PlcLTIME_OF_DAY((Integer) value);
} else if (value instanceof Long) {
return new PlcLTIME_OF_DAY(LocalTime.ofSecondOfDay(((Long) value) / 1000));
return new PlcLTIME_OF_DAY((Long) value);
} else if (value instanceof Float) {
return new PlcLTIME_OF_DAY((Float) value);
} else if (value instanceof Double) {
return new PlcLTIME_OF_DAY((Double) value);
} else if (value instanceof BigInteger) {
// TODO: Not 100% correct, we're loosing precision here
return new PlcLTIME_OF_DAY(LocalTime.ofSecondOfDay(((BigInteger) value).longValue() / 1000));
return new PlcLTIME_OF_DAY((BigInteger) value);
} else if (value instanceof BigDecimal) {
return new PlcLTIME_OF_DAY((BigDecimal) value);
} else {
return new PlcLTIME_OF_DAY(LocalTime.parse(value.toString()));
}
throw new PlcRuntimeException("Invalid value type");
}

public static PlcLTIME_OF_DAY ofNanosecondsSinceMidnight(BigInteger nanosecondsSinceMidnight) {
// TODO: Not 100% correct, we're loosing precision here
return new PlcLTIME_OF_DAY(LocalTime.ofNanoOfDay(nanosecondsSinceMidnight.longValue()));
}

public PlcLTIME_OF_DAY(LocalTime value) {
this.value = value;
public PlcLTIME_OF_DAY(Byte millisecondsSinceMidnight) {
this.value = LocalTime.ofNanoOfDay((long) millisecondsSinceMidnight * 1000000);
this.isNullable = false;
}

public PlcLTIME_OF_DAY(Long nanosecondsSinceMidnight) {
this.value = LocalTime.ofNanoOfDay(nanosecondsSinceMidnight);
public PlcLTIME_OF_DAY(Short millisecondsSinceMidnight) {
this.value = LocalTime.ofNanoOfDay((long) millisecondsSinceMidnight * 1000000);
this.isNullable = false;
}

public PlcLTIME_OF_DAY(BigInteger nanosecondsSinceMidnight) {
// TODO: Not 100% correct, we're loosing precision here
this.value = LocalTime.ofNanoOfDay(nanosecondsSinceMidnight.longValue());
public PlcLTIME_OF_DAY(Integer millisecondsSinceMidnight) {
this.value = LocalTime.ofNanoOfDay((long) millisecondsSinceMidnight * 1000000);
this.isNullable = false;
}

public PlcLTIME_OF_DAY(Long millisecondsSinceMidnight) {
this.value = LocalTime.ofNanoOfDay(millisecondsSinceMidnight * 1000000);
this.isNullable = false;
}

public PlcLTIME_OF_DAY(Float millisecondsSinceMidnight) {
this.value = LocalTime.ofNanoOfDay(millisecondsSinceMidnight.longValue() * 1000000);
this.isNullable = false;
}

public PlcLTIME_OF_DAY(Double millisecondsSinceMidnight) {
this.value = LocalTime.ofNanoOfDay(millisecondsSinceMidnight.longValue() * 1000000);
this.isNullable = false;
}

public PlcLTIME_OF_DAY(BigInteger millisecondsSinceMidnight) {
this.value = LocalTime.ofNanoOfDay(millisecondsSinceMidnight.longValue() * 1000000);
this.isNullable = false;
}

public PlcLTIME_OF_DAY(BigDecimal millisecondsSinceMidnight) {
this.value = LocalTime.ofNanoOfDay(millisecondsSinceMidnight.longValue() * 1000000);
this.isNullable = false;
}

public PlcLTIME_OF_DAY(LocalTime value) {
this.value = value;
this.isNullable = false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@
*/
package org.apache.plc4x.java.spi.values;

import org.apache.plc4x.java.api.exceptions.PlcRuntimeException;
import org.apache.plc4x.java.api.types.PlcValueType;
import org.apache.plc4x.java.spi.codegen.WithOption;
import org.apache.plc4x.java.spi.generation.SerializationException;
import org.apache.plc4x.java.spi.generation.WriteBuffer;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.LocalTime;

Expand All @@ -32,23 +33,73 @@ public class PlcTIME_OF_DAY extends PlcIECValue<LocalTime> {
public static PlcTIME_OF_DAY of(Object value) {
if (value instanceof LocalTime) {
return new PlcTIME_OF_DAY((LocalTime) value);
} else if(value instanceof Long) {
} else if (value instanceof Byte) {
return new PlcTIME_OF_DAY((Byte) value);
} else if (value instanceof Short) {
return new PlcTIME_OF_DAY((Short) value);
} else if (value instanceof Integer) {
return new PlcTIME_OF_DAY((Integer) value);
} else if (value instanceof Long) {
return new PlcTIME_OF_DAY((Long) value);
} else if (value instanceof Float) {
return new PlcTIME_OF_DAY((Float) value);
} else if (value instanceof Double) {
return new PlcTIME_OF_DAY((Double) value);
} else if (value instanceof BigInteger) {
return new PlcTIME_OF_DAY((BigInteger) value);
} else if (value instanceof BigDecimal) {
return new PlcTIME_OF_DAY((BigDecimal) value);
} else {
return new PlcTIME_OF_DAY(LocalTime.parse(value.toString()));
}
throw new PlcRuntimeException("Invalid value type");
}

public static PlcTIME_OF_DAY ofMillisecondsSinceMidnight(long millisecondsSinceMidnight) {
return new PlcTIME_OF_DAY(LocalTime.ofNanoOfDay(millisecondsSinceMidnight * 1000_000));
}

public PlcTIME_OF_DAY(LocalTime value) {
this.value = value;
public PlcTIME_OF_DAY(Byte secondsSinceMidnight) {
this.value = LocalTime.ofSecondOfDay(secondsSinceMidnight);
this.isNullable = false;
}

public PlcTIME_OF_DAY(long millisecondsSinceMidnight) {
this.value = LocalTime.ofNanoOfDay(millisecondsSinceMidnight * 1000_000);
public PlcTIME_OF_DAY(Short secondsSinceMidnight) {
this.value = LocalTime.ofSecondOfDay(secondsSinceMidnight);
this.isNullable = false;
}

public PlcTIME_OF_DAY(Integer secondsSinceMidnight) {
this.value = LocalTime.ofSecondOfDay(secondsSinceMidnight);
this.isNullable = false;
}

public PlcTIME_OF_DAY(Long secondsSinceMidnight) {
this.value = LocalTime.ofSecondOfDay(secondsSinceMidnight);
this.isNullable = false;
}

public PlcTIME_OF_DAY(Float secondsSinceMidnight) {
this.value = LocalTime.ofSecondOfDay(secondsSinceMidnight.longValue());
this.isNullable = false;
}

public PlcTIME_OF_DAY(Double secondsSinceMidnight) {
this.value = LocalTime.ofSecondOfDay(secondsSinceMidnight.longValue());
this.isNullable = false;
}

public PlcTIME_OF_DAY(BigInteger secondsSinceMidnight) {
this.value = LocalTime.ofSecondOfDay(secondsSinceMidnight.longValue());
this.isNullable = false;
}

public PlcTIME_OF_DAY(BigDecimal secondsSinceMidnight) {
this.value = LocalTime.ofSecondOfDay(secondsSinceMidnight.longValue());
this.isNullable = false;
}

public PlcTIME_OF_DAY(LocalTime value) {
this.value = value;
this.isNullable = false;
}

Expand Down
Loading

0 comments on commit 3c0e4ea

Please sign in to comment.