View Javadoc

1   /***
2    *  Copyright 2003-2010 Terracotta, Inc.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  
17  package net.sf.ehcache.util.counter.sampled;
18  
19  /***
20   * An implementation of {@link SampledRateCounter}
21   * 
22   * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
23   * @since 1.7
24   * 
25   */
26  public class SampledRateCounterImpl extends SampledCounterImpl implements SampledRateCounter {
27  
28      private static final String OPERATION_NOT_SUPPORTED_MSG = "This operation is not supported. Use SampledCounter Or Counter instead";
29  
30      private long numeratorValue;
31      private long denominatorValue;
32  
33      /***
34       * Constructor accepting the config
35       * 
36       * @param config
37       */
38      public SampledRateCounterImpl(SampledRateCounterConfig config) {
39          super(config);
40      }
41  
42      /***
43       * {@inheritDoc}
44       */
45      public synchronized void setValue(long numerator, long denominator) {
46          this.numeratorValue = numerator;
47          this.denominatorValue = denominator;
48      }
49  
50      /***
51       * {@inheritDoc}
52       */
53      public synchronized void increment(long numerator, long denominator) {
54          this.numeratorValue += numerator;
55          this.denominatorValue += denominator;
56      }
57  
58      /***
59       * {@inheritDoc}
60       */
61      public synchronized void decrement(long numerator, long denominator) {
62          this.numeratorValue -= numerator;
63          this.denominatorValue -= denominator;
64      }
65  
66      /***
67       * {@inheritDoc}
68       */
69      public synchronized void setDenominatorValue(long newValue) {
70          this.denominatorValue = newValue;
71      }
72  
73      /***
74       * {@inheritDoc}
75       */
76      public synchronized void setNumeratorValue(long newValue) {
77          this.numeratorValue = newValue;
78      }
79  
80      /***
81       * {@inheritDoc}
82       */
83      @Override
84      public synchronized long getValue() {
85          return denominatorValue == 0 ? 0 : (numeratorValue / denominatorValue);
86      }
87  
88      /***
89       * {@inheritDoc}
90       */
91      @Override
92      public synchronized long getAndReset() {
93          long prevVal = getValue();
94          setValue(0, 0);
95          return prevVal;
96      }
97  
98      // ====== unsupported operations. These operations need multiple params for
99      // this class
100     /***
101      * throws {@link UnsupportedOperationException}
102      */
103     @Override
104     public long getAndSet(long newValue) {
105         throw new UnsupportedOperationException(OPERATION_NOT_SUPPORTED_MSG);
106     }
107 
108     /***
109      * throws {@link UnsupportedOperationException}
110      */
111     @Override
112     public synchronized void setValue(long newValue) {
113         throw new UnsupportedOperationException(OPERATION_NOT_SUPPORTED_MSG);
114     }
115 
116     /***
117      * throws {@link UnsupportedOperationException}
118      */
119     @Override
120     public long decrement() {
121         throw new UnsupportedOperationException(OPERATION_NOT_SUPPORTED_MSG);
122     }
123 
124     /***
125      * throws {@link UnsupportedOperationException}
126      */
127     @Override
128     public long decrement(long amount) {
129         throw new UnsupportedOperationException(OPERATION_NOT_SUPPORTED_MSG);
130     }
131 
132     /***
133      * throws {@link UnsupportedOperationException}
134      */
135     public long getMaxValue() {
136         throw new UnsupportedOperationException(OPERATION_NOT_SUPPORTED_MSG);
137     }
138 
139     /***
140      * throws {@link UnsupportedOperationException}
141      */
142     public long getMinValue() {
143         throw new UnsupportedOperationException(OPERATION_NOT_SUPPORTED_MSG);
144     }
145 
146     /***
147      * throws {@link UnsupportedOperationException}
148      */
149     @Override
150     public long increment() {
151         throw new UnsupportedOperationException(OPERATION_NOT_SUPPORTED_MSG);
152     }
153 
154     /***
155      * throws {@link UnsupportedOperationException}
156      */
157     @Override
158     public long increment(long amount) {
159         throw new UnsupportedOperationException(OPERATION_NOT_SUPPORTED_MSG);
160     }
161 
162 }