Code reformat in CompressedObjectStrategy

This commit is contained in:
Charles Allen 2014-11-07 11:19:32 -08:00
parent 8bd6cf0d07
commit 7131cde710
1 changed files with 62 additions and 33 deletions

View File

@ -38,13 +38,14 @@ import java.nio.ByteOrder;
import java.util.Map; import java.util.Map;
/** /**
*/ */
public class CompressedObjectStrategy<T extends Buffer> implements ObjectStrategy<ResourceHolder<T>> public class CompressedObjectStrategy<T extends Buffer> implements ObjectStrategy<ResourceHolder<T>>
{ {
public static final CompressionStrategy DEFAULT_COMPRESSION_STRATEGY = CompressionStrategy.LZ4; public static final CompressionStrategy DEFAULT_COMPRESSION_STRATEGY = CompressionStrategy.LZ4;
public static enum CompressionStrategy { public static enum CompressionStrategy
LZF ((byte)0x0) {
LZF((byte) 0x0)
{ {
@Override @Override
public Decompressor getDecompressor() public Decompressor getDecompressor()
@ -59,34 +60,39 @@ public class CompressedObjectStrategy<T extends Buffer> implements ObjectStrateg
} }
}, },
LZ4 ((byte)0x1) { LZ4((byte) 0x1)
@Override {
public Decompressor getDecompressor() @Override
{ public Decompressor getDecompressor()
return LZ4Decompressor.defaultDecompressor; {
} return LZ4Decompressor.defaultDecompressor;
}
@Override @Override
public Compressor getCompressor() public Compressor getCompressor()
{ {
return LZ4Compressor.defaultCompressor; return LZ4Compressor.defaultCompressor;
} }
}, },
UNCOMPRESSED((byte)0x2){ UNCOMPRESSED((byte) 0x2)
@Override {
public Decompressor getDecompressor(){ @Override
return UncompressedDecompressor.defaultDecompressor; public Decompressor getDecompressor()
} {
@Override return UncompressedDecompressor.defaultDecompressor;
public Compressor getCompressor(){ }
return UncompressedCompressor.defaultCompressor;
} @Override
} public Compressor getCompressor()
; {
return UncompressedCompressor.defaultCompressor;
}
};
final byte id; final byte id;
CompressionStrategy(byte id) { CompressionStrategy(byte id)
{
this.id = id; this.id = id;
} }
@ -94,12 +100,17 @@ public class CompressedObjectStrategy<T extends Buffer> implements ObjectStrateg
{ {
return id; return id;
} }
public abstract Compressor getCompressor(); public abstract Compressor getCompressor();
public abstract Decompressor getDecompressor(); public abstract Decompressor getDecompressor();
static final Map<Byte, CompressionStrategy> idMap = Maps.newHashMap(); static final Map<Byte, CompressionStrategy> idMap = Maps.newHashMap();
static { static {
for(CompressionStrategy strategy : CompressionStrategy.values()) idMap.put(strategy.getId(), strategy); for (CompressionStrategy strategy : CompressionStrategy.values()) {
idMap.put(strategy.getId(), strategy);
}
} }
public static CompressionStrategy forId(byte id) public static CompressionStrategy forId(byte id)
@ -118,6 +129,7 @@ public class CompressedObjectStrategy<T extends Buffer> implements ObjectStrateg
* @param out * @param out
*/ */
public void decompress(ByteBuffer in, int numBytes, ByteBuffer out); public void decompress(ByteBuffer in, int numBytes, ByteBuffer out);
public void decompress(ByteBuffer in, int numBytes, ByteBuffer out, int decompressedSize); public void decompress(ByteBuffer in, int numBytes, ByteBuffer out, int decompressedSize);
} }
@ -127,21 +139,30 @@ public class CompressedObjectStrategy<T extends Buffer> implements ObjectStrateg
* Currently assumes buf is an array backed ByteBuffer * Currently assumes buf is an array backed ByteBuffer
* *
* @param bytes * @param bytes
*
* @return * @return
*/ */
public byte[] compress(byte[] bytes); public byte[] compress(byte[] bytes);
} }
public static class UncompressedCompressor implements Compressor{
public static class UncompressedCompressor implements Compressor
{
private static final UncompressedCompressor defaultCompressor = new UncompressedCompressor(); private static final UncompressedCompressor defaultCompressor = new UncompressedCompressor();
@Override @Override
public byte[] compress(byte[] bytes) { public byte[] compress(byte[] bytes)
{
return bytes; return bytes;
} }
} }
public static class UncompressedDecompressor implements Decompressor{
public static class UncompressedDecompressor implements Decompressor
{
private static final UncompressedDecompressor defaultDecompressor = new UncompressedDecompressor(); private static final UncompressedDecompressor defaultDecompressor = new UncompressedDecompressor();
@Override @Override
public void decompress(ByteBuffer in, int numBytes, ByteBuffer out) { public void decompress(ByteBuffer in, int numBytes, ByteBuffer out)
{
final int maxCopy = Math.min(numBytes, out.remaining()); final int maxCopy = Math.min(numBytes, out.remaining());
final ByteBuffer copyBuffer = in.duplicate(); final ByteBuffer copyBuffer = in.duplicate();
copyBuffer.limit(copyBuffer.position() + maxCopy); copyBuffer.limit(copyBuffer.position() + maxCopy);
@ -151,8 +172,10 @@ public class CompressedObjectStrategy<T extends Buffer> implements ObjectStrateg
out.flip(); out.flip();
in.position(in.position() + maxCopy); in.position(in.position() + maxCopy);
} }
@Override @Override
public void decompress(ByteBuffer in, int numBytes, ByteBuffer out, int decompressedSize) { public void decompress(ByteBuffer in, int numBytes, ByteBuffer out, int decompressedSize)
{
decompress(in, numBytes, out); decompress(in, numBytes, out);
} }
} }
@ -160,6 +183,7 @@ public class CompressedObjectStrategy<T extends Buffer> implements ObjectStrateg
public static class LZFDecompressor implements Decompressor public static class LZFDecompressor implements Decompressor
{ {
private static final LZFDecompressor defaultDecompressor = new LZFDecompressor(); private static final LZFDecompressor defaultDecompressor = new LZFDecompressor();
@Override @Override
public void decompress(ByteBuffer in, int numBytes, ByteBuffer out) public void decompress(ByteBuffer in, int numBytes, ByteBuffer out)
{ {
@ -187,6 +211,7 @@ public class CompressedObjectStrategy<T extends Buffer> implements ObjectStrateg
public static class LZFCompressor implements Compressor public static class LZFCompressor implements Compressor
{ {
private static final LZFCompressor defaultCompressor = new LZFCompressor(); private static final LZFCompressor defaultCompressor = new LZFCompressor();
@Override @Override
public byte[] compress(byte[] bytes) public byte[] compress(byte[] bytes)
{ {
@ -203,6 +228,7 @@ public class CompressedObjectStrategy<T extends Buffer> implements ObjectStrateg
private static final LZ4SafeDecompressor lz4Safe = LZ4Factory.fastestJavaInstance().safeDecompressor(); private static final LZ4SafeDecompressor lz4Safe = LZ4Factory.fastestJavaInstance().safeDecompressor();
private static final LZ4FastDecompressor lz4Fast = LZ4Factory.fastestJavaInstance().fastDecompressor(); private static final LZ4FastDecompressor lz4Fast = LZ4Factory.fastestJavaInstance().fastDecompressor();
private static final LZ4Decompressor defaultDecompressor = new LZ4Decompressor(); private static final LZ4Decompressor defaultDecompressor = new LZ4Decompressor();
@Override @Override
public void decompress(ByteBuffer in, int numBytes, ByteBuffer out) public void decompress(ByteBuffer in, int numBytes, ByteBuffer out)
{ {
@ -334,8 +360,11 @@ public class CompressedObjectStrategy<T extends Buffer> implements ObjectStrateg
public static interface BufferConverter<T> public static interface BufferConverter<T>
{ {
public T convert(ByteBuffer buf); public T convert(ByteBuffer buf);
public int compare(T lhs, T rhs); public int compare(T lhs, T rhs);
public int sizeOf(int count); public int sizeOf(int count);
public T combine(ByteBuffer into, T from); public T combine(ByteBuffer into, T from);
} }
} }