mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-06-14 09:00:04 +02:00
520a510dd9
The boundaries are enforced on the stored value when decoding, before applying the adjustments for scale and offset. Also add some tests for the BaseTypes Introduce new FieldDefinition for Temperature and WeatherCondition (removing the static class) Add accessors for field data in the containing RecordData, thus keeping the FieldData private
59 lines
1.8 KiB
Java
59 lines
1.8 KiB
Java
package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.baseTypes;
|
|
|
|
import java.math.BigInteger;
|
|
import java.nio.ByteBuffer;
|
|
|
|
public class BaseTypeLong implements BaseTypeInterface {
|
|
private final int size = 8;
|
|
private final BigInteger min;
|
|
private final BigInteger max;
|
|
private final long invalid;
|
|
private final boolean unsigned;
|
|
|
|
BaseTypeLong(boolean unsigned, long invalid) {
|
|
if (unsigned) {
|
|
this.min = BigInteger.valueOf(0);
|
|
this.max = BigInteger.valueOf(0xFFFFFFFFFFFFFFFFL);
|
|
} else {
|
|
this.min = BigInteger.valueOf(Long.MIN_VALUE);
|
|
this.max = BigInteger.valueOf(Long.MAX_VALUE);
|
|
}
|
|
this.invalid = invalid;
|
|
this.unsigned = unsigned;
|
|
}
|
|
|
|
public int getByteSize() {
|
|
return size;
|
|
}
|
|
|
|
@Override
|
|
public Object decode(ByteBuffer byteBuffer, int scale, int offset) {
|
|
BigInteger i = unsigned ? BigInteger.valueOf(byteBuffer.getLong() & 0xFFFFFFFFFFFFFFFFL) : BigInteger.valueOf(byteBuffer.getLong());
|
|
if (!unsigned && (i.compareTo(min) < 0 || i.compareTo(max) > 0))
|
|
return null;
|
|
if (i.compareTo(BigInteger.valueOf(invalid)) == 0)
|
|
return null;
|
|
return (i.longValue() + offset) / scale;
|
|
}
|
|
|
|
@Override
|
|
public void encode(ByteBuffer byteBuffer, Object o, int scale, int offset) {
|
|
if (null == o) {
|
|
invalidate(byteBuffer);
|
|
return;
|
|
}
|
|
BigInteger i = BigInteger.valueOf(((Number) o).longValue() * scale - offset);
|
|
if (!unsigned && (i.compareTo(min) < 0 || i.compareTo(max) > 0)) {
|
|
invalidate(byteBuffer);
|
|
return;
|
|
}
|
|
byteBuffer.putLong(i.longValue());
|
|
}
|
|
|
|
@Override
|
|
public void invalidate(ByteBuffer byteBuffer) {
|
|
byteBuffer.putLong((long) invalid);
|
|
}
|
|
|
|
}
|