001/*
002 * JPPF.
003 * Copyright (C) 2005-2015 JPPF Team.
004 * http://www.jppf.org
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * 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.jppf.utils.stats;
020
021/**
022 * Instances of this class simply accumulate the values provided via calls to {@link #addValues(double, long)}.
023 * The min, max, avg and latest values are never computed and thus have a constant value of 0.
024 * <p>This is intended for monitoring snapshots that only handle one value and to avoid excessive overhead during
025 * serialization and deserialization.
026 * @author Laurent Cohen
027 */
028public class SingleValueSnapshot implements JPPFSnapshot
029{
030  /**
031   * Explicit serialVersionUID.
032   */
033  private static final long serialVersionUID = 1L;
034  /**
035   * Label for this snapshot, used in the {@link #toString()} method.
036   * @exclude
037   */
038  protected final String label;
039  /**
040   * The total cumulated values.
041   * @exclude
042   */
043  protected double total = 0d;
044
045  /**
046   * Initialize this time snapshot with a specified title.
047   * @param label the title for this snapshot.
048   * @exclude
049   */
050  public SingleValueSnapshot(final String label)
051  {
052    this.label = label;
053  }
054
055  /**
056   * {@inheritDoc}
057   * @exclude
058   */
059  @Override
060  public synchronized void addValues(final double accumulatedValues, final long count)
061  {
062    total += accumulatedValues;
063  }
064
065  /**
066   * {@inheritDoc}
067   * @exclude
068   */
069  @Override
070  public synchronized JPPFSnapshot copy()
071  {
072    SingleValueSnapshot svs = new SingleValueSnapshot(label);
073    svs.total = total;
074    return svs;
075  }
076
077  /**
078   * {@inheritDoc}
079   * @exclude
080   */
081  @Override
082  public synchronized void reset()
083  {
084    total = 0d;
085  }
086
087  @Override
088  public String getLabel()
089  {
090    return label;
091  }
092
093  @Override
094  public synchronized double getTotal()
095  {
096    return total;
097  }
098
099  @Override
100  public long getValueCount()
101  {
102    return 0;
103  }
104
105  @Override
106  public double getLatest()
107  {
108    return 0;
109  }
110
111  @Override
112  public double getMin()
113  {
114    return 0;
115  }
116
117  @Override
118  public double getMax()
119  {
120    return 0;
121  }
122
123  @Override
124  public double getAvg()
125  {
126    return 0;
127  }
128
129  @Override
130  public synchronized String toString()
131  {
132    return new StringBuilder(JPPFStatisticsHelper.getLocalizedLabel(this)).append(": type=").append(getClass().getSimpleName()).append(", total=").append(total).toString();
133  }
134}