Java tutorial
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.loadtesting.core.data; import java.io.Serializable; import java.util.List; import java.util.concurrent.TimeUnit; import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics; import org.apache.commons.math3.stat.descriptive.StatisticalSummary; import com.loadtesting.core.calltree.stat.cache.CapturerConfig; import com.loadtesting.core.sample.TimeSample; /** * @author Woranop Chaiyakulvat(nineworanop@gmail.com) */ public class TimeSerieData implements Serializable, StatisticalSummary { private static final long serialVersionUID = -3936670866527022117L; /** * Close */ private double closing; private double geometricMean; /** * High (Max) */ private double high; private double kurtosis; /** * Low (Min) */ private double low; /** * Median (HL/2) */ private double median; private String name; /** * Open */ private double opening; private double populationVariance; private List<TimeSample> samples; /** * Standard Deviation */ private double sd; private double skewness; /** * Simple moving average (Arithmetic mean) */ private double sma; /** * the sum of the available values */ private double sum; /** * sum of the squares of the available values */ private double sumsq; /** * Typical (HLC/3) */ private double typical; private TimeUnit unit; /** * Variance */ private double variance; /** * Volume of bar */ private int volume; /** * Weighted close (HLCC/4) */ private double weightedClose; public TimeSerieData(String name, List<TimeSample> samples, CapturerConfig config) { this.name = name; this.unit = config.getUnit(); this.volume = samples.size(); if (volume > 0) { TimeSample first = samples.get(0); this.unit = first.getTimeUnit(); this.opening = first.getTime(unit); TimeSample last = samples.get(volume - 1); this.closing = last.getTime(unit); this.samples = config.getFilter().filter(samples); DescriptiveStatistics stats = new DescriptiveStatistics(volume); for (TimeSample timeSample : samples) { stats.addValue(timeSample.getTime(unit)); } this.high = stats.getMax(); this.low = stats.getMin(); this.median = (high + low) / 2; this.typical = (high + low + closing) / 3; this.weightedClose = (high + low + closing + closing) / 4; this.sma = stats.getMean(); this.variance = stats.getVariance(); this.sd = stats.getStandardDeviation(); this.sum = stats.getSum(); this.sumsq = stats.getSumsq(); this.skewness = stats.getSkewness(); this.kurtosis = stats.getKurtosis(); this.geometricMean = stats.getGeometricMean(); this.populationVariance = stats.getPopulationVariance(); } else { this.samples = samples; } } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; TimeSerieData other = (TimeSerieData) obj; if (Double.doubleToLongBits(closing) != Double.doubleToLongBits(other.closing)) return false; if (Double.doubleToLongBits(geometricMean) != Double.doubleToLongBits(other.geometricMean)) return false; if (Double.doubleToLongBits(high) != Double.doubleToLongBits(other.high)) return false; if (Double.doubleToLongBits(kurtosis) != Double.doubleToLongBits(other.kurtosis)) return false; if (Double.doubleToLongBits(low) != Double.doubleToLongBits(other.low)) return false; if (Double.doubleToLongBits(median) != Double.doubleToLongBits(other.median)) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; if (Double.doubleToLongBits(opening) != Double.doubleToLongBits(other.opening)) return false; if (Double.doubleToLongBits(populationVariance) != Double.doubleToLongBits(other.populationVariance)) return false; if (samples == null) { if (other.samples != null) return false; } else if (!samples.equals(other.samples)) return false; if (Double.doubleToLongBits(sd) != Double.doubleToLongBits(other.sd)) return false; if (Double.doubleToLongBits(skewness) != Double.doubleToLongBits(other.skewness)) return false; if (Double.doubleToLongBits(sma) != Double.doubleToLongBits(other.sma)) return false; if (Double.doubleToLongBits(sum) != Double.doubleToLongBits(other.sum)) return false; if (Double.doubleToLongBits(sumsq) != Double.doubleToLongBits(other.sumsq)) return false; if (Double.doubleToLongBits(typical) != Double.doubleToLongBits(other.typical)) return false; if (unit != other.unit) return false; if (Double.doubleToLongBits(variance) != Double.doubleToLongBits(other.variance)) return false; if (volume != other.volume) return false; if (Double.doubleToLongBits(weightedClose) != Double.doubleToLongBits(other.weightedClose)) return false; return true; } public double getClosing() { return closing; } public double getGeometricMean() { return geometricMean; } public double getHigh() { return high; } public double getKurtosis() { return kurtosis; } public double getLow() { return low; } public double getMax() { return high; } public double getMean() { return sma; } public double getMedian() { return median; } public double getMin() { return low; } public long getN() { return volume; } public String getName() { return name; } public double getOpening() { return opening; } public double getPopulationVariance() { return populationVariance; } public List<TimeSample> getSamples() { return samples; } public double getSd() { return sd; } public double getSkewness() { return skewness; } public double getSma() { return sma; } public double getStandardDeviation() { return sd; } public double getSum() { return sum; } public double getSumsq() { return sumsq; } public double getTypical() { return typical; } public TimeUnit getUnit() { return unit; } public double getVariance() { return variance; } public int getVolume() { return volume; } public double getWeightedClose() { return weightedClose; } @Override public int hashCode() { final int prime = 31; int result = 1; long temp; temp = Double.doubleToLongBits(closing); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(geometricMean); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(high); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(kurtosis); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(low); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(median); result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + ((name == null) ? 0 : name.hashCode()); temp = Double.doubleToLongBits(opening); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(populationVariance); result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + ((samples == null) ? 0 : samples.hashCode()); temp = Double.doubleToLongBits(sd); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(skewness); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(sma); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(sum); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(sumsq); result = prime * result + (int) (temp ^ (temp >>> 32)); temp = Double.doubleToLongBits(typical); result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + ((unit == null) ? 0 : unit.hashCode()); temp = Double.doubleToLongBits(variance); result = prime * result + (int) (temp ^ (temp >>> 32)); result = prime * result + volume; temp = Double.doubleToLongBits(weightedClose); result = prime * result + (int) (temp ^ (temp >>> 32)); return result; } public void setClosing(double closing) { this.closing = closing; } public void setGeometricMean(double geometricMean) { this.geometricMean = geometricMean; } public void setHigh(double high) { this.high = high; } public void setKurtosis(double kurtosis) { this.kurtosis = kurtosis; } public void setLow(double low) { this.low = low; } public void setMedian(double median) { this.median = median; } public void setName(String name) { this.name = name; } public void setOpening(double opening) { this.opening = opening; } public void setPopulationVariance(double populationVariance) { this.populationVariance = populationVariance; } public void setSamples(List<TimeSample> samples) { this.samples = samples; } public void setSd(double sd) { this.sd = sd; } public void setSkewness(double skewness) { this.skewness = skewness; } public void setSma(double sma) { this.sma = sma; } public void setSum(double sum) { this.sum = sum; } public void setSumsq(double sumsq) { this.sumsq = sumsq; } public void setTypical(double typical) { this.typical = typical; } public void setUnit(TimeUnit unit) { this.unit = unit; } public void setVariance(double variance) { this.variance = variance; } public void setVolume(int volume) { this.volume = volume; } public void setWeightedClose(double weightedClose) { this.weightedClose = weightedClose; } @Override public String toString() { return "TimeSerieData [name=" + name + ", unit=" + unit + ", samples=" + samples + ", volume=" + volume + ", opening=" + opening + ", closing=" + closing + ", low=" + low + ", high=" + high + ", sma=" + sma + ", median=" + median + ", typical=" + typical + ", weightedClose=" + weightedClose + ", variance=" + variance + ", sd=" + sd + ", sum=" + sum + ", sumsq=" + sumsq + ", skewness=" + skewness + ", kurtosis=" + kurtosis + ", geometricMean=" + geometricMean + ", populationVariance=" + populationVariance + "]"; } }