// 
// Decompiled by Procyon v0.6.0
// 

package org.bson;

import java.util.Arrays;
import org.bson.assertions.Assertions;
import org.bson.internal.UuidHelper;
import java.util.UUID;

public class BsonBinary extends BsonValue
{
    private final byte type;
    private final byte[] data;
    
    public BsonBinary(final byte[] data) {
        this(BsonBinarySubType.BINARY, data);
    }
    
    public BsonBinary(final BsonBinarySubType type, final byte[] data) {
        if (type == null) {
            throw new IllegalArgumentException("type may not be null");
        }
        if (data == null) {
            throw new IllegalArgumentException("data may not be null");
        }
        this.type = type.getValue();
        this.data = data;
    }
    
    public BsonBinary(final byte type, final byte[] data) {
        if (data == null) {
            throw new IllegalArgumentException("data may not be null");
        }
        this.type = type;
        this.data = data;
    }
    
    public BsonBinary(final UUID uuid) {
        this(uuid, UuidRepresentation.STANDARD);
    }
    
    public BsonBinary(final UUID uuid, final UuidRepresentation uuidRepresentation) {
        if (uuid == null) {
            throw new IllegalArgumentException("uuid may not be null");
        }
        if (uuidRepresentation == null) {
            throw new IllegalArgumentException("uuidRepresentation may not be null");
        }
        this.data = UuidHelper.encodeUuidToBinary(uuid, uuidRepresentation);
        this.type = ((uuidRepresentation == UuidRepresentation.STANDARD) ? BsonBinarySubType.UUID_STANDARD.getValue() : BsonBinarySubType.UUID_LEGACY.getValue());
    }
    
    public UUID asUuid() {
        if (!BsonBinarySubType.isUuid(this.type)) {
            throw new BsonInvalidOperationException("type must be a UUID subtype.");
        }
        if (this.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);
    }
    
    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 (this.type != uuidType) {
            throw new BsonInvalidOperationException("uuidRepresentation does not match current uuidRepresentation.");
        }
        return UuidHelper.decodeBinaryToUuid(this.data.clone(), this.type, uuidRepresentation);
    }
    
    @Override
    public BsonType getBsonType() {
        return BsonType.BINARY;
    }
    
    public byte getType() {
        return this.type;
    }
    
    public byte[] getData() {
        return this.data;
    }
    
    @Override
    public boolean equals(final Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || this.getClass() != o.getClass()) {
            return false;
        }
        final BsonBinary that = (BsonBinary)o;
        return Arrays.equals(this.data, that.data) && this.type == that.type;
    }
    
    @Override
    public int hashCode() {
        int result = this.type;
        result = 31 * result + Arrays.hashCode(this.data);
        return result;
    }
    
    @Override
    public String toString() {
        return "BsonBinary{type=" + this.type + ", data=" + Arrays.toString(this.data) + '}';
    }
    
    static BsonBinary clone(final BsonBinary from) {
        return new BsonBinary(from.type, from.data.clone());
    }
}
