mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge
synced 2024-06-07 05:38:13 +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
57 lines
1.5 KiB
Java
57 lines
1.5 KiB
Java
package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.fit.baseTypes;
|
|
|
|
import java.nio.ByteBuffer;
|
|
|
|
public class BaseTypeShort implements BaseTypeInterface {
|
|
private final int min;
|
|
private final int max;
|
|
private final int invalid;
|
|
private final boolean unsigned;
|
|
private final int size = 2;
|
|
|
|
BaseTypeShort(boolean unsigned, int invalid) {
|
|
if (unsigned) {
|
|
this.min = 0;
|
|
this.max = 0xffff;
|
|
} else {
|
|
this.min = Short.MIN_VALUE;
|
|
this.max = Short.MAX_VALUE;
|
|
}
|
|
this.invalid = invalid;
|
|
this.unsigned = unsigned;
|
|
}
|
|
|
|
public int getByteSize() {
|
|
return size;
|
|
}
|
|
|
|
@Override
|
|
public Object decode(final ByteBuffer byteBuffer, int scale, int offset) {
|
|
int s = unsigned ? Short.toUnsignedInt(byteBuffer.getShort()) : byteBuffer.getShort();
|
|
if (s < min || s > max)
|
|
return null;
|
|
if (s == invalid)
|
|
return null;
|
|
return (s + offset) / scale;
|
|
}
|
|
|
|
@Override
|
|
public void encode(ByteBuffer byteBuffer, Object o, int scale, int offset) {
|
|
if (null == o) {
|
|
invalidate(byteBuffer);
|
|
return;
|
|
}
|
|
int i = ((Number) o).intValue() * scale - offset;
|
|
if (i < min || i > max) {
|
|
invalidate(byteBuffer);
|
|
return;
|
|
}
|
|
byteBuffer.putShort((short) i);
|
|
}
|
|
|
|
@Override
|
|
public void invalidate(ByteBuffer byteBuffer) {
|
|
byteBuffer.putShort((short) invalid);
|
|
}
|
|
}
|