1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.creadur.whisker.model;
20
21 import java.util.Collection;
22 import java.util.Map;
23
24 /**
25 * Indicates that generating an instance of a license
26 * from the template (for the license family) has failed.
27 */
28 public class LicenseTemplateException extends Exception {
29
30 /** Exception are serializable, so provide a SUID. */
31 private static final long serialVersionUID = -4085365930968672572L;
32 /** Names the erroneous license */
33 private final String licenseName;
34
35 /**
36 * Builds an exception indicating that parameter passed
37 * do not fulfill expectations.
38 * @param expectedParameters not null
39 * @param actualParameters not null
40 * @param licenseName not null
41 * @return not null
42 */
43 public static LicenseTemplateException parameterMismatch(
44 final Collection<String> expectedParameters,
45 final Collection<String> actualParameters,
46 final String licenseName) {
47 final StringBuilder message = new StringBuilder("Parameter mismatch for license '");
48 message.append(licenseName);
49 message.append('.');
50 explainDiffs(expectedParameters, actualParameters, message, " Missing parameters: ");
51 explainDiffs(actualParameters, expectedParameters, message, " Unexpected parameters: ");
52 return new LicenseTemplateException(message.toString(), licenseName);
53 }
54
55 /**
56 * Explains the differences between the sample and the base.
57 * @param sample not null
58 * @param base not null
59 * @param message not null
60 * @param preamble not null
61 */
62 private static void explainDiffs(final Collection<String> sample,
63 final Collection<String> base, final StringBuilder message,
64 final String preamble) {
65 boolean first = true;
66 for (final String expected:sample) {
67 if (!base.contains(expected)) {
68 if (first) {
69 first = false;
70 message.append(preamble);
71 } else {
72 message.append(", ");
73 }
74 message.append(expected);
75 }
76 }
77 if (!first) {
78 message.append('.');
79 }
80 }
81
82
83 /**
84 * Builds an instance.
85 * @param parameters not null
86 * @param licenseName not null
87 * @return not null
88 */
89 public static LicenseTemplateException notLicenseTemplate(
90 final Map<String, String> parameters, final String licenseName) {
91 final StringBuilder message = new StringBuilder("'");
92 message.append(licenseName);
93 message.append("' is not a templated license but parameters were set (");
94 boolean first = true;
95 for (final String name:parameters.keySet()) {
96 if (first) {
97 first = false;
98 } else {
99 message.append(", ");
100 }
101 message.append(name);
102 }
103 message.append(")");
104 return new LicenseTemplateException(message.toString(), licenseName);
105 }
106
107 /**
108 * Constructs an instance.
109 * @param message not null
110 * @param parameters parameters passed
111 * @param licenseName license name, not null
112 */
113 private LicenseTemplateException(final String message, final String licenseName) {
114 super(message);
115 this.licenseName = licenseName;
116 }
117
118 /**
119 * Gets the name of the erroneous license.
120 * @return license name, not null
121 */
122 public String getLicenseName() {
123 return licenseName;
124 }
125
126
127 }