Mercurial > stress-tester
changeset 750:29f44148f245
ValueCoderResolver introduced
author | Devel 1 |
---|---|
date | Wed, 13 Dec 2017 11:27:42 +0100 |
parents | 8d1fa0394251 |
children | da1ac89b3029 |
files | stress-tester/src/main/java/com/passus/st/reader/nc/option/DefaultValueCoder.java stress-tester/src/main/java/com/passus/st/reader/nc/option/DefaultValueCoderResolver.java stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoderResolver.java stress-tester/src/test/java/com/passus/st/reader/nc/option/DefaultValueCoderTest.java stress-tester/src/test/java/com/passus/st/reader/nc/option/OptionsListCoderTest.java |
diffstat | 5 files changed, 126 insertions(+), 89 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/main/java/com/passus/st/reader/nc/option/DefaultValueCoder.java Tue Dec 12 13:51:07 2017 +0100 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/DefaultValueCoder.java Wed Dec 13 11:27:42 2017 +0100 @@ -1,9 +1,6 @@ package com.passus.st.reader.nc.option; import com.passus.data.ByteBuff; -import com.passus.data.ByteString; -import java.util.HashMap; -import java.util.Map; /** * @@ -11,87 +8,22 @@ */ public class DefaultValueCoder implements ValueCoder { - private static final int SUPPORTED_CODES = 64; - private static final Map<Class, Byte> CLASS_TO_CODE = new HashMap<>(); - private static final ValueCoder[] CODE_TO_CODER = new ValueCoder[SUPPORTED_CODES]; - - private static final ValueCoder NULL_CODER = new NullCoder(); - public static final DefaultValueCoder INSTANCE = new DefaultValueCoder(); - static final byte NULL_CODE = 0; - static final byte INT_CODE = 1; - static final byte LONG_CODE = 2; - static final byte STRING_CODE = 11; - static final byte BYTE_STRING_CODE = 12; - - static { - CODE_TO_CODER[NULL_CODE] = NULL_CODER; - addCoder(INT_CODE, Integer.class, ValueCoders.INT_CODER); - addCoder(LONG_CODE, Long.class, ValueCoders.LONG_CODER); - addCoder(STRING_CODE, String.class, ValueCoders.STRING_NT_CODER); - addCoder(BYTE_STRING_CODE, ByteString.class, ValueCoders.BYTE_STRING_NT_CODER); - } - - private static void addCoder(byte code, Class cls, ValueCoder coder) { - if (code == 0 || code >= SUPPORTED_CODES) { - throw new IllegalArgumentException("Value code must be in range 1-" + (SUPPORTED_CODES - 1)); - } - if (CODE_TO_CODER[code] != null) { - throw new IllegalArgumentException("Value code used already: " + code); - } - if (CLASS_TO_CODE.containsKey(cls)) { - throw new IllegalArgumentException("Value class used already: " + cls.getSimpleName()); - } - - CLASS_TO_CODE.put(cls, code); - CODE_TO_CODER[code] = coder; - } - - private static byte findCode(Object obj) { - if (obj == null) { - return NULL_CODE; - } - - Class cls = obj.getClass(); - Byte code = CLASS_TO_CODE.get(cls); - if (code != null) { - return code; - } - - for (Map.Entry<Class, Byte> e : CLASS_TO_CODE.entrySet()) { - if (e.getKey().isAssignableFrom(cls)) { - return e.getValue(); - } - } - - throw new IllegalArgumentException("Cannot encode object of type: " + cls.getSimpleName()); - } + private static final ValueCoderResolver RESOLVER = new DefaultValueCoderResolver(); @Override public void encode(Object value, ByteBuff buff) { - byte code = findCode(value); + byte code = RESOLVER.findCode(value); buff.append(code); - CODE_TO_CODER[code].encode(value, buff); + ValueCoder coder = RESOLVER.codeToCoder(code); + coder.encode(value, buff); } @Override public Object decode(ByteBuff buff) { byte code = buff.read(); - ValueCoder coder = CODE_TO_CODER[code]; + ValueCoder coder = RESOLVER.codeToCoder(code); return coder.decode(buff); } - - private static class NullCoder implements ValueCoder<Object> { - - @Override - public void encode(Object value, ByteBuff buff) { - } - - @Override - public Object decode(ByteBuff buff) { - return null; - } - - } }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/DefaultValueCoderResolver.java Wed Dec 13 11:27:42 2017 +0100 @@ -0,0 +1,88 @@ +package com.passus.st.reader.nc.option; + +import com.passus.data.ByteBuff; +import com.passus.data.ByteString; +import java.util.HashMap; +import java.util.Map; + +/** + * + * @author mikolaj.podbielski + */ +public class DefaultValueCoderResolver implements ValueCoderResolver { + + private static final int SUPPORTED_CODES = 64; + private final Map<Class, Byte> CLASS_TO_CODE = new HashMap<>(); + private final ValueCoder[] CODE_TO_CODER = new ValueCoder[SUPPORTED_CODES]; + + private static final ValueCoder NULL_CODER = new NullCoder(); + + static final byte NULL_CODE = 0; + static final byte INT_CODE = 1; + static final byte LONG_CODE = 2; + static final byte STRING_CODE = 11; + static final byte BYTE_STRING_CODE = 12; + + { + CODE_TO_CODER[NULL_CODE] = NULL_CODER; + addCoder(INT_CODE, Integer.class, ValueCoders.INT_CODER); + addCoder(LONG_CODE, Long.class, ValueCoders.LONG_CODER); + addCoder(STRING_CODE, String.class, ValueCoders.STRING_NT_CODER); + addCoder(BYTE_STRING_CODE, ByteString.class, ValueCoders.BYTE_STRING_NT_CODER); + } + + @Override + public void addCoder(byte code, Class cls, ValueCoder coder) { + if (code == 0 || code >= SUPPORTED_CODES) { + throw new IllegalArgumentException("Value code must be in range 1-" + (SUPPORTED_CODES - 1)); + } + if (CODE_TO_CODER[code] != null) { + throw new IllegalArgumentException("Value code used already: " + code); + } + if (CLASS_TO_CODE.containsKey(cls)) { + throw new IllegalArgumentException("Value class used already: " + cls.getSimpleName()); + } + + CLASS_TO_CODE.put(cls, code); + CODE_TO_CODER[code] = coder; + } + + @Override + public byte findCode(Object obj) { + if (obj == null) { + return NULL_CODE; + } + + Class cls = obj.getClass(); + Byte code = CLASS_TO_CODE.get(cls); + if (code != null) { + return code; + } + + for (Map.Entry<Class, Byte> e : CLASS_TO_CODE.entrySet()) { + if (e.getKey().isAssignableFrom(cls)) { + return e.getValue(); + } + } + + throw new IllegalArgumentException("Cannot encode object of type: " + cls.getSimpleName()); + } + + @Override + public ValueCoder codeToCoder(byte code) { + return CODE_TO_CODER[code]; + } + + private static class NullCoder implements ValueCoder<Object> { + + @Override + public void encode(Object value, ByteBuff buff) { + } + + @Override + public Object decode(ByteBuff buff) { + return null; + } + + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/stress-tester/src/main/java/com/passus/st/reader/nc/option/ValueCoderResolver.java Wed Dec 13 11:27:42 2017 +0100 @@ -0,0 +1,15 @@ +package com.passus.st.reader.nc.option; + +/** + * + * @author mikolaj.podbielski + */ +public interface ValueCoderResolver { + + void addCoder(byte code, Class cls, ValueCoder coder); + + ValueCoder codeToCoder(byte code); + + byte findCode(Object obj); + +}
--- a/stress-tester/src/test/java/com/passus/st/reader/nc/option/DefaultValueCoderTest.java Tue Dec 12 13:51:07 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/reader/nc/option/DefaultValueCoderTest.java Wed Dec 13 11:27:42 2017 +0100 @@ -18,7 +18,7 @@ String s = "abc"; DefaultValueCoder.INSTANCE.encode(s, buff); - byte[] expected = {DefaultValueCoder.STRING_CODE, 'a', 'b', 'c', AsciiUtils.NUL}; + byte[] expected = {DefaultValueCoderResolver.STRING_CODE, 'a', 'b', 'c', AsciiUtils.NUL}; assertEquals(0, buff.startIndex()); assertEquals(5, buff.endIndex()); assertEquals(expected, buff.toArray()); @@ -35,7 +35,7 @@ ByteString bs = ByteString.create("abc"); DefaultValueCoder.INSTANCE.encode(bs, buff); - byte[] expected = {DefaultValueCoder.BYTE_STRING_CODE, 'a', 'b', 'c', AsciiUtils.NUL}; + byte[] expected = {DefaultValueCoderResolver.BYTE_STRING_CODE, 'a', 'b', 'c', AsciiUtils.NUL}; assertEquals(0, buff.startIndex()); assertEquals(5, buff.endIndex()); assertEquals(expected, buff.toArray()); @@ -51,7 +51,7 @@ HeapByteBuff buff = new HeapByteBuff(8); DefaultValueCoder.INSTANCE.encode(null, buff); - byte[] expected = {DefaultValueCoder.NULL_CODE, 0, 0, 0, 0, 0, 0, 0}; + byte[] expected = {DefaultValueCoderResolver.NULL_CODE, 0, 0, 0, 0, 0, 0, 0}; assertEquals(0, buff.startIndex()); assertEquals(1, buff.endIndex()); assertEquals(expected, buff.buffer());
--- a/stress-tester/src/test/java/com/passus/st/reader/nc/option/OptionsListCoderTest.java Tue Dec 12 13:51:07 2017 +0100 +++ b/stress-tester/src/test/java/com/passus/st/reader/nc/option/OptionsListCoderTest.java Wed Dec 13 11:27:42 2017 +0100 @@ -12,11 +12,12 @@ */ public class OptionsListCoderTest { - final byte opCode = 127; - final String opName = "xyz"; - final Option op = new Option(opName, 123); - final List<Option> options = Arrays.asList(op, op); - final OptionsListCoder coder = new OptionsListCoder(); + private final byte opCode = 127; + private final String opName = "xyz"; + private final Option op1 = new Option(opName, 123); + private final Option op2 = new Option(opName, 124); + private final List<Option> options = Arrays.asList(op1, op2); + private final OptionsListCoder coder = new OptionsListCoder(); @Test public void testOptionWithCode() { @@ -28,7 +29,7 @@ byte[] expected = { 2, 127, 0, 0, 0, 123, - 127, 0, 0, 0, 123 + 127, 0, 0, 0, 124 }; assertEquals(expected, result); @@ -48,7 +49,7 @@ byte[] expected = { 2, 0, 'x', 'y', 'z', 0, 0, 0, 0, 123, - 0, 'x', 'y', 'z', 0, 0, 0, 0, 123 + 0, 'x', 'y', 'z', 0, 0, 0, 0, 124 }; assertEquals(expected, result); @@ -65,8 +66,8 @@ byte[] result = buff.toArray(); byte[] expected = { 2, - 0, 'x', 'y', 'z', 0, DefaultValueCoder.INT_CODE, 0, 0, 0, 123, - 0, 'x', 'y', 'z', 0, DefaultValueCoder.INT_CODE, 0, 0, 0, 123 + 0, 'x', 'y', 'z', 0, DefaultValueCoderResolver.INT_CODE, 0, 0, 0, 123, + 0, 'x', 'y', 'z', 0, DefaultValueCoderResolver.INT_CODE, 0, 0, 0, 124 }; assertEquals(expected, result); @@ -74,9 +75,10 @@ } private void checkDecodedOptions(HeapByteBuff buff) { - List<Option> options = coder.decode(buff); - assertEquals(2, options.size()); - assertEquals(opName, options.get(0).getName()); - assertEquals(123, options.get(0).getValue()); + List<Option> decoded = coder.decode(buff); + assertEquals(2, decoded.size()); + assertEquals(opName, decoded.get(0).getName()); + assertEquals(123, decoded.get(0).getValue()); + assertEquals(124, decoded.get(1).getValue()); } }