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 */
018package org.jppf.utils.stats;
019
020import java.io.Serializable;
021import java.util.*;
022
023import org.jppf.utils.collections.CollectionUtils;
024
025/**
026 * Convenience class for collecting time or size statistics.
027 * Instances of this class are thread-safe.
028 */
029public interface JPPFSnapshot extends Serializable
030{
031  /**
032   * Add a set of aggregated values to this snapshot.
033   * @param accumulatedValues the accumulated sum of the values to add.
034   * @param count the number of values in the accumalated values.
035   * @exclude
036   */
037  void addValues(final double accumulatedValues, final long count);
038
039  /**
040   * Make a copy of this time snapshot object.
041   * @return a <code>TimeSnapshot</code> instance.
042   * @exclude
043   */
044  JPPFSnapshot copy();
045
046  /**
047   * Reset all counters to their initial values.
048   * @exclude
049   */
050  void reset();
051
052  /**
053   * Get the total cumulated sum of the values.
054   * @return the cumulated values as a double.
055   */
056  double getTotal();
057
058  /**
059   * Get the latest observed value.
060   * @return the latest value as a double.
061   */
062  double getLatest();
063
064  /**
065   * Get the smallest observed value.
066   * @return the minimum value as a double.
067   */
068  double getMin();
069
070  /**
071   * Get the peak observed value.
072   * @return the peak value double.
073   */
074  double getMax();
075
076  /**
077   * Get the average value.
078   * @return the average value as a double.
079   */
080  double getAvg();
081
082  /**
083   * Get the label for this snapshot.
084   * @return the label as a string.
085   */
086  String getLabel();
087
088  /**
089   * Get the count of values added to this snapshot.
090   * @return the count of values as a long.
091   */
092  long getValueCount();
093
094  /**
095   * Base class for filters including or excluding snpashots based on a provided set of labels.
096   */
097  public abstract static class LabelBasedFilter implements JPPFStatistics.Filter
098  {
099    /**
100     * The set of labels to exclude.
101     */
102    protected final Set<String> labels;
103    /**
104     * Determines whether to include or exclude the snapshots.
105     */
106    protected final boolean includeFlag;
107
108    /**
109     * Initialize with the specified array of labels.
110     * @param includeFlag determines whether to include or exclude the snapshots.
111     * @param labels the labels to exclude.
112     */
113    protected LabelBasedFilter(final boolean includeFlag, final String...labels)
114    {
115      this.includeFlag = includeFlag;
116      this.labels = CollectionUtils.set(labels);
117    }
118
119    /**
120     * Initialize with the specified collection of labels.
121     * @param includeFlag determines whether to include or exclude the snapshots.
122     * @param labels the labels to exclude.
123     */
124    protected LabelBasedFilter(final boolean includeFlag, final Collection<String> labels)
125    {
126      this.includeFlag = includeFlag;
127      this.labels = new HashSet(labels);
128    }
129
130    @Override
131    public boolean accept(final JPPFSnapshot snapshot)
132    {
133      boolean b = labels.contains(snapshot.getLabel());
134      return includeFlag ? b : !b;
135    }
136  }
137
138  /**
139   * Filter including snpashots based on a provided set of labels.
140   */
141  public static class LabelIncludingFilter extends LabelBasedFilter
142  {
143    /**
144     * Initialize with the specified array of labels.
145     * @param labels the labels to include.
146     */
147    public LabelIncludingFilter(final String...labels)
148    {
149      super(true, labels);
150    }
151
152    /**
153     * Initialize with the specified collection of labels.
154     * @param labels the labels to include.
155     */
156    public LabelIncludingFilter(final Collection<String> labels)
157    {
158      super(true, labels);
159    }
160  }
161
162  /**
163   * Filter excluding snpashots based on a provided set of labels.
164   */
165  public static class LabelExcludingFilter extends LabelBasedFilter
166  {
167    /**
168     * Initialize with the specified array of labels.
169     * @param labels the labels to exclude.
170     */
171    public LabelExcludingFilter(final String...labels)
172    {
173      super(false, labels);
174    }
175
176    /**
177     * Initialize with the specified collection of labels.
178     * @param labels the labels to exclude.
179     */
180    public LabelExcludingFilter(final Collection<String> labels)
181    {
182      super(false, labels);
183    }
184  }
185}