PDCIStatistics.java
/**
* Copyright 2015 Global Crop Diversity Trust
*
* Licensed 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 org.genesys.server.model.genesys;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
import java.util.List;
/**
* Institute PDCI statistics
*/
@Getter
@Setter
public class PDCIStatistics implements Serializable {
private static final long serialVersionUID = -3576941362217038857L;
private Double min = null;
private Double max = null;
private Double avg = null;
private long[] histogram = new long[21];
private Long count;
public void setMin(Number value) {
this.min = value == null ? null : value.doubleValue();
}
public void setMax(Number value) {
this.max = value == null ? null : value.doubleValue();
}
public void setAvg(Number value) {
this.avg = value == null ? null : value.doubleValue();
}
public void setCount(Number value) {
this.count = value == null ? 0 : value.longValue();
}
/**
* Converts {@link #histogram} to flot.categories compatible JSON
*
* @return
*/
public String getHistogramJson() {
StringBuffer sb = new StringBuffer();
sb.append("[ ");
for (int i = 0; i < histogram.length; i++) {
if (i > 0)
sb.append(", ");
sb.append("['").append(i / 2.0).append("', ").append(histogram[i]).append("]");
}
sb.append(" ]");
return sb.toString();
}
/**
* Fill {@link #histogram} with data
*
* @param hist
*/
public void makeHistogram(List<Object[]> hist) {
for (Object[] h : hist) {
int index = (int) Math.round(((Number) h[0]).doubleValue() * 2);
long count = ((Number) h[1]).longValue();
histogram[index] = count;
}
}
public void updateMin(Number updateMin) {
if (updateMin == null)
return;
this.min = this.min == null || this.min > updateMin.doubleValue() ? updateMin.doubleValue() : this.min;
}
public void updateMax(Number updateMax) {
if (updateMax == null)
return;
this.max = this.max == null || this.max < updateMax.doubleValue() ? updateMax.doubleValue() : this.max;
}
public void updateCountAndAvg(Number count, Number avg) {
if (count == null || count.longValue() == 0 || avg == null)
return;
this.avg = this.avg == null || this.count == null || this.count == 0 ? avg.doubleValue() : (this.avg * this.count + avg.doubleValue() * count.doubleValue()) / (this.count + count.doubleValue());
this.count = this.count==null ? count.longValue() : this.count + count.longValue();
}
public void updateHistogram(long[] updateHistogram) {
if (updateHistogram == null) {
return;
}
for (int i = 0; i < this.histogram.length; i++) {
this.histogram[i] += updateHistogram[i];
}
}
/**
* Return object array to be used for i18n
*
* @return
*/
public Object[] getElStats() {
return new Object[] { count, avg, min, max };
}
public PDCIStatistics merge(PDCIStatistics other) {
if (other != null) {
updateMin(other.getMin());
updateMax(other.getMax());
updateCountAndAvg(other.getCount(), other.getAvg());
updateHistogram(other.getHistogram());
}
return this;
}
}