Skip to content

Instantly share code, notes, and snippets.

@yangmungi
Last active November 8, 2016 01:36
Show Gist options
  • Select an option

  • Save yangmungi/56d44d15afd7d062d8ba01d48db83be9 to your computer and use it in GitHub Desktop.

Select an option

Save yangmungi/56d44d15afd7d062d8ba01d48db83be9 to your computer and use it in GitHub Desktop.
class MutableInteger {
private int value;
public MutableInteger(int value) {
this.value = value;
}
// getValue()
// increment()
}
class Counter<T> {
private Map<T, MutableInteger> counting;
public void incrementValue(T k) {
if (counting.containsKey(k)) {
counting.get(k).increment();
} else {
counting.put(k, new MutableInteger(1));
}
}
}
class Data {
private int total;
private Map<String, Counter<String>> counter;
public void incrementCount(String key, String value) {
counter.get(key).incrementValue(value);
}
public int getTotal() {
return total;
}
protected void addTotal(int add) {
total += add;
}
public void incrementTotal() {
total++;
}
}
class IteratingService {
private GettingService<List<Integer>> getter;
public void doStuff() {
// set up Iterating with all possible keys for counter
}
class Iterating extends Data {
public void doWithIteration(String nextFromIterator) {
Map<String, String> attributes = getter.get(nextFromIterator);
boolean hasValue = false;
// not using .entrySet() due to laziness
Set<String> keys = attributes.keySet();
for (String key : keys) {
String value = attributes.get(key);
if (!value.isEmpty()) {
hasValue = true;
incrementCount(key, value);
}
}
if (hasValue) {
incrementTotal();
}
}
}
}
class Merging extends Data {
public void doMerges(Data otherData) {
addTotal(otherData.getTotal());
// merge the counter stuff
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment