@Override public IntegerFreqMap deserialiseEmpty() throws SerialisationException { return new IntegerFreqMap(); }
@Override public IntegerFreqMap deserialise(final byte[] bytes) throws SerialisationException { IntegerFreqMap freqMap = new IntegerFreqMap(); if (bytes.length == 0) { return freqMap; } String stringMap; try { stringMap = new String(bytes, CommonConstants.ISO_8859_1_ENCODING); } catch (final UnsupportedEncodingException e) { throw new SerialisationException(e.getMessage(), e); } if (stringMap.isEmpty()) { //No values so return the empty map return freqMap; } String[] keyValues = stringMap.split(SEPERATOR_REGEX); if (keyValues.length % 2 != 0) { throw new SerialisationException("Uneven number of entries found for serialised frequency map, unable to deserialise."); } for (int i = 0; i < keyValues.length - 1; i += 2) { freqMap.put(keyValues[i], Integer.parseInt(keyValues[i + 1])); } return freqMap; }
@Override public void shouldDeserialiseEmpty() throws SerialisationException { assertEquals(new IntegerFreqMap(), serialiser.deserialiseEmpty()); } }
@Override @SuppressWarnings("unchecked") public Pair<IntegerFreqMap, byte[]>[] getHistoricSerialisationPairs() { IntegerFreqMap freqMap = new IntegerFreqMap(); freqMap.put("x", 10); freqMap.put("y", 5); freqMap.put("z", 20); return new Pair[]{ new Pair(freqMap, new byte[]{120, 92, 44, 49, 48, 92, 44, 121, 92, 44, 53, 92, 44, 122, 92, 44, 50, 48}) }; }
@Test public void canSerialiseEmptyFreqMap() throws SerialisationException { byte[] b = serialiser.serialise(new IntegerFreqMap()); Object o = serialiser.deserialise(b); assertEquals(IntegerFreqMap.class, o.getClass()); assertEquals(0, ((IntegerFreqMap) o).size()); }
@Test public void shouldMergeFreqMaps() { // Given final IntegerFreqMapAggregator aggregator = new IntegerFreqMapAggregator(); final IntegerFreqMap freqMap1 = new IntegerFreqMap(); freqMap1.put("1", 2); freqMap1.put("2", 3); final IntegerFreqMap freqMap2 = new IntegerFreqMap(); freqMap2.put("2", 4); freqMap2.put("3", 5); // When final IntegerFreqMap result = aggregator.apply(freqMap1, freqMap2); // Then assertEquals((Integer) 2, result.get("1")); assertEquals((Integer) 7, result.get("2")); assertEquals((Integer) 5, result.get("3")); }
@Test public void canSerialiseDeSerialiseFreqMapWithValues() throws SerialisationException { IntegerFreqMap freqMap = new IntegerFreqMap(); freqMap.put("x", 10); freqMap.put("y", 5); freqMap.put("z", 20); byte[] b = serialiser.serialise(freqMap); IntegerFreqMap o = (IntegerFreqMap) serialiser.deserialise(b); assertEquals(IntegerFreqMap.class, o.getClass()); assertEquals((Integer) 10, o.get("x")); assertEquals((Integer) 5, o.get("y")); assertEquals((Integer) 20, o.get("z")); }
@Test public void testSerialiserWillSkipEntryWithNullValue() throws SerialisationException { IntegerFreqMap freqMap = new IntegerFreqMap(); freqMap.put("x", null); freqMap.put("y", 5); freqMap.put("z", 20); byte[] b = serialiser.serialise(freqMap); IntegerFreqMap o = (IntegerFreqMap) serialiser.deserialise(b); assertEquals(IntegerFreqMap.class, o.getClass()); assertNull(o.get("x")); assertEquals((Integer) 5, o.get("y")); assertEquals((Integer) 20, o.get("z")); }
@Override public IntegerFreqMap deserialiseEmpty() throws SerialisationException { return new IntegerFreqMap(); }
@Override public IntegerFreqMap deserialise(final byte[] bytes) throws SerialisationException { IntegerFreqMap freqMap = new IntegerFreqMap(); if (bytes.length == 0) { return freqMap; } String stringMap; try { stringMap = new String(bytes, CommonConstants.ISO_8859_1_ENCODING); } catch (final UnsupportedEncodingException e) { throw new SerialisationException(e.getMessage(), e); } if (stringMap.isEmpty()) { //No values so return the empty map return freqMap; } String[] keyValues = stringMap.split(SEPERATOR_REGEX); if (keyValues.length % 2 != 0) { throw new SerialisationException("Uneven number of entries found for serialised frequency map, unable to deserialise."); } for (int i = 0; i < keyValues.length - 1; i += 2) { freqMap.put(keyValues[i], Integer.parseInt(keyValues[i + 1])); } return freqMap; }