[ https://issues.apache.org/jira/browse/GOSSIP-63?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15901238#comment-15901238 ]
ASF GitHub Bot commented on GOSSIP-63: -------------------------------------- Github user edwardcapriolo commented on a diff in the pull request: https://github.com/apache/incubator-gossip/pull/41#discussion_r104848460 --- Diff: src/main/java/org/apache/gossip/crdt/GrowOnlyCounter.java --- @@ -0,0 +1,94 @@ +package org.apache.gossip.crdt; + + +import java.lang.annotation.ElementType; +import java.util.HashMap; +import java.util.Map; + +public class GrowOnlyCounter implements CrdtCounter<Long, GrowOnlyCounter> { + + + private final Map<String, Long> counters = new HashMap<>(); + private final String myID; + + public GrowOnlyCounter(String myID) { + this.myID = myID; + counters.putIfAbsent(myID, 0L); + } + + private GrowOnlyCounter(String myID, Long count) { + this.myID = myID; + counters.putIfAbsent(myID, count); + } + + private GrowOnlyCounter(String myID, Map<String, Long> counters) { + this.myID = myID; + this.counters.putAll(counters); + } + + + @Override + public GrowOnlyCounter merge(GrowOnlyCounter other) { + //System.out.println(other); + this.counters.putIfAbsent(other.myID, other.counters.get(other.myID)); + Map<String , Long> updatedCounter = new HashMap<>(); + for (Map.Entry<String, Long> entry : this.counters.entrySet()) { + String key = entry.getKey(); + Long value = entry.getValue(); + + if(other.counters.containsKey(key)){ + Long newValue = Math.max(value,other.counters.get(key)); + updatedCounter.put(key,newValue); + }else { + updatedCounter.put(key,value); + } + + } + + return new GrowOnlyCounter(myID,updatedCounter); + } + + @Override + public Long value() { + Long globalCount = 0L; + for (Long increment : counters.values()) { + globalCount += increment; + } + return globalCount; + } + + @Override + public GrowOnlyCounter optimize() { + return new GrowOnlyCounter(myID, counters.get(myID)); + } + + @Override + public boolean equals(Object obj) { + --- End diff -- Avoid random line breaks > Implement Crdt G-Counter > ------------------------ > > Key: GOSSIP-63 > URL: https://issues.apache.org/jira/browse/GOSSIP-63 > Project: Gossip > Issue Type: New Feature > Reporter: Edward Capriolo > -- This message was sent by Atlassian JIRA (v6.3.15#6346)