Search in sources :

Example 6 with IntegerFreqMap

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;
}
Also used : IntegerFreqMap(uk.gov.gchq.gaffer.types.IntegerFreqMap) SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

IntegerFreqMap (uk.gov.gchq.gaffer.types.IntegerFreqMap)6 Test (org.junit.Test)5 AggregateFunctionTest (uk.gov.gchq.gaffer.function.AggregateFunctionTest)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)1