1   package net.sf.clirr.core.internal.checks;
2   
3   import net.sf.clirr.core.ApiDifference;
4   import net.sf.clirr.core.MessageTranslator;
5   import net.sf.clirr.core.Severity;
6   
7   /***
8    * Describes an expected API change.
9    */
10  
11  public final class ExpectedDiff
12  {
13      private static MessageTranslator translator = new MessageTranslator();
14  
15      private String report;
16      private Severity binaryCompatibilitySeverity;
17      private Severity sourceCompatibilitySeverity;
18      private String affectedClass;
19      private String affectedMethod;
20      private String affectedField;
21  
22      /***
23       * Create a new API difference representation.
24       *
25       * @param report a human readable string describing the change that was made, must be non-null.
26       * @param severity the severity in terms of binary and source code compatibility, must be non-null.
27       * @param clazz the fully qualified class name where the change occured, must be non-null.
28       * @param method the method signature of the method that changed, <code>null</code>
29       *   if no method was affected.
30       * @param field the field name where the change occured, <code>null</code>
31       *   if no field was affected.
32       */
33      public ExpectedDiff(String report, Severity severity, String clazz, String method, String field)
34      {
35          this(report, severity, severity, clazz, method, field);
36      }
37  
38      /***
39       * Create a new API difference representation.
40       *
41       * @param report a human readable string describing the change that was made, must be non-null.
42       * @param binarySeverity the severity in terms of binary compatibility, must be non-null.
43       * @param sourceSeverity the severity in terms of source code compatibility, must be non-null.
44       * @param clazz the fully qualified class name where the change occured, must be non-null.
45       * @param method the method signature of the method that changed, <code>null</code>
46       *   if no method was affected.
47       * @param field the field name where the change occured, <code>null</code>
48       *   if no field was affected.
49       */
50      public ExpectedDiff(String report, Severity binarySeverity, Severity sourceSeverity,
51                           String clazz, String method, String field)
52      {
53          checkNonNull(report);
54          checkNonNull(binarySeverity);
55          checkNonNull(sourceSeverity);
56          checkNonNull(clazz);
57  
58          this.report = report;
59          this.binaryCompatibilitySeverity = binarySeverity;
60          this.sourceCompatibilitySeverity = sourceSeverity;
61          this.affectedClass = clazz;
62          this.affectedField = field;
63          this.affectedMethod = method;
64      }
65  
66      /***
67       * Trivial utility method to verify that a specific object is non-null.
68       */
69      private void checkNonNull(Object o)
70      {
71          if (o == null)
72          {
73              throw new IllegalArgumentException();
74          }
75      }
76  
77      /***
78       * {@inheritDoc}
79       */
80      public String toString()
81      {
82          return report + " (" + binaryCompatibilitySeverity + ") - "
83                  + affectedClass + '[' + affectedField + '/' + affectedMethod + ']';
84      }
85  
86      /***
87       * Returns true if the provided ApiDifference object matches the
88       * expected value.
89       */
90      public boolean matches(ApiDifference diff)
91      {
92          if (!report.equals(diff.getReport(translator)))
93          {
94              return false;
95          }
96  
97          if (!binaryCompatibilitySeverity.equals(diff.getBinaryCompatibilitySeverity()))
98          {
99              return false;
100         }
101 
102         if (!sourceCompatibilitySeverity.equals(diff.getSourceCompatibilitySeverity()))
103         {
104             return false;
105         }
106 
107 
108         final String otherClass = diff.getAffectedClass();
109         if (!affectedClass.equals(otherClass))
110         {
111             return false;
112         }
113 
114         final String otherMethod = diff.getAffectedMethod();
115         if (affectedMethod != null ? !affectedMethod.equals(otherMethod) : otherMethod != null)
116         {
117             return false;
118         }
119 
120         final String otherField = diff.getAffectedField();
121         if (affectedField != null ? !affectedField.equals(otherField) : otherField != null)
122         {
123             return false;
124         }
125 
126         return true;
127     }
128 }