use of uk.gov.gchq.gaffer.types.IntegerFreqMap in project Gaffer by gchq.
the class IntegerFreqMapSerialiserTest method canSerialiseEmptyFreqMap.
@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());
}
use of uk.gov.gchq.gaffer.types.IntegerFreqMap in project Gaffer by gchq.
the class IntegerFreqMapSerialiserTest method canSerialiseDeSerialiseFreqMapWithValues.
@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"));
}
use of uk.gov.gchq.gaffer.types.IntegerFreqMap in project Gaffer by gchq.
the class IntegerFreqMapSerialiserTest method testSerialiserWillSkipEntryWithNullValue.
@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"));
}
use of uk.gov.gchq.gaffer.types.IntegerFreqMap in project Gaffer by gchq.
the class IntegerFreqMapSerialiser method deserialise.
@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 (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;
}
use of uk.gov.gchq.gaffer.types.IntegerFreqMap in project Gaffer by gchq.
the class IntegerFreqMapAggregatorTest method shouldMergeFreqMaps.
@Test
public void shouldMergeFreqMaps() {
// Given
final IntegerFreqMapAggregator aggregator = new IntegerFreqMapAggregator();
aggregator.init();
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
aggregator._aggregate(freqMap1);
aggregator._aggregate(freqMap2);
// Then
final IntegerFreqMap mergedFreqMap = ((IntegerFreqMap) aggregator.state()[0]);
assertEquals((Integer) 2, mergedFreqMap.get("1"));
assertEquals((Integer) 7, mergedFreqMap.get("2"));
assertEquals((Integer) 5, mergedFreqMap.get("3"));
}
Aggregations