mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-06-01 19:06:06 +02:00
6b7db3d92d
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
51 lines
1.3 KiB
Java
51 lines
1.3 KiB
Java
package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.baseTypes;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
public class BaseTypeDouble implements BaseTypeInterface {
|
|
private final int size = 8;
|
|
private final double min;
|
|
private final double max;
|
|
private final double invalid;
|
|
|
|
BaseTypeDouble() {
|
|
this.min = -Double.MAX_VALUE;
|
|
this.max = Double.MAX_VALUE;
|
|
this.invalid = Double.longBitsToDouble(0xFFFFFFFFFFFFFFFFL);
|
|
}
|
|
|
|
public int getByteSize() {
|
|
return size;
|
|
}
|
|
|
|
@Override
|
|
public Object decode(final ByteBuffer byteBuffer, int scale, int offset) {
|
|
double d = byteBuffer.getDouble();
|
|
if (d < min || d > max) {
|
|
return null;
|
|
}
|
|
if (Double.isNaN(d) || d == invalid)
|
|
return null;
|
|
return (d + offset) / scale;
|
|
}
|
|
|
|
@Override
|
|
public void encode(ByteBuffer byteBuffer, Object o, int scale, int offset) {
|
|
if (null == o) {
|
|
invalidate(byteBuffer);
|
|
return;
|
|
}
|
|
double d = ((Number) o).doubleValue() * scale - offset;
|
|
if (d < min || d > max) {
|
|
invalidate(byteBuffer);
|
|
return;
|
|
}
|
|
byteBuffer.putDouble(d);
|
|
}
|
|
|
|
@Override
|
|
public void invalidate(ByteBuffer byteBuffer) {
|
|
byteBuffer.putDouble(invalid);
|
|
}
|
|
}
|