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.plugins.kpi; import org.apache.commons.collections.CollectionUtils; import java.io.File; import java.util.ArrayList; import java.util.Collection; import java.util.Comparator; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeMap; import java.util.TreeSet; import hudson.model.AbstractBuild; import io.yields.math.framework.kpi.ScoreDAO; import io.yields.math.framework.kpi.ScoreResult; import static java.util.Collections.sort; import static org.apache.commons.lang.StringUtils.isBlank; /** * KPI Trend indicates the current KPIs and the evolution. */ public class KPIReport { private String reference; private Collection<ScoreResult> kpiScores; private KPIReportBuildAction buildAction; private KPIReport previousReport; private KPIReport(String reference, Collection<ScoreResult> kpiScores) { this.reference = reference; List<ScoreResult> sortedKpiScores = new ArrayList<ScoreResult>(kpiScores); sort(sortedKpiScores, new Comparator<ScoreResult>() { @Override public int compare(ScoreResult scoreResult, ScoreResult other) { return new Double(other.getScore()).compareTo(scoreResult.getScore()); } }); this.kpiScores = sortedKpiScores; } public static KPIReport forPath(File path) { Collection<ScoreResult> scoreResults = ScoreDAO.retrieve(path); return new KPIReport(path.getAbsolutePath(), scoreResults); } public static KPIReport forEmptyScores(File path) { Collection<ScoreResult> scoreResults = new ArrayList<ScoreResult>(); return new KPIReport(path.getAbsolutePath(), scoreResults); } public KPIReport withNextReport(KPIReport nextReport) { nextReport.setPreviousReport(this); return this; } public AbstractBuild<?, ?> getBuild() { return buildAction.getBuild(); } public Integer getBuildNumber() { return buildAction.getBuild() == null ? null : buildAction.getBuild().getNumber(); } public KPIReportBuildAction getBuildAction() { return buildAction; } void setBuildAction(KPIReportBuildAction buildAction) { this.buildAction = buildAction; } public String getReference() { return reference; } public KPIReport getPrevious() { return previousReport; } public void setPreviousReport(KPIReport previousReport) { this.previousReport = previousReport; } public Collection<ScoreResult> getKpiScores() { return kpiScores; } public Collection<KPIReportGroup> getKpiReportGroups() { Map<String, KPIReportGroup> groups = new TreeMap<String, KPIReportGroup>(); for (ScoreResult kpiScore : kpiScores) { KPIReportGroup group = groups.get(kpiScore.getGroup()); if (group == null) { group = new KPIReportGroup(kpiScore.getGroup(), kpiScore.isOfficial()); groups.put(kpiScore.getGroup(), group); } group.addKpiScore(kpiScore); } return new TreeSet<KPIReportGroup>(groups.values()); } public KPIReportGroup getKpiReportGroup(String groupName) { if (isBlank(groupName)) { // no group selected, take first one return getKpiReportGroups().iterator().next(); } else { KPIReportGroup group = null; for (ScoreResult kpiScore : kpiScores) { if (groupName.equals(kpiScore.getGroup())) { if (group == null) { group = new KPIReportGroup(kpiScore.getGroup(), kpiScore.isOfficial()); } group.addKpiScore(kpiScore); } } return group; } } public ScoreResult getKpiScore(String kpiScoreName) { for (ScoreResult scoreResult : kpiScores) { if (scoreResult.getName().equals(kpiScoreName)) { return scoreResult; } } return null; } public Set<String> getKpiScoreNames() { if (CollectionUtils.isEmpty(kpiScores)) { return new HashSet<String>(); } else { Set<String> kpiScoreNames = new HashSet<String>(); for (ScoreResult scoreResult : kpiScores) { kpiScoreNames.add(scoreResult.getName()); } return kpiScoreNames; } } public boolean hasScores() { return CollectionUtils.isNotEmpty(kpiScores); } }