Martin Mucha has uploaded a new change for review.

Change subject: <core>: wrapper of HashMap for counting number of objects
......................................................................

<core>: wrapper of HashMap for counting number of objects

simple class not to have ot deal with HashMap when intention can be
expressed in more readable way.

Change-Id: I38b9ead37a8ebfc56103b87c65ba582a84f4dda6
Bug-Url: https://bugzilla.redhat.com/1063064
Signed-off-by: Martin Mucha <[email protected]>
---
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/ObjectCounter.java
1 file changed, 86 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/02/26402/1

diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/ObjectCounter.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/ObjectCounter.java
new file mode 100644
index 0000000..749242b
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/network/ObjectCounter.java
@@ -0,0 +1,86 @@
+package org.ovirt.engine.core.bll.network;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.ovirt.engine.core.utils.log.Log;
+import org.ovirt.engine.core.utils.log.LogFactory;
+
+class ObjectCounter<T> {
+
+    private static final Log log = LogFactory.getLog(ObjectCounter.class);
+
+    private Map<T, ModifiableInteger> map = new HashMap<>();
+    private final boolean allowDuplicate;
+
+    ObjectCounter(boolean allowDuplicate) {
+        this.allowDuplicate = allowDuplicate;
+    }
+
+    public boolean add(T key) {
+
+        ModifiableInteger counter = map.get(key);
+        if (counter == null) {
+            map.put(key, new ModifiableInteger(1));
+            return true;
+        } else if (allowDuplicate) {
+            counter.increment();
+            return true;
+        } else {
+            return false;
+        }
+    }
+
+
+    public boolean remove(T key) {
+        ModifiableInteger counter = map.get(key);
+        if (counter == null) {
+            //it's not there, ignore;
+            return false;       //key was not there! So it was not removed.
+        }
+
+
+        int count = counter.decrement();
+        if (count == 0) {
+            map.remove(key);
+            return true;        //key was removed;
+        } else if (count < 0) {
+            log.warn("count underflow.");
+            map.remove(key);
+            return true;        //key was removed.
+        }
+
+        return false;   //key is still present with lessened count.
+
+    }
+
+    public boolean contains(T key) {
+        return map.containsKey(key);
+    }
+
+    private static class ModifiableInteger {
+        private int count;
+
+        public ModifiableInteger(int initialValue) {
+            setCount(initialValue);
+        }
+
+        public int getCount() {
+            return count;
+        }
+
+        public void setCount(int count) {
+            this.count = count;
+        }
+
+        public int increment() {
+            count++;
+            return count;
+        }
+
+        public int decrement() {
+            count--;
+            return count;
+        }
+    }
+}


-- 
To view, visit http://gerrit.ovirt.org/26402
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I38b9ead37a8ebfc56103b87c65ba582a84f4dda6
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Martin Mucha <[email protected]>
_______________________________________________
Engine-patches mailing list
[email protected]
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to