Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions src/main/java/com/bc/zarr/CompressorFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,25 @@

public class CompressorFactory {

private static final Map<String, Class<? extends Compressor>> registeredCompressors = defaultCompressors();

private static Map<String, Class<? extends Compressor>> defaultCompressors() {
Map<String, Class<? extends Compressor>> compressors = new HashMap<String, Class<? extends Compressor>>();
compressors.put("null", NullCompressor.class);
compressors.put("zlib", ZlibCompressor.class);
compressors.put("blosc", BloscCompressor.class);
return compressors;
}

public final static Compressor nullCompressor = new NullCompressor();


public static void registerCompressor(String id, Class<? extends Compressor> compressor) {
if (id != null && !registeredCompressors.containsKey(id)) {
registeredCompressors.put(id, compressor);
}
}

/**
* @return the properties of the default compressor as a key/value map.
*/
Expand Down Expand Up @@ -111,14 +128,20 @@ public static Compressor create(String id, Object... keyValuePair) {
* @throws IllegalArgumentException If it is not able to create a Compressor.
*/
public static Compressor create(String id, Map<String, Object> properties) {
if ("null".equals(id)) {
return nullCompressor;
}
if ("zlib".equals(id)) {
return new ZlibCompressor(properties);
try {
if (registeredCompressors.containsKey(id)) {
Class<? extends Compressor> c = registeredCompressors.get(id);
if (c.equals(NullCompressor.class)) {
return nullCompressor;
}
return c.getDeclaredConstructor(Map.class).newInstance(properties);
}
}
if ("blosc".equals(id)) {
return new BloscCompressor(properties);
catch (ReflectiveOperationException e) {
if (e.getCause() instanceof IllegalArgumentException) {
throw (IllegalArgumentException) e.getCause();
}
throw new IllegalArgumentException("Could not get compressor for id:'" + id + "'", e);
}
throw new IllegalArgumentException("Compressor id:'" + id + "' not supported.");
}
Expand Down Expand Up @@ -159,7 +182,7 @@ public void uncompress(InputStream is, OutputStream os) throws IOException {
private static class ZlibCompressor extends Compressor {
private final int level;

private ZlibCompressor(Map<String, Object> map) {
protected ZlibCompressor(Map<String, Object> map) {
final Object levelObj = map.get("level");
if (levelObj == null) {
this.level = 1; //default value
Expand Down Expand Up @@ -242,7 +265,7 @@ public static class BloscCompressor extends Compressor {
private final String cname;
private final int nthreads;

private BloscCompressor(Map<String, Object> map) {
protected BloscCompressor(Map<String, Object> map) {
final Object cnameObj = map.get(keyCname);
if (cnameObj == null) {
cname = defaultCname;
Expand Down
33 changes: 32 additions & 1 deletion src/test/java/com/bc/zarr/CompressorFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@

import org.junit.Test;

import java.io.InputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;

import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -119,4 +122,32 @@ public void create_compressor_not_supported() {
assertEquals("Compressor id:'kkkkkkk' not supported.", expected.getMessage());
}
}
}

@Test
public void registerNewCompressor() {
final String id = "test";
CompressorFactory.registerCompressor(id, TestCompressor.class);
final Compressor compressor = CompressorFactory.create(id, TestUtils.createMap("level", 1));
assertNotNull(compressor);
assertEquals(id, compressor.getId());
}

static class TestCompressor extends Compressor {
public TestCompressor(Map<String, Object> properties) {
}

public String getId() {
return "test";
}

public String toString() {
return getId();
}

public void compress(InputStream is, OutputStream os) throws IOException {
}

public void uncompress(InputStream is, OutputStream os) throws IOException {
}
}
}