package com.sun.solaris.domain.pools;
import java.math.BigInteger;
import java.util.Date;
import java.util.Iterator;
import java.text.*;
import com.sun.solaris.service.pools.UnsignedInt64;
interface Statistic
{
public Date getStart();
public Date getEnd();
public Object getValue();
public Long getLongValue();
public Double getDoubleValue();
public UnsignedInt64 getUnsignedInt64Value();
}
interface AggregateStatistic extends Statistic
{
public AggregateStatistic add(AggregateStatistic o);
public AggregateStatistic subtract(AggregateStatistic o);
public AggregateStatistic getSnapshotForInterval(Iterator it,
Date start, Date end) throws IllegalArgumentException;
}
abstract class AbstractStatistic implements Statistic
{
private final Object value;
private final Date start;
private final Date end;
private static final DateFormat df = new SimpleDateFormat("kk:mm:ss");
protected AbstractStatistic(Object value)
{
this(value, null, null);
}
protected AbstractStatistic(Object value, Date start, Date end)
{
this.value = value;
this.start = start;
this.end = end;
}
public Date getStart()
{
return (start);
}
public Date getEnd()
{
return (end);
}
public Object getValue()
{
return (value);
}
public abstract Long getLongValue();
public abstract Double getDoubleValue();
public abstract UnsignedInt64 getUnsignedInt64Value();
public String toString()
{
StringBuffer buf = new StringBuffer();
buf.append(value);
if (start != null && end != null) {
buf.append(" from ");
buf.append(df.format(start));
buf.append(" to ");
buf.append(df.format(end));
}
return (buf.toString());
}
}
final class DoubleStatistic extends AbstractStatistic
implements AggregateStatistic
{
public DoubleStatistic(Double value)
{
super(value);
}
public DoubleStatistic(Double value, Date start, Date end)
{
super(value, start, end);
}
public Double getDoubleValue()
{
return ((Double) getValue());
}
public Long getLongValue()
{
return (Long.valueOf(((Double) getValue()).longValue()));
}
public UnsignedInt64 getUnsignedInt64Value()
{
return (new UnsignedInt64(Long.toString(((Double) getValue()).
longValue())));
}
public AggregateStatistic add(AggregateStatistic o)
{
Double v1 = getDoubleValue();
Double v2 = o.getDoubleValue();
return (new DoubleStatistic(Double.valueOf(v1.doubleValue() +
v2.doubleValue()),
getStart(), getEnd()));
}
public AggregateStatistic subtract(AggregateStatistic o)
{
Double v1 = getDoubleValue();
Double v2 = o.getDoubleValue();
return (new DoubleStatistic(Double.valueOf(v1.doubleValue() -
v2.doubleValue()),
getStart(), getEnd()));
}
public AggregateStatistic getSnapshotForInterval(Iterator it,
Date start, Date end) throws IllegalArgumentException
{
double total = 0;
int count = 0;
Date first = start, last = end;
while (it.hasNext()) {
DoubleStatistic s = (DoubleStatistic) it.next();
if (start != null) {
if (s.getStart().compareTo(start) < 0)
continue;
}
if (first == null)
first = s.getStart();
if (end != null) {
if (s.getEnd().compareTo(end) > 0)
continue;
}
last = s.getEnd();
total += s.getDoubleValue().doubleValue();
count++;
}
if (count == 0)
throw new IllegalArgumentException("Cannot derive a " +
"snapshot from an empty iterator");
return (new DoubleStatistic(Double.valueOf(total / count),
first, last));
}
}
final class LongStatistic extends AbstractStatistic
implements AggregateStatistic
{
public LongStatistic(Long value, Date start, Date end)
{
super(value, start, end);
}
public Double getDoubleValue()
{
return (Double.valueOf(((Long) getValue()).longValue()));
}
public Long getLongValue()
{
return ((Long) getValue());
}
public UnsignedInt64 getUnsignedInt64Value()
{
return (new UnsignedInt64(Long.toString(((Long) getValue()).
longValue())));
}
public AggregateStatistic add(AggregateStatistic o)
{
Long v1 = getLongValue();
Long v2 = o.getLongValue();
return (new LongStatistic(Long.valueOf(v1.longValue() +
v2.longValue()),
getStart(), getEnd()));
}
public AggregateStatistic subtract(AggregateStatistic o)
{
Long v1 = getLongValue();
Long v2 = o.getLongValue();
return (new LongStatistic(Long.valueOf(v1.longValue() -
v2.longValue()),
getStart(), getEnd()));
}
public AggregateStatistic getSnapshotForInterval(Iterator it,
Date start, Date end) throws IllegalArgumentException
{
long total = 0;
int count = 0;
Date first = start, last = end;
while (it.hasNext()) {
LongStatistic s = (LongStatistic) it.next();
if (start != null) {
if (s.getStart().compareTo(start) < 0)
continue;
}
if (first == null)
first = s.getStart();
if (end != null) {
if (s.getEnd().compareTo(end) > 0)
continue;
}
last = s.getEnd();
total += s.getLongValue().longValue();
count++;
}
if (count == 0)
throw new IllegalArgumentException("Cannot derive a " +
"snapshot from an empty iterator");
return (new LongStatistic(Long.valueOf(total / count), first,
last));
}
}
final class UnsignedInt64Statistic extends AbstractStatistic
implements AggregateStatistic
{
public UnsignedInt64Statistic(UnsignedInt64 value, Date start,
Date end)
{
super(value, start, end);
}
public Double getDoubleValue()
{
return (Double.valueOf(
((UnsignedInt64) getValue()).longValue()));
}
public Long getLongValue()
{
return (Long.valueOf(((UnsignedInt64) getValue()).longValue()));
}
public UnsignedInt64 getUnsignedInt64Value()
{
return ((UnsignedInt64) getValue());
}
public AggregateStatistic add(AggregateStatistic o)
{
UnsignedInt64 v1 = getUnsignedInt64Value();
UnsignedInt64 v2 = o.getUnsignedInt64Value();
return (new UnsignedInt64Statistic(
new UnsignedInt64(v1.add(v2)),
getStart(), getEnd()));
}
public AggregateStatistic subtract(AggregateStatistic o)
{
UnsignedInt64 v1 = getUnsignedInt64Value();
UnsignedInt64 v2 = o.getUnsignedInt64Value();
return (new UnsignedInt64Statistic(
new UnsignedInt64(v1.subtract(v2)),
getStart(), getEnd()));
}
public AggregateStatistic getSnapshotForInterval(Iterator it,
Date start, Date end) throws IllegalArgumentException
{
BigInteger total = new BigInteger("0");
int count = 0;
Date first = start, last = end;
while (it.hasNext()) {
UnsignedInt64Statistic s = (UnsignedInt64Statistic)
it.next();
if (start != null) {
if (s.getStart().compareTo(start) < 0)
continue;
}
if (first == null)
first = s.getStart();
if (end != null) {
if (s.getEnd().compareTo(end) > 0)
continue;
}
last = s.getEnd();
total = total.add(s.getUnsignedInt64Value());
count++;
}
if (count == 0)
throw new IllegalArgumentException("Cannot derive a " +
"snapshot from an empty iterator");
return (new UnsignedInt64Statistic(
new UnsignedInt64(total.divide(new BigInteger(
Integer.toString(count)))), first, last));
}
}