17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  *
267c478bd9Sstevel@tonic-gate  * ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate package com.sun.solaris.domain.pools;
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate import java.math.BigInteger;
327c478bd9Sstevel@tonic-gate import java.util.Date;
337c478bd9Sstevel@tonic-gate import java.util.Iterator;
347c478bd9Sstevel@tonic-gate import java.text.*;
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate import com.sun.solaris.service.pools.UnsignedInt64;
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /**
397c478bd9Sstevel@tonic-gate  * Contains the information relating to a specific statistic for an
407c478bd9Sstevel@tonic-gate  * object. The Statistic has no notion of the source of the data, it
417c478bd9Sstevel@tonic-gate  * is simply a repository for holding statistical information.
427c478bd9Sstevel@tonic-gate  */
437c478bd9Sstevel@tonic-gate interface Statistic
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate 	/**
467c478bd9Sstevel@tonic-gate 	 * Return the start of the sample period for which the
477c478bd9Sstevel@tonic-gate 	 * statistic is representative.
487c478bd9Sstevel@tonic-gate 	 */
getStart()497c478bd9Sstevel@tonic-gate 	public Date getStart();
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	/**
527c478bd9Sstevel@tonic-gate 	 * Return the end of the sample period for which the
537c478bd9Sstevel@tonic-gate 	 * statistic is representative.
547c478bd9Sstevel@tonic-gate 	 */
getEnd()557c478bd9Sstevel@tonic-gate 	public Date getEnd();
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate 	/**
587c478bd9Sstevel@tonic-gate 	 * Get the value of this statistic.
597c478bd9Sstevel@tonic-gate 	 */
getValue()607c478bd9Sstevel@tonic-gate 	public Object getValue();
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate 	/**
637c478bd9Sstevel@tonic-gate 	 * Get the value of this statistic as a Long.
647c478bd9Sstevel@tonic-gate 	 */
getLongValue()657c478bd9Sstevel@tonic-gate 	public Long getLongValue();
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 	/**
687c478bd9Sstevel@tonic-gate 	 * Get the value of this statistic as a Double.
697c478bd9Sstevel@tonic-gate 	 */
getDoubleValue()707c478bd9Sstevel@tonic-gate 	public Double getDoubleValue();
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	/**
737c478bd9Sstevel@tonic-gate 	 * Get the value of this statistic as a UnsignedInt64.
747c478bd9Sstevel@tonic-gate 	 */
getUnsignedInt64Value()757c478bd9Sstevel@tonic-gate 	public UnsignedInt64 getUnsignedInt64Value();
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /**
797c478bd9Sstevel@tonic-gate  * An interface for Statistics which may be aggregated.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate interface AggregateStatistic extends Statistic
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	/**
847c478bd9Sstevel@tonic-gate 	 * Add the supplied statistic to this.
857c478bd9Sstevel@tonic-gate 	 *
867c478bd9Sstevel@tonic-gate 	 * @param o The other statistic.
877c478bd9Sstevel@tonic-gate 	 */
add(AggregateStatistic o)887c478bd9Sstevel@tonic-gate 	public AggregateStatistic add(AggregateStatistic o);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	/**
917c478bd9Sstevel@tonic-gate 	 * Subtract the supplied statistic from this.
927c478bd9Sstevel@tonic-gate 	 *
937c478bd9Sstevel@tonic-gate 	 * @param o The other statistic.
947c478bd9Sstevel@tonic-gate 	 */
subtract(AggregateStatistic o)957c478bd9Sstevel@tonic-gate 	public AggregateStatistic subtract(AggregateStatistic o);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/**
987c478bd9Sstevel@tonic-gate 	 * Produce the aggregate of all objects in the supplied
997c478bd9Sstevel@tonic-gate 	 * iterator (which must be of the same type) whose start and
1007c478bd9Sstevel@tonic-gate 	 * end dates lie within the supplied ranges. If either start
1017c478bd9Sstevel@tonic-gate 	 * or end is null, then that bound is not applied. i.e. if no
1027c478bd9Sstevel@tonic-gate 	 * start date is supplied, then start checking is disabled.
1037c478bd9Sstevel@tonic-gate 	 *
1047c478bd9Sstevel@tonic-gate 	 * @param start The start date for qualification in the snapshot.
1057c478bd9Sstevel@tonic-gate 	 * @param end The end date for qualification in the snapshot.
1067c478bd9Sstevel@tonic-gate 	 * @throws IllegalArgumentException If the iterator is empty.
1077c478bd9Sstevel@tonic-gate 	 */
getSnapshotForInterval(Iterator it, Date start, Date end)1087c478bd9Sstevel@tonic-gate 	public AggregateStatistic getSnapshotForInterval(Iterator it,
1097c478bd9Sstevel@tonic-gate 	    Date start, Date end) throws IllegalArgumentException;
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate /**
1137c478bd9Sstevel@tonic-gate  * A basic Statistic implementation which makes it easy to derive
1147c478bd9Sstevel@tonic-gate  * concrete statistic types. This is an immutable class, the state is
1157c478bd9Sstevel@tonic-gate  * set when the object is constructed and cannot be later changed.
1167c478bd9Sstevel@tonic-gate  */
1177c478bd9Sstevel@tonic-gate abstract class AbstractStatistic implements Statistic
1187c478bd9Sstevel@tonic-gate {
1197c478bd9Sstevel@tonic-gate 	/**
1207c478bd9Sstevel@tonic-gate 	 * The value of the statistic.
1217c478bd9Sstevel@tonic-gate 	 */
1227c478bd9Sstevel@tonic-gate 	private final Object value;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	/**
1257c478bd9Sstevel@tonic-gate 	 * The start of the interval during which the statistic was
1267c478bd9Sstevel@tonic-gate 	 * captured.
1277c478bd9Sstevel@tonic-gate 	 */
1287c478bd9Sstevel@tonic-gate 	private final Date start;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	/**
1317c478bd9Sstevel@tonic-gate 	 * The end of the interval during which the statistic was
1327c478bd9Sstevel@tonic-gate 	 * captured.
1337c478bd9Sstevel@tonic-gate 	 */
1347c478bd9Sstevel@tonic-gate 	private final Date end;
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 	/**
1377c478bd9Sstevel@tonic-gate 	 * Formatter for the sample start time, used by toString().
1387c478bd9Sstevel@tonic-gate 	 */
1397c478bd9Sstevel@tonic-gate 	private static final DateFormat df = new SimpleDateFormat("kk:mm:ss");
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 	/**
1427c478bd9Sstevel@tonic-gate 	 * Constructor. This is provided as a mechanism to allow
1437c478bd9Sstevel@tonic-gate 	 * inherited classes to correctly initialize their state.
1447c478bd9Sstevel@tonic-gate 	 *
1457c478bd9Sstevel@tonic-gate 	 * @param value The value of this statistic.
1467c478bd9Sstevel@tonic-gate 	 */
AbstractStatistic(Object value)1477c478bd9Sstevel@tonic-gate 	protected AbstractStatistic(Object value)
1487c478bd9Sstevel@tonic-gate 	{
1497c478bd9Sstevel@tonic-gate 		this(value, null, null);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/**
1537c478bd9Sstevel@tonic-gate 	 * Constructor. This is provided as a mechanism to allow
1547c478bd9Sstevel@tonic-gate 	 * inherited classes to correctly initialize their state.
1557c478bd9Sstevel@tonic-gate 	 *
1567c478bd9Sstevel@tonic-gate 	 * @param value The value of this statistic.
1577c478bd9Sstevel@tonic-gate 	 * @param start The start of the sample period which this
1587c478bd9Sstevel@tonic-gate 	 * statistic represents.
1597c478bd9Sstevel@tonic-gate 	 * @param end The end of the sample period which this
1607c478bd9Sstevel@tonic-gate 	 * statistic represents.
1617c478bd9Sstevel@tonic-gate 	 */
AbstractStatistic(Object value, Date start, Date end)1627c478bd9Sstevel@tonic-gate 	protected AbstractStatistic(Object value, Date start, Date end)
1637c478bd9Sstevel@tonic-gate 	{
1647c478bd9Sstevel@tonic-gate 		this.value = value;
1657c478bd9Sstevel@tonic-gate 		this.start = start;
1667c478bd9Sstevel@tonic-gate 		this.end = end;
1677c478bd9Sstevel@tonic-gate 	}
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate 	/**
1707c478bd9Sstevel@tonic-gate 	 * Return the start of the sample period for which the
1717c478bd9Sstevel@tonic-gate 	 * statistic is representative.
1727c478bd9Sstevel@tonic-gate 	 */
getStart()1737c478bd9Sstevel@tonic-gate 	public Date getStart()
1747c478bd9Sstevel@tonic-gate 	{
1757c478bd9Sstevel@tonic-gate 		return (start);
1767c478bd9Sstevel@tonic-gate 	}
177*55fea89dSDan Cross 
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/**
1807c478bd9Sstevel@tonic-gate 	 * Return the end of the sample period for which the
1817c478bd9Sstevel@tonic-gate 	 * statistic is representative.
1827c478bd9Sstevel@tonic-gate 	 */
getEnd()1837c478bd9Sstevel@tonic-gate 	public Date getEnd()
1847c478bd9Sstevel@tonic-gate 	{
1857c478bd9Sstevel@tonic-gate 		return (end);
1867c478bd9Sstevel@tonic-gate 	}
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	/**
1897c478bd9Sstevel@tonic-gate 	 * Get the value of this statistic.
1907c478bd9Sstevel@tonic-gate 	 */
getValue()1917c478bd9Sstevel@tonic-gate 	public Object getValue()
1927c478bd9Sstevel@tonic-gate 	{
1937c478bd9Sstevel@tonic-gate 		return (value);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 
getLongValue()1967c478bd9Sstevel@tonic-gate 	public abstract Long getLongValue();
getDoubleValue()1977c478bd9Sstevel@tonic-gate 	public abstract Double getDoubleValue();
getUnsignedInt64Value()1987c478bd9Sstevel@tonic-gate 	public abstract UnsignedInt64 getUnsignedInt64Value();
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate 	/**
2017c478bd9Sstevel@tonic-gate 	 * Return the string representation of this statistic.
2027c478bd9Sstevel@tonic-gate 	 */
toString()2037c478bd9Sstevel@tonic-gate 	public String toString()
2047c478bd9Sstevel@tonic-gate 	{
2057c478bd9Sstevel@tonic-gate 		StringBuffer buf = new StringBuffer();
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		buf.append(value);
2087c478bd9Sstevel@tonic-gate 		if (start != null && end != null) {
2097c478bd9Sstevel@tonic-gate 			buf.append(" from ");
2107c478bd9Sstevel@tonic-gate 			buf.append(df.format(start));
2117c478bd9Sstevel@tonic-gate 			buf.append(" to ");
2127c478bd9Sstevel@tonic-gate 			buf.append(df.format(end));
2137c478bd9Sstevel@tonic-gate 		}
2147c478bd9Sstevel@tonic-gate 		return (buf.toString());
2157c478bd9Sstevel@tonic-gate 	}
2167c478bd9Sstevel@tonic-gate }
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /**
2197c478bd9Sstevel@tonic-gate  * A statistic of type Double.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate final class DoubleStatistic extends AbstractStatistic
2227c478bd9Sstevel@tonic-gate     implements AggregateStatistic
2237c478bd9Sstevel@tonic-gate {
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	/**
2267c478bd9Sstevel@tonic-gate 	 * Constructor.
2277c478bd9Sstevel@tonic-gate 	 *
2287c478bd9Sstevel@tonic-gate 	 * @param value The value of this statistic.
2297c478bd9Sstevel@tonic-gate 	 */
DoubleStatistic(Double value)2307c478bd9Sstevel@tonic-gate 	public DoubleStatistic(Double value)
2317c478bd9Sstevel@tonic-gate 	{
2327c478bd9Sstevel@tonic-gate 		super(value);
2337c478bd9Sstevel@tonic-gate 	}
234*55fea89dSDan Cross 
2357c478bd9Sstevel@tonic-gate 	/**
2367c478bd9Sstevel@tonic-gate 	 * Constructor.
2377c478bd9Sstevel@tonic-gate 	 *
2387c478bd9Sstevel@tonic-gate 	 * @param value The value of this statistic.
2397c478bd9Sstevel@tonic-gate 	 * @param start The start of the interval over which this
2407c478bd9Sstevel@tonic-gate 	 * statistic is representative.
2417c478bd9Sstevel@tonic-gate 	 * @param end The end of the interval over which this
2427c478bd9Sstevel@tonic-gate 	 * statistic is representative.
2437c478bd9Sstevel@tonic-gate 	 */
DoubleStatistic(Double value, Date start, Date end)2447c478bd9Sstevel@tonic-gate 	public DoubleStatistic(Double value, Date start, Date end)
2457c478bd9Sstevel@tonic-gate 	{
2467c478bd9Sstevel@tonic-gate 		super(value, start, end);
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate 
getDoubleValue()2497c478bd9Sstevel@tonic-gate 	public Double getDoubleValue()
2507c478bd9Sstevel@tonic-gate 	{
2517c478bd9Sstevel@tonic-gate 		return ((Double) getValue());
2527c478bd9Sstevel@tonic-gate 	}
253*55fea89dSDan Cross 
getLongValue()2547c478bd9Sstevel@tonic-gate 	public Long getLongValue()
2557c478bd9Sstevel@tonic-gate 	{
2567c478bd9Sstevel@tonic-gate 		return (new Long(((Double) getValue()).longValue()));
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
getUnsignedInt64Value()2597c478bd9Sstevel@tonic-gate 	public UnsignedInt64 getUnsignedInt64Value()
2607c478bd9Sstevel@tonic-gate 	{
2617c478bd9Sstevel@tonic-gate 		return (new UnsignedInt64(Long.toString(((Double) getValue()).
2627c478bd9Sstevel@tonic-gate 					      longValue())));
2637c478bd9Sstevel@tonic-gate 	}
2647c478bd9Sstevel@tonic-gate 
add(AggregateStatistic o)2657c478bd9Sstevel@tonic-gate 	public AggregateStatistic add(AggregateStatistic o)
2667c478bd9Sstevel@tonic-gate 	{
2677c478bd9Sstevel@tonic-gate 		Double v1 = getDoubleValue();
2687c478bd9Sstevel@tonic-gate 		Double v2 = o.getDoubleValue();
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		return (new DoubleStatistic(new Double(v1.doubleValue() +
2717c478bd9Sstevel@tonic-gate 					    v2.doubleValue()),
2727c478bd9Sstevel@tonic-gate 			getStart(), getEnd()));
2737c478bd9Sstevel@tonic-gate 	}
274*55fea89dSDan Cross 
subtract(AggregateStatistic o)2757c478bd9Sstevel@tonic-gate 	public AggregateStatistic subtract(AggregateStatistic o)
2767c478bd9Sstevel@tonic-gate 	{
2777c478bd9Sstevel@tonic-gate 		Double v1 = getDoubleValue();
2787c478bd9Sstevel@tonic-gate 		Double v2 = o.getDoubleValue();
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		return (new DoubleStatistic(new Double(v1.doubleValue() -
2817c478bd9Sstevel@tonic-gate 					    v2.doubleValue()),
2827c478bd9Sstevel@tonic-gate 			getStart(), getEnd()));
2837c478bd9Sstevel@tonic-gate 	}
2847c478bd9Sstevel@tonic-gate 
getSnapshotForInterval(Iterator it, Date start, Date end)2857c478bd9Sstevel@tonic-gate 	public AggregateStatistic getSnapshotForInterval(Iterator it,
2867c478bd9Sstevel@tonic-gate 	    Date start, Date end) throws IllegalArgumentException
2877c478bd9Sstevel@tonic-gate 	{
2887c478bd9Sstevel@tonic-gate 		double total = 0;
2897c478bd9Sstevel@tonic-gate 		int count = 0;
2907c478bd9Sstevel@tonic-gate 		Date first = start, last = end;
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		while (it.hasNext()) {
2937c478bd9Sstevel@tonic-gate 			DoubleStatistic s = (DoubleStatistic) it.next();
2947c478bd9Sstevel@tonic-gate 			if (start != null) {
2957c478bd9Sstevel@tonic-gate 				if (s.getStart().compareTo(start) < 0)
2967c478bd9Sstevel@tonic-gate 					continue;
2977c478bd9Sstevel@tonic-gate 			}
2987c478bd9Sstevel@tonic-gate 			if (first == null)
2997c478bd9Sstevel@tonic-gate 				first = s.getStart();
3007c478bd9Sstevel@tonic-gate 			if (end != null) {
3017c478bd9Sstevel@tonic-gate 				if (s.getEnd().compareTo(end) > 0)
3027c478bd9Sstevel@tonic-gate 					continue;
3037c478bd9Sstevel@tonic-gate 			}
3047c478bd9Sstevel@tonic-gate 			last = s.getEnd();
3057c478bd9Sstevel@tonic-gate 			total += s.getDoubleValue().doubleValue();
3067c478bd9Sstevel@tonic-gate 			count++;
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 		if (count == 0)
3097c478bd9Sstevel@tonic-gate 			throw new IllegalArgumentException("Cannot derive a " +
3107c478bd9Sstevel@tonic-gate 			    "snapshot from an empty iterator");
3117c478bd9Sstevel@tonic-gate 		return (new DoubleStatistic(new Double(total / count), first,
3127c478bd9Sstevel@tonic-gate 			last));
3137c478bd9Sstevel@tonic-gate 	}
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate /**
3177c478bd9Sstevel@tonic-gate  * A statistic of type Long.
3187c478bd9Sstevel@tonic-gate  */
3197c478bd9Sstevel@tonic-gate final class LongStatistic extends AbstractStatistic
3207c478bd9Sstevel@tonic-gate     implements AggregateStatistic
3217c478bd9Sstevel@tonic-gate {
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	/**
3247c478bd9Sstevel@tonic-gate 	 * Constructor.
3257c478bd9Sstevel@tonic-gate 	 *
3267c478bd9Sstevel@tonic-gate 	 * @param value The value of this statistic.
3277c478bd9Sstevel@tonic-gate 	 * @param start The start of the interval over which this
3287c478bd9Sstevel@tonic-gate 	 * statistic is representative.
3297c478bd9Sstevel@tonic-gate 	 * @param end The end of the interval over which this
3307c478bd9Sstevel@tonic-gate 	 * statistic is representative.
3317c478bd9Sstevel@tonic-gate 	 */
LongStatistic(Long value, Date start, Date end)3327c478bd9Sstevel@tonic-gate 	public LongStatistic(Long value, Date start, Date end)
3337c478bd9Sstevel@tonic-gate 	{
3347c478bd9Sstevel@tonic-gate 		super(value, start, end);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
getDoubleValue()3377c478bd9Sstevel@tonic-gate 	public Double getDoubleValue()
3387c478bd9Sstevel@tonic-gate 	{
3397c478bd9Sstevel@tonic-gate 		return (new Double(((Long) getValue()).longValue()));
3407c478bd9Sstevel@tonic-gate 	}
341*55fea89dSDan Cross 
getLongValue()3427c478bd9Sstevel@tonic-gate 	public Long getLongValue()
3437c478bd9Sstevel@tonic-gate 	{
3447c478bd9Sstevel@tonic-gate 		return ((Long) getValue());
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
getUnsignedInt64Value()3477c478bd9Sstevel@tonic-gate 	public UnsignedInt64 getUnsignedInt64Value()
3487c478bd9Sstevel@tonic-gate 	{
3497c478bd9Sstevel@tonic-gate 		return (new UnsignedInt64(Long.toString(((Long) getValue()).
3507c478bd9Sstevel@tonic-gate 					      longValue())));
3517c478bd9Sstevel@tonic-gate 	}
3527c478bd9Sstevel@tonic-gate 
add(AggregateStatistic o)3537c478bd9Sstevel@tonic-gate 	public AggregateStatistic add(AggregateStatistic o)
3547c478bd9Sstevel@tonic-gate 	{
3557c478bd9Sstevel@tonic-gate 		Long v1 = getLongValue();
3567c478bd9Sstevel@tonic-gate 		Long v2 = o.getLongValue();
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 		return (new LongStatistic(new Long(v1.longValue() +
3597c478bd9Sstevel@tonic-gate 					    v2.longValue()),
3607c478bd9Sstevel@tonic-gate 			getStart(), getEnd()));
3617c478bd9Sstevel@tonic-gate 	}
3627c478bd9Sstevel@tonic-gate 
subtract(AggregateStatistic o)3637c478bd9Sstevel@tonic-gate 	public AggregateStatistic subtract(AggregateStatistic o)
3647c478bd9Sstevel@tonic-gate 	{
3657c478bd9Sstevel@tonic-gate 		Long v1 = getLongValue();
3667c478bd9Sstevel@tonic-gate 		Long v2 = o.getLongValue();
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate 		return (new LongStatistic(new Long(v1.longValue() -
3697c478bd9Sstevel@tonic-gate 					    v2.longValue()),
3707c478bd9Sstevel@tonic-gate 			getStart(), getEnd()));
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
getSnapshotForInterval(Iterator it, Date start, Date end)3737c478bd9Sstevel@tonic-gate 	public AggregateStatistic getSnapshotForInterval(Iterator it,
3747c478bd9Sstevel@tonic-gate 	    Date start, Date end) throws IllegalArgumentException
3757c478bd9Sstevel@tonic-gate 	{
3767c478bd9Sstevel@tonic-gate 		long total = 0;
3777c478bd9Sstevel@tonic-gate 		int count = 0;
3787c478bd9Sstevel@tonic-gate 		Date first = start, last = end;
3797c478bd9Sstevel@tonic-gate 		while (it.hasNext()) {
3807c478bd9Sstevel@tonic-gate 			LongStatistic s = (LongStatistic) it.next();
3817c478bd9Sstevel@tonic-gate 			if (start != null) {
3827c478bd9Sstevel@tonic-gate 				if (s.getStart().compareTo(start) < 0)
3837c478bd9Sstevel@tonic-gate 					continue;
3847c478bd9Sstevel@tonic-gate 			}
3857c478bd9Sstevel@tonic-gate 			if (first == null)
3867c478bd9Sstevel@tonic-gate 				first = s.getStart();
3877c478bd9Sstevel@tonic-gate 			if (end != null) {
3887c478bd9Sstevel@tonic-gate 				if (s.getEnd().compareTo(end) > 0)
3897c478bd9Sstevel@tonic-gate 					continue;
3907c478bd9Sstevel@tonic-gate 			}
3917c478bd9Sstevel@tonic-gate 			last = s.getEnd();
3927c478bd9Sstevel@tonic-gate 			total += s.getLongValue().longValue();
3937c478bd9Sstevel@tonic-gate 			count++;
3947c478bd9Sstevel@tonic-gate 		}
3957c478bd9Sstevel@tonic-gate 		if (count == 0)
3967c478bd9Sstevel@tonic-gate 			throw new IllegalArgumentException("Cannot derive a " +
3977c478bd9Sstevel@tonic-gate 			    "snapshot from an empty iterator");
3987c478bd9Sstevel@tonic-gate 		return (new LongStatistic(new Long(total / count), first,
3997c478bd9Sstevel@tonic-gate 			last));
4007c478bd9Sstevel@tonic-gate 	}
4017c478bd9Sstevel@tonic-gate }
402*55fea89dSDan Cross 
4037c478bd9Sstevel@tonic-gate /**
4047c478bd9Sstevel@tonic-gate  * A statistic of type UnsignedInt64.
4057c478bd9Sstevel@tonic-gate  */
4067c478bd9Sstevel@tonic-gate final class UnsignedInt64Statistic extends AbstractStatistic
4077c478bd9Sstevel@tonic-gate     implements AggregateStatistic
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 	/**
4117c478bd9Sstevel@tonic-gate 	 * Constructor.
4127c478bd9Sstevel@tonic-gate 	 *
4137c478bd9Sstevel@tonic-gate 	 * @param value The value of this statistic.
4147c478bd9Sstevel@tonic-gate 	 * @param start The start of the interval over which this
4157c478bd9Sstevel@tonic-gate 	 * statistic is representative.
4167c478bd9Sstevel@tonic-gate 	 * @param end The end of the interval over which this
4177c478bd9Sstevel@tonic-gate 	 * statistic is representative.
4187c478bd9Sstevel@tonic-gate 	 */
UnsignedInt64Statistic(UnsignedInt64 value, Date start, Date end)4197c478bd9Sstevel@tonic-gate 	public UnsignedInt64Statistic(UnsignedInt64 value, Date start,
4207c478bd9Sstevel@tonic-gate 	    Date end)
4217c478bd9Sstevel@tonic-gate 	{
4227c478bd9Sstevel@tonic-gate 		super(value, start, end);
4237c478bd9Sstevel@tonic-gate 	}
4247c478bd9Sstevel@tonic-gate 
getDoubleValue()4257c478bd9Sstevel@tonic-gate 	public Double getDoubleValue()
4267c478bd9Sstevel@tonic-gate 	{
4277c478bd9Sstevel@tonic-gate 		return (new Double(((UnsignedInt64) getValue()).longValue()));
4287c478bd9Sstevel@tonic-gate 	}
429*55fea89dSDan Cross 
getLongValue()4307c478bd9Sstevel@tonic-gate 	public Long getLongValue()
4317c478bd9Sstevel@tonic-gate 	{
4327c478bd9Sstevel@tonic-gate 		return (new Long(((UnsignedInt64) getValue()).longValue()));
4337c478bd9Sstevel@tonic-gate 	}
4347c478bd9Sstevel@tonic-gate 
getUnsignedInt64Value()4357c478bd9Sstevel@tonic-gate 	public UnsignedInt64 getUnsignedInt64Value()
4367c478bd9Sstevel@tonic-gate 	{
4377c478bd9Sstevel@tonic-gate 		return ((UnsignedInt64) getValue());
4387c478bd9Sstevel@tonic-gate 	}
4397c478bd9Sstevel@tonic-gate 
add(AggregateStatistic o)4407c478bd9Sstevel@tonic-gate 	public AggregateStatistic add(AggregateStatistic o)
4417c478bd9Sstevel@tonic-gate 	{
4427c478bd9Sstevel@tonic-gate 		UnsignedInt64 v1 = getUnsignedInt64Value();
4437c478bd9Sstevel@tonic-gate 		UnsignedInt64 v2 = o.getUnsignedInt64Value();
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 		return (new UnsignedInt64Statistic(
4467c478bd9Sstevel@tonic-gate 		    new UnsignedInt64(v1.add(v2)),
4477c478bd9Sstevel@tonic-gate 		        getStart(), getEnd()));
4487c478bd9Sstevel@tonic-gate 	}
449*55fea89dSDan Cross 
subtract(AggregateStatistic o)4507c478bd9Sstevel@tonic-gate 	public AggregateStatistic subtract(AggregateStatistic o)
4517c478bd9Sstevel@tonic-gate 	{
4527c478bd9Sstevel@tonic-gate 		UnsignedInt64 v1 = getUnsignedInt64Value();
4537c478bd9Sstevel@tonic-gate 		UnsignedInt64 v2 =  o.getUnsignedInt64Value();
4547c478bd9Sstevel@tonic-gate 
4557c478bd9Sstevel@tonic-gate 		return (new UnsignedInt64Statistic(
4567c478bd9Sstevel@tonic-gate 		    new UnsignedInt64(v1.subtract(v2)),
4577c478bd9Sstevel@tonic-gate 		        getStart(), getEnd()));
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
getSnapshotForInterval(Iterator it, Date start, Date end)4607c478bd9Sstevel@tonic-gate 	public AggregateStatistic getSnapshotForInterval(Iterator it,
4617c478bd9Sstevel@tonic-gate 	    Date start, Date end) throws IllegalArgumentException
4627c478bd9Sstevel@tonic-gate 	{
4637c478bd9Sstevel@tonic-gate 		BigInteger total = new BigInteger("0");
4647c478bd9Sstevel@tonic-gate 		int count = 0;
4657c478bd9Sstevel@tonic-gate 		Date first = start, last = end;
4667c478bd9Sstevel@tonic-gate 		while (it.hasNext()) {
4677c478bd9Sstevel@tonic-gate 			UnsignedInt64Statistic s = (UnsignedInt64Statistic)
4687c478bd9Sstevel@tonic-gate 			    it.next();
4697c478bd9Sstevel@tonic-gate 			if (start != null) {
4707c478bd9Sstevel@tonic-gate 				if (s.getStart().compareTo(start) < 0)
4717c478bd9Sstevel@tonic-gate 					continue;
4727c478bd9Sstevel@tonic-gate 			}
4737c478bd9Sstevel@tonic-gate 			if (first == null)
4747c478bd9Sstevel@tonic-gate 				first = s.getStart();
475*55fea89dSDan Cross 
4767c478bd9Sstevel@tonic-gate 			if (end != null) {
4777c478bd9Sstevel@tonic-gate 				if (s.getEnd().compareTo(end) > 0)
4787c478bd9Sstevel@tonic-gate 					continue;
4797c478bd9Sstevel@tonic-gate 			}
4807c478bd9Sstevel@tonic-gate 			last = s.getEnd();
4817c478bd9Sstevel@tonic-gate 			total = total.add(s.getUnsignedInt64Value());
4827c478bd9Sstevel@tonic-gate 			count++;
4837c478bd9Sstevel@tonic-gate 		}
4847c478bd9Sstevel@tonic-gate 		if (count == 0)
4857c478bd9Sstevel@tonic-gate 			throw new IllegalArgumentException("Cannot derive a " +
4867c478bd9Sstevel@tonic-gate 			    "snapshot from an empty iterator");
4877c478bd9Sstevel@tonic-gate 		return (new UnsignedInt64Statistic(
4887c478bd9Sstevel@tonic-gate 		    new UnsignedInt64(total.divide(new BigInteger(
4897c478bd9Sstevel@tonic-gate 	            Integer.toString(count)))), first, last));
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate }
492