PageRenderTime 29ms CodeModel.GetById 12ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 1ms

/testability-explorer/src/main/java/com/google/test/metric/report/Diff.java

http://testability-explorer.googlecode.com/
Java | 145 lines | 105 code | 20 blank | 20 comment | 13 complexity | 9cd46b9d936224082c706420a2ebeec8 MD5 | raw file
  1/*
  2 * Copyright 2007 Google Inc.
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5 * use this file except in compliance with the License. You may obtain a copy of
  6 * the License at
  7 *
  8 * http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 13 * License for the specific language governing permissions and limitations under
 14 * the License.
 15 */
 16package com.google.test.metric.report;
 17
 18import java.util.Collections;
 19import java.util.Comparator;
 20import java.util.List;
 21
 22/**
 23 * Simple bean to store a difference between two testability reports.
 24 * @author alexeagle@google.com (Alex Eagle)
 25 *
 26 */
 27public class Diff {
 28
 29  private final List<ClassDiff> classDiffs;
 30
 31  public Diff(List<ClassDiff> classDiffs) {
 32    this.classDiffs = classDiffs;
 33  }
 34
 35  public List<ClassDiff> getClassDiffs() {
 36    return classDiffs;
 37  }
 38
 39  public void sort() {
 40    Collections.sort(classDiffs, new ClassDeltaComparator());
 41    for (ClassDiff classDiff : classDiffs) {
 42      Collections.sort(classDiff.methodDiffs, new MethodDeltaComparator());
 43    }
 44  }
 45
 46  public static class ClassDiff {
 47    private final Integer oldMetric;
 48    private final Integer newMetric;
 49    private final List<MethodDiff> methodDiffs;
 50    private final String className;
 51
 52    @SuppressWarnings("unchecked")
 53	public ClassDiff(String className, Integer oldMetric, Integer newMetric) {
 54      this.className = className;
 55      this.oldMetric = oldMetric;
 56      this.newMetric = newMetric;
 57      this.methodDiffs = Collections.EMPTY_LIST;
 58    }
 59
 60    public ClassDiff(String className, Integer oldMetric, Integer newMetric, List<MethodDiff> methodDiffs) {
 61      this.className = className;
 62      this.oldMetric = oldMetric;
 63      this.newMetric = newMetric;
 64      this.methodDiffs = methodDiffs;
 65    }
 66
 67    public String getClassName() {
 68      return className;
 69    }
 70    public Integer getNewMetric() {
 71      return newMetric;
 72    }
 73    public Integer getOldMetric() {
 74      return oldMetric;
 75    }
 76
 77    public List<MethodDiff> getMethodDiffs() {
 78      return methodDiffs;
 79    }
 80
 81    public Integer getDelta() {
 82      if (newMetric == null) {
 83        return -oldMetric;
 84      }
 85      if (oldMetric == null) {
 86        return newMetric;
 87      }
 88      return (newMetric - oldMetric);
 89    }
 90  }
 91
 92  public static class MethodDiff {
 93    private final Integer oldMetric;
 94    private final Integer newMetric;
 95    private final String methodName;
 96
 97    public MethodDiff(String methodName, Integer oldMetric, Integer newMetric) {
 98      this.oldMetric = oldMetric;
 99      this.newMetric = newMetric;
100      this.methodName = methodName;
101    }
102
103    public Integer getOldMetric() {
104      return oldMetric;
105    }
106
107    public Integer getNewMetric() {
108      return newMetric;
109    }
110
111    public String getMethodName() {
112      return methodName;
113    }
114
115    public Integer getDelta() {
116      if (newMetric == null) {
117        return -oldMetric;
118      }
119      if (oldMetric == null) {
120        return newMetric;
121      }
122      return (newMetric - oldMetric);
123    }
124  }
125
126  private static class ClassDeltaComparator implements Comparator<ClassDiff> {
127    public int compare(ClassDiff classDiff, ClassDiff classDiff1) {
128      int result = classDiff.getDelta().compareTo(classDiff1.getDelta());
129      if (result == 0) {
130        result = classDiff.getClassName().compareTo(classDiff1.getClassName());
131      }
132      return result;
133    }
134  }
135
136  private static class MethodDeltaComparator implements Comparator<MethodDiff> {
137    public int compare(MethodDiff methodDiff, MethodDiff methodDiff1) {
138      int result = methodDiff.getDelta().compareTo(methodDiff1.getDelta());
139      if (result == 0) {
140        result = methodDiff.getMethodName().compareTo(methodDiff.getMethodName());
141      }
142      return result;
143    }
144  }
145}