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, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package org.apache.hadoop.metrics2.lib;
020    
021    import org.apache.hadoop.classification.InterfaceAudience;
022    import org.apache.hadoop.classification.InterfaceStability;
023    import org.apache.hadoop.metrics2.MetricsInfo;
024    import org.apache.hadoop.metrics2.MetricsRecordBuilder;
025    
026    /**
027     * A mutable int gauge
028     */
029    @InterfaceAudience.Public
030    @InterfaceStability.Evolving
031    public class MutableGaugeInt extends MutableGauge {
032    
033      private volatile int value;
034    
035      MutableGaugeInt(MetricsInfo info, int initValue) {
036        super(info);
037        this.value = initValue;
038      }
039    
040      public int value() {
041        return value;
042      }
043    
044      @Override
045      public synchronized void incr() {
046        ++value;
047        setChanged();
048      }
049    
050      /**
051       * Increment by delta
052       * @param delta of the increment
053       */
054      public synchronized void incr(int delta) {
055        value += delta;
056        setChanged();
057      }
058    
059      @Override
060      public synchronized void decr() {
061        --value;
062        setChanged();
063      }
064    
065      /**
066       * decrement by delta
067       * @param delta of the decrement
068       */
069      public synchronized void decr(int delta) {
070        value -= delta;
071        setChanged();
072      }
073    
074      /**
075       * Set the value of the metric
076       * @param value to set
077       */
078      public void set(int value) {
079        this.value = value;
080        setChanged();
081      }
082    
083      @Override
084      public void snapshot(MetricsRecordBuilder builder, boolean all) {
085        if (all || changed()) {
086          builder.addGauge(info(), value);
087          clearChanged();
088        }
089      }
090    }