flume-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From esam...@apache.org
Subject svn commit: r1156868 - in /incubator/flume/branches/flume-728/flume-ng-core/src: main/java/org/apache/flume/core/CounterGroup.java test/java/org/apache/flume/core/TestCounterGroup.java
Date Fri, 12 Aug 2011 00:46:24 GMT
Author: esammer
Date: Fri Aug 12 00:46:24 2011
New Revision: 1156868

URL: http://svn.apache.org/viewvc?rev=1156868&view=rev
Log:
- Added CounterGroup for holding a collection of counters (for metrics).

Added:
    incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/core/CounterGroup.java
    incubator/flume/branches/flume-728/flume-ng-core/src/test/java/org/apache/flume/core/TestCounterGroup.java

Added: incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/core/CounterGroup.java
URL: http://svn.apache.org/viewvc/incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/core/CounterGroup.java?rev=1156868&view=auto
==============================================================================
--- incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/core/CounterGroup.java
(added)
+++ incubator/flume/branches/flume-728/flume-ng-core/src/main/java/org/apache/flume/core/CounterGroup.java
Fri Aug 12 00:46:24 2011
@@ -0,0 +1,58 @@
+package org.apache.flume.core;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicLong;
+
+public class CounterGroup {
+
+  private String name;
+  private ConcurrentHashMap<String, AtomicLong> counters;
+
+  public CounterGroup() {
+    counters = new ConcurrentHashMap<String, AtomicLong>();
+  }
+
+  public Long get(String name) {
+    return getCounter(name).get();
+  }
+
+  public Long incrementAndGet(String name) {
+    return getCounter(name).incrementAndGet();
+  }
+
+  public Long addAndGet(String name, Long delta) {
+    return getCounter(name).addAndGet(delta);
+  }
+
+  public AtomicLong getCounter(String name) {
+    synchronized (counters) {
+      if (!counters.containsKey(name)) {
+        counters.put(name, new AtomicLong());
+      }
+    }
+
+    return counters.get(name);
+  }
+
+  @Override
+  public String toString() {
+    return "{ name:" + name + " counters:" + counters + " }";
+  }
+
+  public String getName() {
+    return name;
+  }
+
+  public void setName(String name) {
+    this.name = name;
+  }
+
+  public ConcurrentHashMap<String, AtomicLong> getCounters() {
+    return counters;
+  }
+
+  public void setCounters(ConcurrentHashMap<String, AtomicLong> counters) {
+    this.counters = counters;
+  }
+
+}

Added: incubator/flume/branches/flume-728/flume-ng-core/src/test/java/org/apache/flume/core/TestCounterGroup.java
URL: http://svn.apache.org/viewvc/incubator/flume/branches/flume-728/flume-ng-core/src/test/java/org/apache/flume/core/TestCounterGroup.java?rev=1156868&view=auto
==============================================================================
--- incubator/flume/branches/flume-728/flume-ng-core/src/test/java/org/apache/flume/core/TestCounterGroup.java
(added)
+++ incubator/flume/branches/flume-728/flume-ng-core/src/test/java/org/apache/flume/core/TestCounterGroup.java
Fri Aug 12 00:46:24 2011
@@ -0,0 +1,48 @@
+package org.apache.flume.core;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+import junit.framework.Assert;
+
+import org.junit.Before;
+import org.junit.Test;
+
+public class TestCounterGroup {
+
+  private CounterGroup counterGroup;
+
+  @Before
+  public void setUp() {
+    counterGroup = new CounterGroup();
+  }
+
+  @Test
+  public void testGetCounter() {
+    AtomicLong counter = counterGroup.getCounter("test");
+
+    Assert.assertNotNull(counter);
+    Assert.assertEquals(0, counter.get());
+  }
+
+  @Test
+  public void testGet() {
+    long value = counterGroup.get("test");
+
+    Assert.assertEquals(0, value);
+  }
+
+  @Test
+  public void testIncrementAndGet() {
+    long value = counterGroup.incrementAndGet("test");
+
+    Assert.assertEquals(1, value);
+  }
+
+  @Test
+  public void testAddAndGet() {
+    long value = counterGroup.addAndGet("test", 13L);
+
+    Assert.assertEquals(13, value);
+  }
+
+}



Mime
View raw message