Skip to content

Instantly share code, notes, and snippets.

@electrum
Created November 5, 2014 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save electrum/fca9f373ce1455d0655d to your computer and use it in GitHub Desktop.
Save electrum/fca9f373ce1455d0655d to your computer and use it in GitHub Desktop.
/*
* 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 jdbi.util;
import org.skife.jdbi.v2.ResultSetMapperFactory;
import org.skife.jdbi.v2.StatementContext;
import org.skife.jdbi.v2.tweak.Argument;
import org.skife.jdbi.v2.tweak.ArgumentFactory;
import org.skife.jdbi.v2.tweak.ResultSetMapper;
import org.skife.jdbi.v2.util.TypedMapper;
import java.nio.ByteBuffer;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.UUID;
import static com.google.common.base.Preconditions.checkNotNull;
public final class UuidUtil
{
private UuidUtil() {}
public static final class UuidArgumentFactory
implements ArgumentFactory<UUID>
{
@Override
public boolean accepts(Class<?> expectedType, Object value, StatementContext ctx)
{
return value instanceof UUID;
}
@Override
public Argument build(Class<?> expectedType, UUID value, StatementContext ctx)
{
return new UuidArgument(value);
}
}
public static final class UuidArgument
implements Argument
{
private final byte[] value;
public UuidArgument(UUID value)
{
this.value = uuidToBytes(checkNotNull(value, "value is null"));
}
@Override
public void apply(int position, PreparedStatement statement, StatementContext ctx)
throws SQLException
{
statement.setBytes(position, value);
}
}
@SuppressWarnings("rawtypes")
public static class UuidMapperFactory
implements ResultSetMapperFactory
{
@Override
public boolean accepts(Class type, StatementContext ctx)
{
return type == UUID.class;
}
@Override
public ResultSetMapper mapperFor(Class type, StatementContext ctx)
{
return new UuidMapper();
}
}
public static final class UuidMapper
extends TypedMapper<UUID>
{
@Override
protected UUID extractByName(ResultSet r, String name)
throws SQLException
{
return uuidFromBytes(r.getBytes(name));
}
@Override
protected UUID extractByIndex(ResultSet r, int index)
throws SQLException
{
return uuidFromBytes(r.getBytes(index));
}
}
public static UUID uuidFromBytes(byte[] bytes)
{
ByteBuffer buffer = ByteBuffer.wrap(bytes);
long msb = buffer.getLong();
long lsb = buffer.getLong();
return new UUID(msb, lsb);
}
public static byte[] uuidToBytes(UUID uuid)
{
return ByteBuffer.allocate(16)
.putLong(uuid.getMostSignificantBits())
.putLong(uuid.getLeastSignificantBits())
.array();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment