
ru.ilb.common.jpa.converters.UUIDConverterBytes Maven / Gradle / Ivy
/*
* Copyright 2016 Bystrobank
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ru.ilb.common.jpa.converters;
import java.nio.ByteBuffer;
import java.util.UUID;
import javax.persistence.AttributeConverter;
import javax.persistence.Converter;
/**
*
* @author slavb
*/
@Converter
public class UUIDConverterBytes implements AttributeConverter {
@Override
public byte[] convertToDatabaseColumn(UUID uuid) {
if(uuid==null){
return null;
}
ByteBuffer bb = ByteBuffer.wrap(new byte[16]);
bb.putLong(uuid.getMostSignificantBits());
bb.putLong(uuid.getLeastSignificantBits());
return bb.array();
}
@Override
public UUID convertToEntityAttribute(byte[] bytes) {
if(bytes==null || bytes.length==0){
return null;
}
if (bytes.length != 16) {
throw new IllegalArgumentException();
}
int i = 0;
long msl = 0;
for (; i < 8; i++) {
msl = (msl << 8) | (bytes[i] & 0xFF);
}
long lsl = 0;
for (; i < 16; i++) {
lsl = (lsl << 8) | (bytes[i] & 0xFF);
}
return new UUID(msl, lsl);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy