public class HandoverRequestRecordDecoder implements WellKnownRecordPayloadDecoder {
@Override
public WellKnownRecord decodePayload(byte[] payload, NdefMessageDecoder messageDecoder) {
HandoverRequestRecord handoverRequestRecord = new HandoverRequestRecord();
byte minorVersion = (byte)(payload[0] & 0x0F);
byte majorVersion = (byte)((payload[0] >> 4) & 0x0F);
handoverRequestRecord.setMinorVersion(minorVersion);
handoverRequestRecord.setMajorVersion(majorVersion);
List<Record> records = messageDecoder.decodeToRecords(payload, 1, payload.length - 1);
if (records.isEmpty()) {
throw new IllegalArgumentException(
"Expected collision resolution record and at least one alternative carrier");
}
for (int i = 0; i < records.size(); i++) {
Record record = records.get(i);
if (record instanceof CollisionResolutionRecord) {
handoverRequestRecord.setCollisionResolution((CollisionResolutionRecord)record);
}
else if (record instanceof AlternativeCarrierRecord)
handoverRequestRecord.add((AlternativeCarrierRecord)records.get(i));
// An implementation SHALL silently ignore and SHALL NOT raise an error
// if it encounters other unknown record types.
}
if (handoverRequestRecord.getAlternativeCarriers().size() == 0)
throw new IllegalArgumentException("Expected at least one alternative carrier");
return handoverRequestRecord;
}