Skip to content

Commit

Permalink
refactor: Added more testcases to the PlcValueHandler tests (Refactor…
Browse files Browse the repository at this point in the history
…ed the PlcValue types)
  • Loading branch information
chrisdutz committed Sep 26, 2024
1 parent f7638e5 commit 1c5ffaf
Show file tree
Hide file tree
Showing 34 changed files with 350 additions and 211 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static PlcBINT of(Object value) {
} else if (value instanceof BigDecimal) {
return new PlcBINT((BigDecimal) value);
} else {
return new PlcBINT((String) value);
return new PlcBINT(value.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static PlcBREAL of(Object value) {
} else if (value instanceof BigDecimal) {
return new PlcBREAL((BigDecimal) value);
} else {
return new PlcBREAL((String) value);
return new PlcBREAL(value.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static PlcBYTE of(Object value) {
} else if (value instanceof BigDecimal) {
return new PlcBYTE((BigDecimal) value);
} else {
return new PlcBYTE((String) value);
return new PlcBYTE(value.toString());
}
}

Expand Down Expand Up @@ -120,7 +120,7 @@ public PlcBYTE(BigInteger value) {
}

public PlcBYTE(BigDecimal value) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0) || (value.scale() > 0)) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) {
throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()));
}
this.value = value.shortValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ public static PlcCHAR of(Object value) {
} else if(value instanceof Character){
return new PlcCHAR((Character) value);
}else {
return new PlcCHAR((String) value);
return new PlcCHAR(value.toString());
}
}

public PlcCHAR(Boolean value) {
super();
this.value = value ? Short.valueOf((short) 1) : Short.valueOf((short) 0);
this.value = value ? (short) 'T' : (short) 'F';
this.isNullable = false;
}

