001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.jcs.jcache;
020
021import java.util.concurrent.atomic.AtomicLong;
022
023public class Statistics
024{
025    private volatile boolean active = true;
026
027    private final AtomicLong removals = new AtomicLong();
028    private final AtomicLong expiries = new AtomicLong();
029    private final AtomicLong puts = new AtomicLong();
030    private final AtomicLong hits = new AtomicLong();
031    private final AtomicLong misses = new AtomicLong();
032    private final AtomicLong evictions = new AtomicLong();
033    private final AtomicLong putTimeTaken = new AtomicLong();
034    private final AtomicLong getTimeTaken = new AtomicLong();
035    private final AtomicLong removeTimeTaken = new AtomicLong();
036
037    public long getHits()
038    {
039        return hits.get();
040    }
041
042    public long getMisses()
043    {
044        return misses.get();
045    }
046
047    public long getPuts()
048    {
049        return puts.get();
050    }
051
052    public long getRemovals()
053    {
054        return removals.get();
055    }
056
057    public long getEvictions()
058    {
059        return evictions.get();
060    }
061
062    public long getTimeTakenForGets()
063    {
064        return getTimeTaken.get();
065    }
066
067    public long getTimeTakenForPuts()
068    {
069        return putTimeTaken.get();
070    }
071
072    public long getTimeTakenForRemovals()
073    {
074        return removeTimeTaken.get();
075    }
076
077    public void increaseRemovals(final long number)
078    {
079        increment(removals, number);
080    }
081
082    public void increaseExpiries(final long number)
083    {
084        increment(expiries, number);
085    }
086
087    public void increasePuts(final long number)
088    {
089        increment(puts, number);
090    }
091
092    public void increaseHits(final long number)
093    {
094        increment(hits, number);
095    }
096
097    public void increaseMisses(final long number)
098    {
099        increment(misses, number);
100    }
101
102    public void increaseEvictions(final long number)
103    {
104        increment(evictions, number);
105    }
106
107    public void addGetTime(final long duration)
108    {
109        increment(duration, getTimeTaken);
110    }
111
112    public void addPutTime(final long duration)
113    {
114        increment(duration, putTimeTaken);
115    }
116
117    public void addRemoveTime(final long duration)
118    {
119        increment(duration, removeTimeTaken);
120    }
121
122    private void increment(final AtomicLong counter, final long number)
123    {
124        if (!active)
125        {
126            return;
127        }
128        counter.addAndGet(number);
129    }
130
131    private void increment(final long duration, final AtomicLong counter)
132    {
133        if (!active)
134        {
135            return;
136        }
137
138        if (counter.get() < Long.MAX_VALUE - duration)
139        {
140            counter.addAndGet(duration);
141        }
142        else
143        {
144            reset();
145            counter.set(duration);
146        }
147    }
148
149    public void reset()
150    {
151        puts.set(0);
152        misses.set(0);
153        removals.set(0);
154        expiries.set(0);
155        hits.set(0);
156        evictions.set(0);
157        getTimeTaken.set(0);
158        putTimeTaken.set(0);
159        removeTimeTaken.set(0);
160    }
161
162    public void setActive(final boolean active)
163    {
164        this.active = active;
165    }
166}