/** * Returns the binary as a UUID. The binary type must be 4. * * @return the uuid * @since 3.9 */ public UUID asUuid() { if (!BsonBinarySubType.isUuid(type)) { throw new BsonInvalidOperationException("type must be a UUID subtype."); } if (type != BsonBinarySubType.UUID_STANDARD.getValue()) { throw new BsonInvalidOperationException("uuidRepresentation must be set to return the correct UUID."); } return UuidHelper.decodeBinaryToUuid(this.data.clone(), this.type, UuidRepresentation.STANDARD); }
@Override public UUID decode(final BsonReader reader, final DecoderContext decoderContext) { byte subType = reader.peekBinarySubType(); if (subType != BsonBinarySubType.UUID_LEGACY.getValue() && subType != BsonBinarySubType.UUID_STANDARD.getValue()) { throw new BSONException("Unexpected BsonBinarySubType"); } byte[] bytes = reader.readBinaryData().getData(); return UuidHelper.decodeBinaryToUuid(bytes, subType, decoderUuidRepresentation); }
/** * Returns the binary as a UUID. * * @param uuidRepresentation the UUID representation * @return the uuid * @since 3.9 */ public UUID asUuid(final UuidRepresentation uuidRepresentation) { Assertions.notNull("uuidRepresentation", uuidRepresentation); final byte uuidType = uuidRepresentation == UuidRepresentation.STANDARD ? BsonBinarySubType.UUID_STANDARD.getValue() : BsonBinarySubType.UUID_LEGACY.getValue(); if (type != uuidType) { throw new BsonInvalidOperationException("uuidRepresentation does not match current uuidRepresentation."); } return UuidHelper.decodeBinaryToUuid(data.clone(), type, uuidRepresentation); }