~ubuntu-branches/ubuntu/oneiric/cobertura/oneiric

« back to all changes in this revision

Viewing changes to src/net/sourceforge/cobertura/coveragedata/countermaps/AtomicCounterMap.java

  • Committer: Bazaar Package Importer
  • Author(s): Miguel Landaeta
  • Date: 2010-05-11 19:21:46 UTC
  • mfrom: (0.1.4 sid) (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100511192146-j742v5jsl89ztndu
Tags: 1.9.4.1+dfsg-2
* Now Build-Depends on libservlet2.5-java and add a missing Depends
  on the same package. (Closes: #580842). 
* Simplify list of JRE dependences for cobertura and drop JRE dependences for
  libcobertura-java as Java libraries are no longer required to depend on a
  JVM.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Cobertura - http://cobertura.sourceforge.net/
 
3
 *
 
4
 * Copyright (C) 2010 Piotr Tabor
 
5
 *
 
6
 * Note: This file is dual licensed under the GPL and the Apache
 
7
 * Source License (so that it can be used from both the main
 
8
 * Cobertura classes and the ant tasks).
 
9
 *
 
10
 * Cobertura is free software; you can redistribute it and/or modify
 
11
 * it under the terms of the GNU General Public License as published
 
12
 * by the Free Software Foundation; either version 2 of the License,
 
13
 * or (at your option) any later version.
 
14
 *
 
15
 * Cobertura is distributed in the hope that it will be useful, but
 
16
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 
18
 * General Public License for more details.
 
19
 *
 
20
 * You should have received a copy of the GNU General Public License
 
21
 * along with Cobertura; if not, write to the Free Software
 
22
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 
23
 * USA
 
24
 */
 
25
 
 
26
package net.sourceforge.cobertura.coveragedata.countermaps;
 
27
 
 
28
import java.util.Iterator;
 
29
import java.util.LinkedHashMap;
 
30
import java.util.Map;
 
31
import java.util.concurrent.ConcurrentHashMap;
 
32
import java.util.concurrent.ConcurrentMap;
 
33
import java.util.concurrent.atomic.AtomicInteger;
 
34
 
 
35
import net.sourceforge.cobertura.coveragedata.HasBeenInstrumented;
 
36
 
 
37
/**
 
38
 * Thread-safe implementation of map that counts number of keys (like multi-set)
 
39
 * @author ptab
 
40
 *
 
41
 * @param <T>
 
42
 */
 
43
public class AtomicCounterMap<T> implements CounterMap<T>,HasBeenInstrumented{
 
44
        private final ConcurrentMap<T, AtomicInteger> counters=new ConcurrentHashMap<T, AtomicInteger>();
 
45
        
 
46
        public final void incrementValue(T key, int inc){
 
47
                AtomicInteger v=counters.get(key);
 
48
                if(v!=null){
 
49
                        v.addAndGet(inc);
 
50
                }else{
 
51
                        v=counters.putIfAbsent(key, new AtomicInteger(inc));
 
52
                        if(v!=null)v.addAndGet(inc);                    
 
53
                }
 
54
        }
 
55
        
 
56
        public final void incrementValue(T key){
 
57
                //AtomicInteger v=counters.putIfAbsent(key, new AtomicInteger(1));
 
58
                //return (v!=null)?v.incrementAndGet():1;
 
59
                AtomicInteger v=counters.get(key);
 
60
                if(v!=null){
 
61
                        v.incrementAndGet();                    
 
62
                }else{
 
63
                        v=counters.putIfAbsent(key, new AtomicInteger(1));
 
64
                        if(v!=null)v.incrementAndGet();
 
65
                }
 
66
        }       
 
67
        
 
68
        public final int getValue(T key){
 
69
                AtomicInteger v=counters.get(key);
 
70
                return v==null?0:v.get();
 
71
        }
 
72
        
 
73
        
 
74
        public synchronized  Map<T,Integer> getFinalStateAndCleanIt(){          
 
75
                Map<T,Integer> res=new LinkedHashMap<T, Integer>();
 
76
                Iterator<Map.Entry<T, AtomicInteger>> iterator=counters.entrySet().iterator();
 
77
                while (iterator.hasNext()) {
 
78
                        Map.Entry<T, AtomicInteger> entry=iterator.next();
 
79
                        T key=entry.getKey();
 
80
                        int old=entry.getValue().get();
 
81
                        iterator.remove();
 
82
                        if(old>0){
 
83
                                res.put(key, old);
 
84
                        }
 
85
                }               
 
86
                return res;             
 
87
        }
 
88
        
 
89
        public int getSize(){
 
90
                return counters.size();
 
91
        }
 
92
}