Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the usage of raw types in BeaconParser / BeaconParserFactory. #43

Merged
merged 1 commit into from
Jun 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions luch/src/main/java/aga/android/luch/Region.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package aga.android.luch;

import androidx.annotation.NonNull;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import androidx.annotation.NonNull;

import static java.lang.String.format;

public class Region {

private List regionFields = new ArrayList();
private List<Object> regionFields = new ArrayList<>();

private Region(@NonNull List regionFields) {
//noinspection unchecked
private Region(@NonNull List<Object> regionFields) {
this.regionFields.addAll(regionFields);
}

Expand All @@ -28,14 +27,13 @@ public Object getFieldAt(int i) {
@SuppressWarnings("WeakerAccess")
public static class Builder {

private List regionFields = new ArrayList();
private List<Object> regionFields = new ArrayList<>();

public Builder() {

}

public Builder addNullField() {
//noinspection unchecked
regionFields.add(null);
return this;
}
Expand All @@ -49,19 +47,17 @@ public Builder addIntegerField(int field) {
)
);
}
//noinspection unchecked

regionFields.add(field);
return this;
}

public Builder addUuidField(@NonNull UUID field) {
//noinspection unchecked
regionFields.add(field);
return this;
}

public Builder addByteField(byte field) {
//noinspection unchecked
regionFields.add(field);
return this;
}
Expand Down
23 changes: 11 additions & 12 deletions luch/src/main/java/aga/android/luch/parsers/BeaconParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import android.bluetooth.le.ScanResult;
import android.util.SparseArray;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
Expand All @@ -12,16 +15,14 @@
import aga.android.luch.BeaconLogger;
import aga.android.luch.Region;
import aga.android.luch.distance.AbstractDistanceCalculator;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import static aga.android.luch.parsers.Conversions.asByteArray;
import static aga.android.luch.parsers.Conversions.byteArrayToHexString;
import static java.lang.String.format;

final class BeaconParser implements IBeaconParser {

private final List<IFieldConverter> fieldConverters = new ArrayList<>();
private final List<IFieldConverter<?>> fieldConverters = new ArrayList<>();

private final Object beaconType;

Expand All @@ -31,7 +32,7 @@ final class BeaconParser implements IBeaconParser {

private final AbstractDistanceCalculator distanceCalculator;

BeaconParser(@NonNull List<? extends IFieldConverter> fieldConverters,
BeaconParser(@NonNull List<IFieldConverter<?>> fieldConverters,
@Nullable AbstractDistanceCalculator distanceCalculator,
int beaconTypePosition,
int manufacturerId,
Expand Down Expand Up @@ -60,13 +61,12 @@ public Beacon parse(@NonNull ScanResult scanResult) {
final List<Byte> bytesList = Conversions.asList(rawBytes);

try {
final List identifiers = new ArrayList();
final List<Object> identifiers = new ArrayList<>();

for (int j = 0; j < fieldConverters.size(); j++) {
final IFieldConverter converter = fieldConverters.get(j);
final IFieldConverter<?> converter = fieldConverters.get(j);

final Object parsedField = converter.consume(bytesList);
//noinspection unchecked
identifiers.add(parsedField);

if (j == beaconTypePosition && !beaconType.equals(parsedField)) {
Expand Down Expand Up @@ -166,7 +166,7 @@ private List<Byte> getMask(@NonNull Region region,
final List<Byte> data = new ArrayList<>();

for (int i = 0; i < fieldConverters.size(); i++) {
final IFieldConverter parser = fieldConverters.get(i);
final IFieldConverter<?> parser = fieldConverters.get(i);
final Object field = region.getFieldAt(i);

byteProducer.produce(data, field, i, parser);
Expand All @@ -180,20 +180,19 @@ private interface ByteProducer {
void produce(List<Byte> packet,
Object field,
int position,
IFieldConverter parser) throws RegionConversionException;
IFieldConverter<?> parser) throws RegionConversionException;
}

private static final ByteProducer FILTER_PRODUCER = new ByteProducer() {
@Override
public void produce(List<Byte> packet,
Object field,
int position,
IFieldConverter parser) throws RegionConversionException {
IFieldConverter<?> parser) throws RegionConversionException {

if (field == null) {
parser.insertMask(packet, (byte) 0x00);
} else if (parser.canParse(field.getClass())) {
//noinspection unchecked
parser.insert(packet, field);
} else {
throw new RegionConversionException(
Expand All @@ -210,7 +209,7 @@ public void produce(List<Byte> packet,
public void produce(List<Byte> packet,
Object field,
int position,
IFieldConverter parser) throws RegionConversionException {
IFieldConverter<?> parser) throws RegionConversionException {

final byte maskByte;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package aga.android.luch.parsers;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import aga.android.luch.distance.AbstractDistanceCalculator;
import aga.android.luch.distance.DistanceCalculatorFactory;
import androidx.annotation.NonNull;

import static java.lang.Integer.parseInt;
import static java.lang.String.format;
Expand All @@ -17,11 +19,11 @@ public final class BeaconParserFactory {

private static final List<String> SUPPORTED_PREFIXES = asList("m", "i", "p", "d");

private static final List<? extends IFieldConverter> SUPPORTED_CONVERTERS = asList(
private static final IFieldConverter<?>[] SUPPORTED_CONVERTERS = new IFieldConverter[] {
new SingleByteFieldConverter(),
new IntegerFieldConverter(),
new UuidFieldConverter()
);
};

private BeaconParserFactory() {
// no instances please
Expand All @@ -41,7 +43,7 @@ public static IBeaconParser createFromLayout(@NonNull String beaconLayout) {
public static IBeaconParser createFromLayout(@NonNull String beaconLayout,
int manufacturerId) {

final List<IFieldConverter> converters = new ArrayList<>();
final List<IFieldConverter<?>> converters = new ArrayList<>();

final String[] tokens = beaconLayout.split(",");

Expand All @@ -54,7 +56,7 @@ public static IBeaconParser createFromLayout(@NonNull String beaconLayout,
for (int i = 0; i < tokens.length; i++) {
final String token = tokens[i];
final String fieldPrefix = getFieldPrefix(token);
final IFieldConverter converter = getSuitableConverter(token);
final IFieldConverter<?> converter = getSuitableConverter(token);

if (converter == null) {
throw new IllegalArgumentException(
Expand Down Expand Up @@ -110,7 +112,8 @@ private static String getFieldPrefix(@NonNull String token) {
return fieldPrefix;
}

private static IFieldConverter getSuitableConverter(String token) {
@Nullable
private static IFieldConverter<?> getSuitableConverter(String token) {

final String[] fieldRange;
final int length;
Expand All @@ -131,15 +134,12 @@ private static IFieldConverter getSuitableConverter(String token) {
);
}

IFieldConverter suitableConverter = null;

for (IFieldConverter converter : SUPPORTED_CONVERTERS) {
for (IFieldConverter<?> converter : SUPPORTED_CONVERTERS) {
if (converter.canParse(length)) {
suitableConverter = converter;
break;
return converter;
}
}

return suitableConverter;
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package aga.android.luch.parsers;

import java.util.List;

import androidx.annotation.NonNull;

import java.util.List;

interface IFieldConverter<T> {

/**
Expand All @@ -26,7 +26,7 @@ interface IFieldConverter<T> {
* @param packet the list of bytes to append the aforementioned bytes to
* @param value the value to be converted into a sequence of {@link Byte}s
*/
void insert(@NonNull List<Byte> packet, @NonNull T value);
void insert(@NonNull List<Byte> packet, @NonNull Object value);

/**
* Appends the mask bits (either 0 or 1) to the given mask packet. The amount of bytes to be
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package aga.android.luch.parsers;

import java.util.List;

import androidx.annotation.NonNull;

import java.util.List;

class IntegerFieldConverter implements IFieldConverter<Integer> {

@Override
Expand All @@ -15,9 +15,9 @@ public Integer consume(@NonNull List<Byte> packet) {
}

@Override
public void insert(@NonNull List<Byte> packet, @NonNull Integer value) {
packet.add((byte) (value / 256));
packet.add((byte) (value / 256));
public void insert(@NonNull List<Byte> packet, @NonNull Object value) {
packet.add((byte) (((int) value) / 256));
packet.add((byte) (((int) value) / 256));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package aga.android.luch.parsers;

import java.util.List;

import androidx.annotation.NonNull;

import java.util.List;

class SingleByteFieldConverter implements IFieldConverter<Byte> {

@Override
Expand All @@ -12,8 +12,8 @@ public Byte consume(@NonNull List<Byte> packet) {
}

@Override
public void insert(@NonNull List<Byte> packet, @NonNull Byte value) {
packet.add(value);
public void insert(@NonNull List<Byte> packet, @NonNull Object value) {
packet.add(((byte) value));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package aga.android.luch.parsers;

import androidx.annotation.NonNull;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.List;
import java.util.UUID;

import androidx.annotation.NonNull;

class UuidFieldConverter implements IFieldConverter<UUID> {

private static final int UUID_BYTE_SIZE = 16;
Expand All @@ -25,12 +25,14 @@ public UUID consume(@NonNull List<Byte> packet) {
}

@Override
public void insert(@NonNull List<Byte> packet, @NonNull UUID value) {
public void insert(@NonNull List<Byte> packet, @NonNull Object value) {
final UUID uuid = (UUID) value;

ByteBuffer
.wrap(bytes)
.order(ByteOrder.BIG_ENDIAN)
.putLong(value.getMostSignificantBits())
.putLong(value.getLeastSignificantBits());
.putLong(uuid.getMostSignificantBits())
.putLong(uuid.getLeastSignificantBits());

for (int i = 0; i < UUID_BYTE_SIZE; i++) {
packet.add(bytes[i]);
Expand Down