Java tutorial
/* * Copyright 2014 by Yields. * * 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 io.yields.math.framework.kpi; import com.fasterxml.jackson.annotation.JsonIgnore; import org.apache.commons.lang.builder.CompareToBuilder; import java.time.ZonedDateTime; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.TreeMap; import static java.util.Comparator.comparing; import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toMap; import static org.apache.commons.lang.StringUtils.isNotBlank; /** * Outcome of a score calculation. */ public class ScoreResult implements Comparable<ScoreResult> { private String name; private String group; private boolean official; private double score; private ZonedDateTime timestamp; @JsonIgnore private Map<String, Double> kpiResults; private List<KPIResult> results; /** * For JSON */ public ScoreResult() { } public ScoreResult(String name, String group, boolean official, double score, Map<String, Double> kpiResults) { this.name = name; this.group = group; this.official = official; this.score = score; this.kpiResults = kpiResults; this.results = kpiResults.entrySet().stream().map(entry -> new KPIResult(entry.getKey(), entry.getValue())) .collect(toList()); } public Map<String, Double> getKpiResults() { if (kpiResults == null) { if (results != null) { kpiResults = new TreeMap<>( results.stream().collect(toMap(KPIResult::getName, KPIResult::getValue))); } else { kpiResults = new TreeMap<>(); } } return kpiResults; } public List<KPIResult> getResults() { return results; } public void setKpiResults(Map<String, Double> kpiResults) { this.kpiResults = kpiResults; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGroup() { return group; } public void setGroup(String group) { this.group = group; } public boolean isOfficial() { return official; } public void setOfficial(boolean official) { this.official = official; } public double getScore() { return score; } @JsonIgnore public String getFormattedScore() { return String.format("%.4f", score); } public void setScore(double score) { this.score = score; } public ZonedDateTime getTimestamp() { return timestamp; } public void setTimestamp(ZonedDateTime timestamp) { this.timestamp = timestamp; } @JsonIgnore public List<KPIResult> getScores() { return getKpiResults().entrySet().stream().map(entry -> new KPIResult(entry.getKey(), entry.getValue())) .sorted(comparing(KPIResult::getName)).collect(toList()); } @JsonIgnore public boolean isReportable() { return isNotBlank(name) && isNotBlank(group) && results != null && !results.isEmpty(); } public static ScoreResult empty() { return new ScoreResult("", "", false, 0d, new HashMap<>()); } @Override public int compareTo(ScoreResult other) { return new CompareToBuilder().append(other.getScore(), this.score).append(other.getName(), this.name) .toComparison(); } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } ScoreResult that = (ScoreResult) o; if (official != that.official) { return false; } if (Double.compare(that.score, score) != 0) { return false; } if (group != null ? !group.equals(that.group) : that.group != null) { return false; } if (name != null ? !name.equals(that.name) : that.name != null) { return false; } return true; } @Override public int hashCode() { int result; long temp; result = name != null ? name.hashCode() : 0; result = 31 * result + (group != null ? group.hashCode() : 0); result = 31 * result + (official ? 1 : 0); temp = Double.doubleToLongBits(score); result = 31 * result + (int) (temp ^ (temp >>> 32)); return result; } public static class KPIResult { private String name; private Double value; /** * For JSON */ public KPIResult() { } public KPIResult(String name, Double value) { this.name = name; this.value = value; } public String getName() { return name; } public Double getValue() { return value; } @JsonIgnore public String getFormattedValue() { return String.format("%.4f", value); } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } KPIResult kpiResult = (KPIResult) o; if (name != null ? !name.equals(kpiResult.name) : kpiResult.name != null) { return false; } if (value != null ? !value.equals(kpiResult.value) : kpiResult.value != null) { return false; } return true; } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + (value != null ? value.hashCode() : 0); return result; } } }