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 import net.sf.ehcache.util.counter.Counter;
20 import net.sf.ehcache.util.counter.CounterConfig;
21
22 /***
23 * Config for a {@link SampledCounter}
24 *
25 * @author <a href="mailto:asanoujam@terracottatech.com">Abhishek Sanoujam</a>
26 * @since 1.7
27 *
28 */
29 public class SampledCounterConfig extends CounterConfig {
30 private final int intervalSecs;
31 private final int historySize;
32 private final boolean isReset;
33
34 /***
35 * Make a new timed counter config (duh)
36 *
37 * @param intervalSecs
38 * the interval (in seconds) between sampling
39 * @param historySize
40 * number of counter samples that will be retained in memory
41 * @param isResetOnSample
42 * true if the counter should be reset to 0 upon each sample
43 */
44 public SampledCounterConfig(int intervalSecs, int historySize, boolean isResetOnSample, long initialValue) {
45 super(initialValue);
46 if (intervalSecs < 1) {
47 throw new IllegalArgumentException("Interval (" + intervalSecs + ") must be greater than or equal to 1");
48 }
49 if (historySize < 1) {
50 throw new IllegalArgumentException("History size (" + historySize + ") must be greater than or equal to 1");
51 }
52
53 this.intervalSecs = intervalSecs;
54 this.historySize = historySize;
55 this.isReset = isResetOnSample;
56 }
57
58 /***
59 * Returns the history size
60 *
61 * @return The history size
62 */
63 public int getHistorySize() {
64 return historySize;
65 }
66
67 /***
68 * Returns the interval time (seconds)
69 *
70 * @return Interval of the sampling thread in seconds
71 */
72 public int getIntervalSecs() {
73 return intervalSecs;
74 }
75
76 /***
77 * Returns true if counters created from this config will reset on each
78 * sample
79 *
80 * @return true if values are reset to the initial value after each sample
81 */
82 public boolean isResetOnSample() {
83 return this.isReset;
84 }
85
86 /***
87 * {@inheritDoc}
88 */
89 @Override
90 public Counter createCounter() {
91 return new SampledCounterImpl(this);
92 }
93 }