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

http://testability-explorer.googlecode.com/ · Java · 117 lines · 78 code · 19 blank · 20 comment · 8 complexity · ed957b6256e1a54a47325ae6a7cf5cdd MD5 · raw file

  1. /*
  2. * Copyright 2009 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. */
  16. package com.google.test.metric.report.issues;
  17. import java.util.Comparator;
  18. import com.google.common.base.Nullable;
  19. import com.google.common.base.Predicate;
  20. import com.google.test.metric.SourceLocation;
  21. /**
  22. * A model of a single reportable issue with the class under analysis.
  23. *
  24. * @author alexeagle@google.com (Alex Eagle)
  25. */
  26. public class Issue implements IssueHolder {
  27. private final String element;
  28. private float contributionToClassCost;
  29. private boolean isLineNumberApproximate;
  30. private IssueType type;
  31. private IssueSubType subType;
  32. private SourceLocation location;
  33. public Issue(SourceLocation location, String element, float contributionToClassCost,
  34. IssueType type, IssueSubType subType) {
  35. this.location = location;
  36. this.element = element;
  37. this.contributionToClassCost = contributionToClassCost;
  38. this.type = type;
  39. this.subType = subType;
  40. }
  41. public void setContributionToClassCost(float contributionToClassCost) {
  42. this.contributionToClassCost = contributionToClassCost;
  43. }
  44. public String getElement() {
  45. return element;
  46. }
  47. public float getContributionToClassCost() {
  48. return contributionToClassCost;
  49. }
  50. public boolean isLineNumberApproximate() {
  51. return isLineNumberApproximate;
  52. }
  53. public void setLineNumberIsApproximate(boolean isApprox) {
  54. this.isLineNumberApproximate = isApprox;
  55. }
  56. @Override
  57. public String toString() {
  58. return String.format("On line %d, element %s with contribution %f (%s/%s)",
  59. location.getLineNumber(), element, contributionToClassCost, type, subType);
  60. }
  61. public void setType(IssueType type) {
  62. this.type = type;
  63. }
  64. public IssueType getType() {
  65. return type;
  66. }
  67. public IssueSubType getSubType() {
  68. return subType;
  69. }
  70. public void setSubType(IssueSubType subType) {
  71. this.subType = subType;
  72. }
  73. public boolean isEmpty() {
  74. return 0f == contributionToClassCost;
  75. }
  76. public float getTotalCost() {
  77. return contributionToClassCost;
  78. }
  79. public SourceLocation getLocation() {
  80. return location;
  81. }
  82. public static Predicate<? super Issue> isType(final IssueType issueType,
  83. final IssueSubType subType) {
  84. return new Predicate<Issue>() {
  85. public boolean apply(@Nullable Issue issue) {
  86. if (issue.getType() == null || issue.getSubType() == null) {
  87. return false;
  88. }
  89. return issue.getType() == issueType && issue.getSubType() == subType;
  90. }
  91. };
  92. }
  93. public static class TotalCostComparator implements Comparator<Issue> {
  94. public int compare(Issue issue, Issue issue1) {
  95. return Float.compare(issue.getContributionToClassCost(), issue1.getContributionToClassCost());
  96. }
  97. }
  98. }