Statistics.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *   http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing,
  13.  * software distributed under the License is distributed on an
  14.  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15.  * KIND, either express or implied.  See the License for the
  16.  * specific language governing permissions and limitations
  17.  * under the License.
  18.  */
  19. package org.apache.commons.jcs3.jcache;

  20. import java.util.concurrent.atomic.AtomicLong;

  21. public class Statistics
  22. {
  23.     private volatile boolean active = true;

  24.     private final AtomicLong removals = new AtomicLong();
  25.     private final AtomicLong expiries = new AtomicLong();
  26.     private final AtomicLong puts = new AtomicLong();
  27.     private final AtomicLong hits = new AtomicLong();
  28.     private final AtomicLong misses = new AtomicLong();
  29.     private final AtomicLong evictions = new AtomicLong();
  30.     private final AtomicLong putTimeTaken = new AtomicLong();
  31.     private final AtomicLong getTimeTaken = new AtomicLong();
  32.     private final AtomicLong removeTimeTaken = new AtomicLong();

  33.     public long getHits()
  34.     {
  35.         return hits.get();
  36.     }

  37.     public long getMisses()
  38.     {
  39.         return misses.get();
  40.     }

  41.     public long getPuts()
  42.     {
  43.         return puts.get();
  44.     }

  45.     public long getRemovals()
  46.     {
  47.         return removals.get();
  48.     }

  49.     public long getEvictions()
  50.     {
  51.         return evictions.get();
  52.     }

  53.     public long getTimeTakenForGets()
  54.     {
  55.         return getTimeTaken.get();
  56.     }

  57.     public long getTimeTakenForPuts()
  58.     {
  59.         return putTimeTaken.get();
  60.     }

  61.     public long getTimeTakenForRemovals()
  62.     {
  63.         return removeTimeTaken.get();
  64.     }

  65.     public void increaseRemovals(final long number)
  66.     {
  67.         increment(removals, number);
  68.     }

  69.     public void increaseExpiries(final long number)
  70.     {
  71.         increment(expiries, number);
  72.     }

  73.     public void increasePuts(final long number)
  74.     {
  75.         increment(puts, number);
  76.     }

  77.     public void increaseHits(final long number)
  78.     {
  79.         increment(hits, number);
  80.     }

  81.     public void increaseMisses(final long number)
  82.     {
  83.         increment(misses, number);
  84.     }

  85.     public void increaseEvictions(final long number)
  86.     {
  87.         increment(evictions, number);
  88.     }

  89.     public void addGetTime(final long duration)
  90.     {
  91.         increment(duration, getTimeTaken);
  92.     }

  93.     public void addPutTime(final long duration)
  94.     {
  95.         increment(duration, putTimeTaken);
  96.     }

  97.     public void addRemoveTime(final long duration)
  98.     {
  99.         increment(duration, removeTimeTaken);
  100.     }

  101.     private void increment(final AtomicLong counter, final long number)
  102.     {
  103.         if (!active)
  104.         {
  105.             return;
  106.         }
  107.         counter.addAndGet(number);
  108.     }

  109.     private void increment(final long duration, final AtomicLong counter)
  110.     {
  111.         if (!active)
  112.         {
  113.             return;
  114.         }

  115.         if (counter.get() < Long.MAX_VALUE - duration)
  116.         {
  117.             counter.addAndGet(duration);
  118.         }
  119.         else
  120.         {
  121.             reset();
  122.             counter.set(duration);
  123.         }
  124.     }

  125.     public void reset()
  126.     {
  127.         puts.set(0);
  128.         misses.set(0);
  129.         removals.set(0);
  130.         expiries.set(0);
  131.         hits.set(0);
  132.         evictions.set(0);
  133.         getTimeTaken.set(0);
  134.         putTimeTaken.set(0);
  135.         removeTimeTaken.set(0);
  136.     }

  137.     public void setActive(final boolean active)
  138.     {
  139.         this.active = active;
  140.     }
  141. }