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
019package org.apache.hadoop.metrics2.lib;
020
021import org.apache.hadoop.classification.InterfaceAudience;
022import org.apache.hadoop.classification.InterfaceStability;
023import org.apache.hadoop.metrics2.MetricsInfo;
024import org.apache.hadoop.metrics2.MetricsRecordBuilder;
025
026/**
027 * A mutable long gauge
028 */
029@InterfaceAudience.Public
030@InterfaceStability.Evolving
031public class MutableGaugeDouble extends MutableGauge {
032
033  private Double value = new Double(0);
034
035  MutableGaugeDouble(MetricsInfo info, double doubleValue) {
036    super(info);
037    this.value = doubleValue;
038  }
039
040  public synchronized double value() {
041    return value;
042  }
043
044  @Override
045  public synchronized void incr() {
046    value += 1;
047  }
048
049  /**
050   * Increment by delta
051   * @param delta of the increment
052   */
053  public synchronized void incr(double delta) {
054    value += delta;
055    setChanged();
056  }
057
058  @Override
059  public synchronized void decr() {
060    value -= 1;
061  }
062
063  /**
064   * decrement by delta
065   * @param delta of the decrement
066   */
067  public synchronized void decr(double delta) {
068    value -= delta;
069    setChanged();
070  }
071
072  /**
073   * Set the value of the metric
074   * @param value to set
075   */
076  public synchronized void set(double value) {
077    this.value = value;
078    setChanged();
079  }
080
081  public synchronized void snapshot(MetricsRecordBuilder builder, boolean all) {
082    if (all || changed()) {
083      builder.addGauge(info(), value());
084      clearChanged();
085    }
086  }
087
088}