/**
 * Copyright 2013 Netflix, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.netflix.servo.monitor;

import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.netflix.servo.annotations.DataSourceType;

public class NumberGauge extends AbstractMonitor<Number> implements Gauge<Number>  {
    private final Number number;

    public NumberGauge(MonitorConfig config, Number number) {
        super(config.withAdditionalTag(DataSourceType.GAUGE));
        Preconditions.checkNotNull(number);
        this.number = number;
    }

    @Override
    public Number getValue(int pollerIdx) {
        return number;
    }

    /** {@inheritDoc} */
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }

        if (!(o instanceof NumberGauge)) {
            return false;
        }

        final NumberGauge that = (NumberGauge) o;
        return config.equals(that.config) && number.equals(that.number);
    }

    /** {@inheritDoc} */
    @Override
    public int hashCode() {
        return Objects.hashCode(number, config);
    }

    /** {@inheritDoc} */
    @Override
    public String toString() {
        return Objects.toStringHelper(this).add("number", number).add("config", config).toString();
    }
}

@@ -765,16 +765,129 @@
 eType;%0A%0A
+import java.lang.ref.WeakReference;%0A%0A/**%0A * A %7B@link Gauge%7D that returns the value stored in %7B@link Number%7D.%0A */%0A
 public c
@@ -959,17 +959,16 @@
 %3CNumber%3E
- 
  %7B%0A    p
@@ -980,22 +980,37 @@
 e final 
+WeakReference%3C
 Number
+%3E
  number;
@@ -1019,289 +1019,662 @@
     
-public NumberGauge(MonitorConfig config, Number number) %7B%0A        super(config.withAdditionalTag(DataSourceType.GAUGE));%0A        Preconditions.checkNotNull(number);%0A        this.number = number;%0A    %7D%0A%0A    @Override%0A    public Number getValue(int pollerIdx) %7B%0A        return number
+/**%0A     * Construct a gauge that will store weak reference to the number. The value returned%0A     * by the monitor will be the value stored in %7B@code number%7D or %7B@code Double.NaN%7D in case%0A     * the referred Number has been garbage collected.%0A     */%0A    public NumberGauge(MonitorConfig config, Number number) %7B%0A        super(config.withAdditionalTag(DataSourceType.GAUGE));%0A        Preconditions.checkNotNull(number);%0A        this.number = new WeakReference%3CNumber%3E(number);%0A    %7D%0A%0A    /**%0A     * %7B@inheritDoc%7D%0A     */%0A    @Override%0A    public Number getValue(int pollerIdx) %7B%0A        Number n = number.get();%0A        return n != null ? n : Double.NaN
 ;%0A  
@@ -1677,32 +1677,39 @@
 ;%0A    %7D%0A%0A    /**
+%0A     *
  %7B@inheritDoc%7D *
@@ -1698,32 +1698,37 @@
  * %7B@inheritDoc%7D
+%0A    
  */%0A    @Overrid
@@ -2003,22 +2003,26 @@
 fig) && 
-number
+getValue()
 .equals(
@@ -2026,22 +2026,26 @@
 ls(that.
-number
+getValue()
 );%0A    %7D
@@ -2045,32 +2045,39 @@
 ;%0A    %7D%0A%0A    /**
+%0A     *
  %7B@inheritDoc%7D *
@@ -2066,32 +2066,37 @@
  * %7B@inheritDoc%7D
+%0A    
  */%0A    @Overrid
@@ -2163,16 +2163,22 @@
 e(number
+.get()
 , config
@@ -2194,16 +2194,23 @@
 %0A    /**
+%0A     *
  %7B@inher
@@ -2215,16 +2215,21 @@
 eritDoc%7D
+%0A    
  */%0A    
@@ -2312,16 +2312,33 @@
 er(this)
+%0A                
 .add(%22nu
@@ -2350,17 +2350,40 @@
 , number
-)
+.get())%0A                
 .add(%22co

Use a weak reference for NumberGauge