creadur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject svn commit: r1373062 - in /creadur/whisker/trunk/apache-whisker-model/src/main/java/org/apache/creadur/whisker/model: License.java LicenseTemplateException.java
Date Tue, 14 Aug 2012 20:15:22 GMT
Author: rdonkin
Date: Tue Aug 14 20:15:22 2012
New Revision: 1373062

URL: http://svn.apache.org/viewvc?rev=1373062&view=rev
Log:
Thanks to PMD for spotting this unfinished class...

Modified:
    creadur/whisker/trunk/apache-whisker-model/src/main/java/org/apache/creadur/whisker/model/License.java
    creadur/whisker/trunk/apache-whisker-model/src/main/java/org/apache/creadur/whisker/model/LicenseTemplateException.java

Modified: creadur/whisker/trunk/apache-whisker-model/src/main/java/org/apache/creadur/whisker/model/License.java
URL: http://svn.apache.org/viewvc/creadur/whisker/trunk/apache-whisker-model/src/main/java/org/apache/creadur/whisker/model/License.java?rev=1373062&r1=1373061&r2=1373062&view=diff
==============================================================================
--- creadur/whisker/trunk/apache-whisker-model/src/main/java/org/apache/creadur/whisker/model/License.java
(original)
+++ creadur/whisker/trunk/apache-whisker-model/src/main/java/org/apache/creadur/whisker/model/License.java
Tue Aug 14 20:15:22 2012
@@ -133,7 +133,7 @@ public class License implements Comparab
 
         if (!parametersMatch(parameters, this.expectedParameters)) {
             throw LicenseTemplateException.parameterMismatch(
-                    this.expectedParameters, parameters.keySet());
+                    this.expectedParameters, parameters.keySet(), getName());
         }
 
         return parameters;

Modified: creadur/whisker/trunk/apache-whisker-model/src/main/java/org/apache/creadur/whisker/model/LicenseTemplateException.java
URL: http://svn.apache.org/viewvc/creadur/whisker/trunk/apache-whisker-model/src/main/java/org/apache/creadur/whisker/model/LicenseTemplateException.java?rev=1373062&r1=1373061&r2=1373062&view=diff
==============================================================================
--- creadur/whisker/trunk/apache-whisker-model/src/main/java/org/apache/creadur/whisker/model/LicenseTemplateException.java
(original)
+++ creadur/whisker/trunk/apache-whisker-model/src/main/java/org/apache/creadur/whisker/model/LicenseTemplateException.java
Tue Aug 14 20:15:22 2012
@@ -29,77 +29,99 @@ public class LicenseTemplateException ex
 
     /** Exception are serializable, so provide a SUID. */
     private static final long serialVersionUID = -4085365930968672572L;
+    /** Names the erroneous license */
+    private final String licenseName;
 
     /**
-     * Builds an instance.
-     * @param parameters not null
-     * @param name not null
-     * @return not null 
+     * Builds an exception indicating that parameter passed
+     * do not fulfill expectations.
+     * @param expectedParameters not null
+     * @param actualParameters not null
+     * @param licenseName not null
+     * @return not null
      */
-    public static LicenseTemplateException notLicenseTemplate(
-            final Map<String, String> parameters, final String name) {
-        return new LicenseTemplateException("This is not a templated license",
-                parameters, name);
+    public static LicenseTemplateException parameterMismatch(
+            final Collection<String> expectedParameters,
+            final Collection<String> actualParameters,
+            final String licenseName) {
+        final StringBuilder message = new StringBuilder("Parameter mismatch for license '");
+        message.append(licenseName);
+        message.append('.');
+        explainDiffs(expectedParameters, actualParameters, message, " Missing parameters:
");
+        explainDiffs(actualParameters, expectedParameters, message, " Unexpected parameters:
");
+        return new LicenseTemplateException(message.toString(), licenseName);
     }
 
     /**
-     * Constructs an instance.
+     * Explains the differences between the sample and the base.
+     * @param sample not null
+     * @param base not null
      * @param message not null
-     * @param parameters parameters passed
-     * @param name license name
+     * @param preamble not null
      */
-    private LicenseTemplateException(final String message,
-            final Map<String, String> parameters, final String name) {
-        // TODO: improve reporting
-        super(message);
+    private static void explainDiffs(final Collection<String> sample,
+            final Collection<String> base, final StringBuilder message,
+            final String preamble) {
+        boolean first = true;
+        for (final String expected:sample) {
+            if (!base.contains(expected)) {
+                if (first) {
+                    first = false;
+                    message.append(preamble);
+                } else {
+                    message.append(", ");
+                }
+                message.append(expected);
+            }
+        }
+        if (!first) {
+            message.append('.');
+        }
     }
 
+    
     /**
-     * Constructs an instance.
-     * @param message not null
-     * @param expectedParameters not null
-     * @param actualParameters not null
+     * Builds an instance.
+     * @param parameters not null
+     * @param licenseName not null
+     * @return not null 
      */
-    private LicenseTemplateException(final String message,
-            final Collection<String> expectedParameters,
-            final Collection<String> actualParameters) {
-        // TODO improve reporting
-        super(message);
+    public static LicenseTemplateException notLicenseTemplate(
+            final Map<String, String> parameters, final String licenseName) {
+        final StringBuilder message = new StringBuilder("'");
+        message.append(licenseName);
+        message.append("' is not a templated license but parameters were set (");
+        boolean first = true;
+        for (final String name:parameters.keySet()) {
+            if (first) {
+                first = false;
+            } else {
+                message.append(", ");
+            }
+            message.append(name);
+        }
+        message.append(")");
+        return new LicenseTemplateException(message.toString(), licenseName);
     }
 
     /**
-     * Constructs an instance
+     * Constructs an instance.
      * @param message not null
-     * @param name not null
+     * @param parameters parameters passed
+     * @param licenseName license name, not null
      */
-    private LicenseTemplateException(final String message, final String name) {
-        // TODO improve reporting
+    private LicenseTemplateException(final String message, final String licenseName) {
         super(message);
+        this.licenseName = licenseName;
     }
 
     /**
-     * Builds an exception indicating that parameter passed
-     * do not fulfill expectations.
-     * @param expectedParameters not null
-     * @param actualParameters not null
-     * @return not null
-     */
-    public static LicenseTemplateException parameterMismatch(
-            final Collection<String> expectedParameters,
-            final Collection<String> actualParameters) {
-        return new LicenseTemplateException("Parameter mismatch.",
-                expectedParameters, actualParameters);
-    }
-
-    /**
-     * Builds an exception indicating that duplicate parameter
-     * names have been passed.
-     * @param name names the parameter duplicated, not null
-     * @return not null
+     * Gets the name of the erroneous license.
+     * @return license name, not null
      */
-    public static LicenseTemplateException duplicateParameterName(
-            final String name) {
-        return new LicenseTemplateException("Duplicate parameter name.", name);
+    public String getLicenseName() {
+        return licenseName;
     }
 
+    
 }
\ No newline at end of file



Mime
View raw message