Expand Down Expand Up @@ -119,7 +119,7 @@ public PlcCHAR(Long value) {

public PlcCHAR(Float value) {
super();
if ((value >= minValue) && (value <= maxValue) && (value % 1 == 0)) {
if ((value >= minValue) && (value <= maxValue)) {
this.value = value.shortValue();
this.isNullable = false;
} else {
Expand All @@ -129,7 +129,7 @@ public PlcCHAR(Float value) {

public PlcCHAR(Double value) {
super();
if ((value >= minValue) && (value <= maxValue) && (value % 1 == 0)) {
if ((value >= minValue) && (value <= maxValue)) {
this.value = value.shortValue();
this.isNullable = false;
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@
import org.apache.plc4x.java.spi.generation.WriteBuffer;

import java.nio.charset.StandardCharsets;
import java.time.*;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class PlcDATE extends PlcSimpleValue<LocalDate> {
public class PlcDATE extends PlcIECValue<LocalDate> {

public static PlcDATE of(Object value) {
if (value instanceof LocalDate) {
Expand All @@ -54,19 +57,22 @@ public static PlcDATE ofDaysSinceEpoch(int daysSinceEpoch) {
}

public PlcDATE(LocalDate value) {
super(value, true);
this.value = value;
this.isNullable = false;
}

public PlcDATE(int daysSinceEpoch) {
// REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9"
super(LocalDateTime.ofInstant(
Instant.ofEpochSecond(((long) daysSinceEpoch) * 86400), ZoneOffset.UTC).toLocalDate(), true);
this.value = LocalDateTime.ofInstant(
Instant.ofEpochSecond(((long) daysSinceEpoch) * 86400), ZoneOffset.UTC).toLocalDate();
this.isNullable = false;
}

public PlcDATE(long secondsSinceEpoch) {
// REMARK: Yes, I'm using LocalDataTime.ofInstant as LocalDate.ofInstant is marked "JDK 1.9"
super(LocalDateTime.ofInstant(
Instant.ofEpochSecond(secondsSinceEpoch), ZoneOffset.UTC).toLocalDate(), true);
this.value = LocalDateTime.ofInstant(
Instant.ofEpochSecond(secondsSinceEpoch), ZoneOffset.UTC).toLocalDate();
this.isNullable = false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@

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

public class PlcDATE_AND_LTIME extends PlcSimpleValue<LocalDateTime> {
public class PlcDATE_AND_LTIME extends PlcIECValue<LocalDateTime> {

public static PlcDATE_AND_LTIME of(Object value) {
if (value instanceof LocalDateTime) {
Expand All @@ -48,16 +52,16 @@ public static PlcDATE_AND_LTIME ofNanosecondsSinceEpoch(BigInteger nanosecondsSi
}

public PlcDATE_AND_LTIME(LocalDateTime value) {
super(value, true);
this.value = value;
this.isNullable = false;
}

public PlcDATE_AND_LTIME(BigInteger nanosecondsSinceEpoch) {
super(
LocalDateTime.ofEpochSecond(
this.value = LocalDateTime.ofEpochSecond(
nanosecondsSinceEpoch.divide(BigInteger.valueOf(1000000000L)).longValue(),
nanosecondsSinceEpoch.mod(BigInteger.valueOf(1000000000L)).intValue(),
ZoneOffset.UTC),
true);
ZoneOffset.UTC);
this.isNullable = false;
}

public static PlcDATE_AND_LTIME ofSegments(int year, int month, int day, int hour, int minutes, int seconds, long nannosecondsOfSecond) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@
import org.apache.plc4x.java.spi.generation.WriteBuffer;

import java.nio.charset.StandardCharsets;
import java.time.*;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;

public class PlcDATE_AND_TIME extends PlcSimpleValue<LocalDateTime> {
public class PlcDATE_AND_TIME extends PlcIECValue<LocalDateTime> {

public static PlcDATE_AND_TIME of(Object value) {
if (value instanceof LocalDateTime) {
Expand All @@ -49,16 +53,19 @@ public static PlcDATE_AND_TIME ofSegments(int year, int month, int day, int hour
}

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

public PlcDATE_AND_TIME(long secondsSinceEpoch) {
super(LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0,
ZoneOffset.UTC), true);
this.value = LocalDateTime.ofEpochSecond(secondsSinceEpoch, 0,
ZoneOffset.UTC);
this.isNullable = false;
}

public PlcDATE_AND_TIME(int year, int month, int day, int hour, int minutes, int seconds, int nanoseconds) {
super(LocalDateTime.of(year, month, day, hour, minutes, seconds, nanoseconds), true);
this.value = LocalDateTime.of(year, month, day, hour, minutes, seconds, nanoseconds);
this.isNullable = false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static PlcDINT of(Object value) {
} else if (value instanceof BigDecimal) {
return new PlcDINT((BigDecimal) value);
} else {
return new PlcDINT((String) value);
return new PlcDINT(value.toString());
}
}

Expand Down Expand Up @@ -109,7 +109,7 @@ public PlcDINT(BigInteger value) {
}

public PlcDINT(BigDecimal value) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0) || (value.scale() > 0)) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) {
throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()));
}
this.value = value.intValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public static PlcDWORD of(Object value) {
} else if (value instanceof BigDecimal) {
return new PlcDWORD((BigDecimal) value);
} else {
return new PlcDWORD((String) value);
return new PlcDWORD(value.toString());
}
}

Expand Down Expand Up @@ -120,7 +120,7 @@ public PlcDWORD(BigInteger value) {
}

public PlcDWORD(BigDecimal value) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0) || (value.scale() > 0)) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) {
throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()));
}
this.value = value.longValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static PlcINT of(Object value) {
} else if (value instanceof BigDecimal) {
return new PlcINT((BigDecimal) value);
} else {
return new PlcINT((String) value);
return new PlcINT(value.toString());
}
}

Expand Down Expand Up @@ -113,7 +113,7 @@ public PlcINT(BigInteger value) {
}

public PlcINT(BigDecimal value) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0) || (value.scale() > 0)) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) {
throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()));
}
this.value = value.shortValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@

import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.time.*;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class PlcLDATE extends PlcSimpleValue<LocalDate> {
public class PlcLDATE extends PlcIECValue<LocalDate> {

public static PlcLDATE of(Object value) {
if (value instanceof LocalDate) {
Expand All @@ -48,13 +51,15 @@ public static PlcLDATE ofNanosecondsSinceEpoch(BigInteger nanosecondsSinceEpoch)
}

public PlcLDATE(LocalDate value) {
super(value, true);
this.value = value;
this.isNullable = false;
}

public PlcLDATE(BigInteger nanosecondsSinceEpoch) {
super(LocalDateTime.ofEpochSecond(nanosecondsSinceEpoch.longValue() / 1000000,
this.value = LocalDateTime.ofEpochSecond(nanosecondsSinceEpoch.longValue() / 1000000,
(int) (nanosecondsSinceEpoch.longValue() % 1000000),
ZoneOffset.of(ZoneOffset.systemDefault().getId())).toLocalDate(), true);
ZoneOffset.of(ZoneOffset.systemDefault().getId())).toLocalDate();
this.isNullable = false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@

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

public class PlcLDATE_AND_TIME extends PlcSimpleValue<LocalDateTime> {
public class PlcLDATE_AND_TIME extends PlcIECValue<LocalDateTime> {

public static PlcLDATE_AND_TIME of(Object value) {
if (value instanceof LocalDateTime) {
Expand All @@ -48,13 +52,15 @@ public static PlcLDATE_AND_TIME ofNanosecondsSinceEpoch(BigInteger nanosecondsSi
}

public PlcLDATE_AND_TIME(LocalDateTime value) {
super(value, true);
this.value = value;
this.isNullable = false;
}

public PlcLDATE_AND_TIME(BigInteger nanosecondsSinceEpoch) {
super(LocalDateTime.ofEpochSecond(nanosecondsSinceEpoch.divide(BigInteger.valueOf(1000_000)).longValue(),
this.value = LocalDateTime.ofEpochSecond(nanosecondsSinceEpoch.divide(BigInteger.valueOf(1000_000)).longValue(),
nanosecondsSinceEpoch.mod(BigInteger.valueOf(1000_000)).intValue(),
ZoneOffset.of(ZoneOffset.systemDefault().getId())), true);
ZoneOffset.of(ZoneOffset.systemDefault().getId()));
this.isNullable = false;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static PlcLINT of(Object value) {
} else if (value instanceof BigDecimal) {
return new PlcLINT((BigDecimal) value);
} else {
return new PlcLINT((String) value);
return new PlcLINT(value.toString());
}
}

Expand Down Expand Up @@ -106,7 +106,7 @@ public PlcLINT(BigInteger value) {
}

public PlcLINT(BigDecimal value) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0) || (value.scale() > 0)) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) {
throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()));
}
this.value = value.longValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static PlcLREAL of(Object value) {
} else if (value instanceof BigDecimal) {
return new PlcLREAL((BigDecimal) value);
} else {
return new PlcLREAL((String) value);
return new PlcLREAL(value.toString());
}
}

Expand Down Expand Up @@ -96,7 +96,7 @@ public PlcLREAL(BigInteger value) {
}

public PlcLREAL(BigDecimal value) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0) || (value.scale() > 0)) {
if ((value.compareTo(BigDecimal.valueOf(minValue)) < 0) || (value.compareTo(BigDecimal.valueOf(maxValue)) > 0)) {
throw new PlcInvalidTagException(String.format(VALUE_OUT_OF_RANGE, value, minValue, maxValue, this.getClass().getSimpleName()));
}
this.value = value.doubleValue();
Expand Down
Loading

0 comments on commit 1c5ffaf

Please sign in to comment.