View Javadoc

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, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.metrics2.lib;
20  
21  import org.apache.hadoop.hbase.util.Counter;
22  import org.apache.hadoop.metrics2.MetricsInfo;
23  import org.apache.hadoop.metrics2.MetricsRecordBuilder;
24  
25  public class MutableFastCounter extends MutableCounter {
26  
27    private final Counter counter;
28  
29    protected MutableFastCounter(MetricsInfo info, long iVal) {
30      super(info);
31      counter = new Counter(iVal);
32    }
33  
34    @Override
35    public void incr() {
36      counter.increment();
37      setChanged();
38    }
39  
40    /**
41     * Increment the value by a delta
42     * @param delta of the increment
43     */
44    public void incr(long delta) {
45      counter.add(delta);
46      setChanged();
47    }
48  
49    @Override
50    public void snapshot(MetricsRecordBuilder builder, boolean all) {
51      if (all || changed()) {
52        builder.addCounter(info(), value());
53        clearChanged();
54      }
55    }
56  
57    public long value() {
58      return counter.get();
59    }
60  }