adjust ConcurrentScoreMap internal value map to interface and use parameter

Long -> Integer (saves some bytes)
pull/91/head
reger 9 years ago
parent 31d2a5645e
commit aaae7c6462

@ -32,17 +32,17 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicInteger;
public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreMap<E> { public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreMap<E> {
protected final ConcurrentHashMap<E, AtomicLong> map; // a mapping from a reference to the cluster key protected final ConcurrentHashMap<E, AtomicInteger> map; // a mapping from a reference to the cluster key
private long gcount; private long gcount; // sum of all scores
public ConcurrentScoreMap() { public ConcurrentScoreMap() {
this.map = new ConcurrentHashMap<E, AtomicLong>(); this.map = new ConcurrentHashMap<E, AtomicInteger>();
this.gcount = 0; this.gcount = 0;
} }
@ -77,8 +77,8 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
*/ */
@Override @Override
public void shrinkToMinScore(final int minScore) { public void shrinkToMinScore(final int minScore) {
final Iterator<Map.Entry<E, AtomicLong>> i = this.map.entrySet().iterator(); final Iterator<Map.Entry<E, AtomicInteger>> i = this.map.entrySet().iterator();
Map.Entry<E, AtomicLong> entry; Map.Entry<E, AtomicInteger> entry;
while (i.hasNext()) { while (i.hasNext()) {
entry = i.next(); entry = i.next();
if (entry.getValue().intValue() < minScore) i.remove(); if (entry.getValue().intValue() < minScore) i.remove();
@ -109,7 +109,7 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
if (obj == null) return; if (obj == null) return;
// use atomic operations // use atomic operations
this.map.putIfAbsent(obj, new AtomicLong(0)); this.map.putIfAbsent(obj, new AtomicInteger(0));
this.map.get(obj).incrementAndGet(); this.map.get(obj).incrementAndGet();
// increase overall counter // increase overall counter
@ -121,7 +121,7 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
if (obj == null) return; if (obj == null) return;
// use atomic operations // use atomic operations
this.map.putIfAbsent(obj, new AtomicLong(0)); this.map.putIfAbsent(obj, new AtomicInteger(0));
this.map.get(obj).decrementAndGet(); this.map.get(obj).decrementAndGet();
// increase overall counter // increase overall counter
@ -133,12 +133,12 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
if (obj == null) return; if (obj == null) return;
// use atomic operations // use atomic operations
final AtomicLong old = this.map.putIfAbsent(obj, new AtomicLong(0)); final AtomicInteger old = this.map.putIfAbsent(obj, new AtomicInteger(newScore));
// adjust overall counter if value replaced // adjust overall counter if value replaced
if (old != null) this.gcount -= old.longValue(); // must use old befor setting a new value (it's a object reference) if (old != null) {
this.gcount -= old.intValue(); // must use old before setting a new value (it's a object reference)
this.map.get(obj).set(newScore); this.map.get(obj).set(newScore);
}
// increase overall counter // increase overall counter
this.gcount += newScore; this.gcount += newScore;
} }
@ -148,7 +148,7 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
if (obj == null) return; if (obj == null) return;
// use atomic operations // use atomic operations
this.map.putIfAbsent(obj, new AtomicLong(0)); this.map.putIfAbsent(obj, new AtomicInteger(0));
this.map.get(obj).addAndGet(incrementScore); this.map.get(obj).addAndGet(incrementScore);
// increase overall counter // increase overall counter
@ -164,7 +164,7 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
public int delete(final E obj) { public int delete(final E obj) {
// deletes entry and returns previous score // deletes entry and returns previous score
if (obj == null) return 0; if (obj == null) return 0;
final AtomicLong score = this.map.remove(obj); final AtomicInteger score = this.map.remove(obj);
if (score == null) return 0; if (score == null) return 0;
// decrease overall counter // decrease overall counter
@ -180,7 +180,7 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
@Override @Override
public int get(final E obj) { public int get(final E obj) {
if (obj == null) return 0; if (obj == null) return 0;
final AtomicLong score = this.map.get(obj); final AtomicInteger score = this.map.get(obj);
if (score == null) return 0; if (score == null) return 0;
return score.intValue(); return score.intValue();
} }
@ -188,7 +188,7 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
public int getMinScore() { public int getMinScore() {
if (this.map.isEmpty()) return -1; if (this.map.isEmpty()) return -1;
int minScore = Integer.MAX_VALUE; int minScore = Integer.MAX_VALUE;
for (final Map.Entry<E, AtomicLong> entry : this.map.entrySet()) for (final Map.Entry<E, AtomicInteger> entry : this.map.entrySet())
if (entry.getValue().intValue() < minScore) { if (entry.getValue().intValue() < minScore) {
minScore = entry.getValue().intValue(); minScore = entry.getValue().intValue();
} }
@ -199,7 +199,7 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
if (this.map.isEmpty()) if (this.map.isEmpty())
return -1; return -1;
int maxScore = Integer.MIN_VALUE; int maxScore = Integer.MIN_VALUE;
for (final Map.Entry<E, AtomicLong> entry : this.map.entrySet()) for (final Map.Entry<E, AtomicInteger> entry : this.map.entrySet())
if (entry.getValue().intValue() > maxScore) { if (entry.getValue().intValue() > maxScore) {
maxScore = entry.getValue().intValue(); maxScore = entry.getValue().intValue();
} }
@ -211,13 +211,17 @@ public class ConcurrentScoreMap<E> extends AbstractScoreMap<E> implements ScoreM
return this.map.toString(); return this.map.toString();
} }
/**
* @param up true = asc order, fals = reverse order
* @return iteratior accessing the keys in ordered by score values
*/
@Override @Override
public Iterator<E> keys(final boolean up) { public Iterator<E> keys(final boolean up) {
// re-organize entries // re-organize entries
final TreeMap<Integer, Set<E>> m = new TreeMap<Integer, Set<E>>(); final TreeMap<Integer, Set<E>> m = new TreeMap<Integer, Set<E>>();
Set<E> s; Set<E> s;
Integer is; Integer is;
for (final Map.Entry<E, AtomicLong> entry: this.map.entrySet()) { for (final Map.Entry<E, AtomicInteger> entry: this.map.entrySet()) {
is = new Integer(entry.getValue().intValue()); is = new Integer(entry.getValue().intValue());
s = m.get(is); s = m.get(is);
if (s == null) { if (s == null) {

Loading…
Cancel
Save