com.netflix.astyanax.serializers.AbstractSerializer Maven / Gradle / Ivy
package com.netflix.astyanax.serializers;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.netflix.astyanax.Serializer;
/**
* A base class for serializer implementations. Takes care of the default
* implementations of to/fromBytesList and to/fromBytesMap. Extenders of this
* class only need to implement toByteBuffer and fromByteBuffer.
*
* @author Ed Anuff
*
* @param
*/
public abstract class AbstractSerializer implements Serializer {
@Override
public abstract ByteBuffer toByteBuffer(T obj);
@Override
public byte[] toBytes(T obj) {
ByteBuffer bb = toByteBuffer(obj);
byte[] bytes = new byte[bb.remaining()];
bb.get(bytes, 0, bytes.length);
return bytes;
}
@Override
public T fromBytes(byte[] bytes) {
return fromByteBuffer(ByteBuffer.wrap(bytes));
}
/*
* public ByteBuffer toByteBuffer(T obj) { return
* ByteBuffer.wrap(toBytes(obj)); }
*
* public ByteBuffer toByteBuffer(T obj, ByteBuffer byteBuffer, int offset,
* int length) { byteBuffer.put(toBytes(obj), offset, length); return
* byteBuffer; }
*/
@Override
public abstract T fromByteBuffer(ByteBuffer byteBuffer);
/*
* public T fromByteBuffer(ByteBuffer byteBuffer) { return
* fromBytes(byteBuffer.array()); }
*
* public T fromByteBuffer(ByteBuffer byteBuffer, int offset, int length) {
* return fromBytes(Arrays.copyOfRange(byteBuffer.array(), offset, length));
* }
*/
public Set toBytesSet(List list) {
Set bytesList = new HashSet(computeInitialHashSize(list.size()));
for (T s : list) {
bytesList.add(toByteBuffer(s));
}
return bytesList;
}
public List fromBytesSet(Set set) {
List objList = new ArrayList(set.size());
for (ByteBuffer b : set) {
objList.add(fromByteBuffer(b));
}
return objList;
}
public List toBytesList(List list) {
return Lists.transform(list, new Function() {
@Override
public ByteBuffer apply(T s) {
return toByteBuffer(s);
}
});
}
public List toBytesList(Collection list) {
List bytesList = new ArrayList(list.size());
for (T s : list) {
bytesList.add(toByteBuffer(s));
}
return bytesList;
}
public List toBytesList(Iterable list) {
return Lists.newArrayList(Iterables.transform(list, new Function() {
@Override
public ByteBuffer apply(T s) {
return toByteBuffer(s);
}
}));
}
public List fromBytesList(List list) {
List objList = new ArrayList(list.size());
for (ByteBuffer s : list) {
objList.add(fromByteBuffer(s));
}
return objList;
}
public Map toBytesMap(Map map) {
Map bytesMap = new LinkedHashMap(computeInitialHashSize(map.size()));
for (Entry entry : map.entrySet()) {
bytesMap.put(toByteBuffer(entry.getKey()), entry.getValue());
}
return bytesMap;
}
public Map fromBytesMap(Map map) {
Map objMap = new LinkedHashMap(computeInitialHashSize(map.size()));
for (Entry entry : map.entrySet()) {
objMap.put(fromByteBuffer(entry.getKey()), entry.getValue());
}
return objMap;
}
public int computeInitialHashSize(int initialSize) {
return Double.valueOf(Math.floor(initialSize / 0.75)).intValue() + 1;
}
public ComparatorType getComparatorType() {
return ComparatorType.BYTESTYPE;
}
@Override
public ByteBuffer fromString(String string) {
throw new UnsupportedOperationException(this.getClass().getCanonicalName());
}
@Override
public String getString(ByteBuffer byteBuffer) {
throw new UnsupportedOperationException(this.getClass().getCanonicalName());
}
@Override
public ByteBuffer getNext(ByteBuffer byteBuffer) {
ByteBufferOutputStream next = new ByteBufferOutputStream();
try {
next.write(byteBuffer);
next.write((byte) 0);
return next.getByteBuffer();
}
finally {
try {
next.close();
}
catch (Throwable t) {
}
}
}
}