@Override protected FreqMap _apply(final FreqMap a, final FreqMap b) { for (final Entry<String, Long> entry : b.entrySet()) { if (a.containsKey(entry.getKey())) { a.put(entry.getKey(), a.get(entry.getKey()) + entry.getValue()); } else { a.put(entry.getKey(), entry.getValue()); } } return a; } }
/** * Creates a filtered copy of the map using a supplied predicate.<br> * Returns a copy of the map if predicate supplied is null. * * @param map The frequency map that is to be sorted through * @return A new frequency map with only the filtered entries present. */ private FreqMap filterPredicate(final FreqMap map) { final FreqMap f = new FreqMap(); if (predicate == null) { map.forEach(f::upsert); } else { map.entrySet().stream().filter(e -> predicate.test(e.getKey())) .forEach(e -> f.upsert(e.getKey(), e.getValue())); } return f; }
@Override public byte[] serialise(final FreqMap map) throws SerialisationException { Set<Entry<String, Long>> entrySet = map.entrySet(); ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean isFirst = true; for (final Entry<String, Long> entry : entrySet) { if (null != entry.getKey() && null != entry.getValue()) { if (isFirst) { isFirst = false; } else { out.write(ByteArrayEscapeUtils.DELIMITER); } try { out.write(ByteArrayEscapeUtils.escape(entry.getKey().getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a key from a FreqMap: " + entry.getKey(), e); } out.write(ByteArrayEscapeUtils.DELIMITER); try { out.write(ByteArrayEscapeUtils.escape(longSerialiser.serialise(entry.getValue()))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a value from a FreqMap: " + entry.getValue(), e); } } } return out.toByteArray(); }
@Override protected FreqMap _apply(final FreqMap a, final FreqMap b) { for (final Entry<String, Long> entry : b.entrySet()) { if (a.containsKey(entry.getKey())) { a.put(entry.getKey(), a.get(entry.getKey()) + entry.getValue()); } else { a.put(entry.getKey(), entry.getValue()); } } return a; } }
@Override public byte[] serialise(final FreqMap map) throws SerialisationException { Set<Entry<String, Long>> entrySet = map.entrySet(); ByteArrayOutputStream out = new ByteArrayOutputStream(); boolean isFirst = true; for (final Entry<String, Long> entry : entrySet) { if (null != entry.getKey() && null != entry.getValue()) { if (isFirst) { isFirst = false; } else { out.write(ByteArrayEscapeUtils.DELIMITER); } try { out.write(ByteArrayEscapeUtils.escape(entry.getKey().getBytes(CommonConstants.UTF_8))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a key from a FreqMap: " + entry.getKey(), e); } out.write(ByteArrayEscapeUtils.DELIMITER); try { out.write(ByteArrayEscapeUtils.escape(longSerialiser.serialise(entry.getValue()))); } catch (final IOException e) { throw new SerialisationException("Failed to serialise a value from a FreqMap: " + entry.getValue(), e); } } } return out.toByteArray(); }