001    /*
002     * Copyright 2014-2015 UnboundID Corp.
003     * All Rights Reserved.
004     */
005    /*
006     * Copyright (C) 2015 UnboundID Corp.
007     *
008     * This program is free software; you can redistribute it and/or modify
009     * it under the terms of the GNU General Public License (GPLv2 only)
010     * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
011     * as published by the Free Software Foundation.
012     *
013     * This program is distributed in the hope that it will be useful,
014     * but WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program; if not, see <http://www.gnu.org/licenses>.
020     */
021    package com.unboundid.ldap.sdk.unboundidds.monitors;
022    
023    
024    
025    import java.util.ArrayList;
026    import java.util.Collections;
027    import java.util.LinkedHashMap;
028    import java.util.List;
029    import java.util.Map;
030    import java.util.StringTokenizer;
031    
032    import com.unboundid.ldap.sdk.Entry;
033    import com.unboundid.util.Debug;
034    import com.unboundid.util.NotMutable;
035    import com.unboundid.util.ThreadSafety;
036    import com.unboundid.util.ThreadSafetyLevel;
037    
038    import static com.unboundid.ldap.sdk.unboundidds.monitors.MonitorMessages.*;
039    
040    
041    
042    /**
043     * <BLOCKQUOTE>
044     *   <B>NOTE:</B>  This class is part of the Commercial Edition of the UnboundID
045     *   LDAP SDK for Java.  It is not available for use in applications that
046     *   include only the Standard Edition of the LDAP SDK, and is not supported for
047     *   use in conjunction with non-UnboundID products.
048     * </BLOCKQUOTE>
049     * This class defines a numeric gauge monitor entry, which obtains its
050     * information from a numeric value in a monitor entry.
051     */
052    @NotMutable()
053    @ThreadSafety(level=ThreadSafetyLevel.COMPLETELY_THREADSAFE)
054    public final class NumericGaugeMonitorEntry
055           extends GaugeMonitorEntry
056    {
057      /**
058       * The structural object class used in gauge monitor entries.
059       */
060      static final String NUMERIC_GAUGE_MONITOR_OC =
061           "ds-numeric-gauge-monitor-entry";
062    
063    
064    
065      /**
066       * The serial version UID for this serializable class.
067       */
068      private static final long serialVersionUID = 2049893927290436280L;
069    
070    
071    
072      // The current value for the gauge.
073      private final Double currentValue;
074    
075      // The maximum value observed for the gauge.
076      private final Double maximumValue;
077    
078      // The minimum value observed for the gauge.
079      private final Double minimumValue;
080    
081      // The current value for the gauge.
082      private final Double previousValue;
083    
084      // The set of observed values for the gauge.
085      private final List<Double> observedValues;
086    
087    
088    
089      /**
090       * Creates a new numeric gauge monitor entry from the provided entry.
091       *
092       * @param  entry  The entry to be parsed as a numeric gauge monitor entry.  It
093       *                must not be {@code null}.
094       */
095      public NumericGaugeMonitorEntry(final Entry entry)
096      {
097        super(entry);
098    
099        currentValue = getDouble("value");
100        previousValue = getDouble("previous-value");
101        minimumValue = getDouble("value-minimum");
102        maximumValue = getDouble("value-maximum");
103    
104        final String observedStr = getString("observed-values");
105        if ((observedStr == null) || (observedStr.length() == 0))
106        {
107          observedValues = Collections.emptyList();
108        }
109        else
110        {
111          final ArrayList<Double> values = new ArrayList<Double>(10);
112          try
113          {
114            final StringTokenizer tokenizer = new StringTokenizer(observedStr, ",");
115            while (tokenizer.hasMoreTokens())
116            {
117              values.add(Double.parseDouble(tokenizer.nextToken()));
118            }
119          }
120          catch (final Exception e)
121          {
122            Debug.debugException(e);
123            values.clear();
124          }
125    
126          observedValues = Collections.unmodifiableList(values);
127        }
128      }
129    
130    
131    
132      /**
133       * Retrieves the current value for the gauge, if available.
134       *
135       * @return  The current value for the gauge, or {@code null} if it was not
136       *          included in the monitor entry.
137       */
138      public Double getCurrentValue()
139      {
140        return currentValue;
141      }
142    
143    
144    
145      /**
146       * Retrieves the previous value for the gauge, if available.
147       *
148       * @return  The previous value for the gauge, or {@code null} if it was not
149       *          included in the monitor entry.
150       */
151      public Double getPreviousValue()
152      {
153        return previousValue;
154      }
155    
156    
157    
158      /**
159       * Retrieves the minimum value observed for the gauge, if available.
160       *
161       * @return  The minimum value observed for the gauge, or {@code null} if it
162       *          was not included in the monitor entry.
163       */
164      public Double getMinimumValue()
165      {
166        return minimumValue;
167      }
168    
169    
170    
171      /**
172       * Retrieves the maximum value observed for the gauge, if available.
173       *
174       * @return  The maximum value observed for the gauge, or {@code null} if it
175       *          was not included in the monitor entry.
176       */
177      public Double getMaximumValue()
178      {
179        return maximumValue;
180      }
181    
182    
183    
184      /**
185       * Retrieves the set of observed values for the gauge, if available.
186       *
187       * @return  The set of observed values for the gauge, or {@code null} if it
188       *          was not included in the monitor entry.
189       */
190      public List<Double> getObservedValues()
191      {
192        return observedValues;
193      }
194    
195    
196    
197      /**
198       * {@inheritDoc}
199       */
200      @Override()
201      public String getMonitorDisplayName()
202      {
203        return INFO_NUMERIC_GAUGE_MONITOR_DISPNAME.get();
204      }
205    
206    
207    
208      /**
209       * {@inheritDoc}
210       */
211      @Override()
212      public String getMonitorDescription()
213      {
214        return INFO_NUMERIC_GAUGE_MONITOR_DESC.get();
215      }
216    
217    
218    
219      /**
220       * {@inheritDoc}
221       */
222      @Override()
223      public Map<String,MonitorAttribute> getMonitorAttributes()
224      {
225        final Map<String,MonitorAttribute> superAttributes =
226             super.getMonitorAttributes();
227    
228        final LinkedHashMap<String,MonitorAttribute> attrs =
229             new LinkedHashMap<String,MonitorAttribute>(superAttributes.size() + 5);
230        attrs.putAll(superAttributes);
231    
232        if (currentValue != null)
233        {
234          addMonitorAttribute(attrs,
235               "value",
236               INFO_NUMERIC_GAUGE_DISPNAME_CURRENT_VALUE.get(),
237               INFO_NUMERIC_GAUGE_DESC_CURRENT_VALUE.get(),
238               currentValue);
239        }
240    
241        if (previousValue != null)
242        {
243          addMonitorAttribute(attrs,
244               "previous-value",
245               INFO_NUMERIC_GAUGE_DISPNAME_PREVIOUS_VALUE.get(),
246               INFO_NUMERIC_GAUGE_DESC_PREVIOUS_VALUE.get(),
247               previousValue);
248        }
249    
250        if (minimumValue != null)
251        {
252          addMonitorAttribute(attrs,
253               "value-minimum",
254               INFO_NUMERIC_GAUGE_DISPNAME_MINIMUM_VALUE.get(),
255               INFO_NUMERIC_GAUGE_DESC_MINIMUM_VALUE.get(),
256               minimumValue);
257        }
258    
259        if (maximumValue != null)
260        {
261          addMonitorAttribute(attrs,
262               "value-maximum",
263               INFO_NUMERIC_GAUGE_DISPNAME_MAXIMUM_VALUE.get(),
264               INFO_NUMERIC_GAUGE_DESC_MAXIMUM_VALUE.get(),
265               maximumValue);
266        }
267    
268        if (! observedValues.isEmpty())
269        {
270          final Double[] values = new Double[observedValues.size()];
271          observedValues.toArray(values);
272    
273          attrs.put("observed-values",
274               new MonitorAttribute("observed-values",
275                    INFO_NUMERIC_GAUGE_DISPNAME_OBSERVED_VALUES.get(),
276                    INFO_NUMERIC_GAUGE_DESC_OBSERVED_VALUES.get(),
277                    values));
278        }
279    
280        return Collections.unmodifiableMap(attrs);
281      }
282    }