View Javadoc
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.app.analysis;
20  
21  import java.util.Collection;
22  import java.util.Collections;
23  import java.util.Map;
24  
25  /**
26   * Indicates an issue with a resource definition.
27   */
28  public final class ResourceDefinitionException extends Exception {
29  
30      /**
31       * Composes an informative error message.
32       * @param issues not null
33       * @return informative error message, not null
34       */
35      private static String message(
36              final Map<
37                  ResourceDefinitionError,
38                  Collection<ResourceDescription>> issues) {
39          final StringBuilder builder =
40                  new StringBuilder("Resources definitions are incorrect. ");
41          for (Map.Entry<
42                  ResourceDefinitionError,
43                  Collection<ResourceDescription>>
44                      entry: issues.entrySet()) {
45              final ResourceDefinitionError error = entry.getKey();
46              final Collection<ResourceDescription> resources = entry.getValue();
47              if (!resources.isEmpty()) {
48                  builder.append(error.getDescription()).append(": ");
49                  boolean firstTime = true;
50                  for (final ResourceDescription description: resources) {
51                      if (firstTime) {
52                          firstTime = false;
53                      } else {
54                          builder.append("; ");
55                      }
56                      builder
57                          .append(description.getResource())
58                          .append(" in ")
59                          .append(description.getDirectory());
60                  }
61                  builder.append(". ");
62              }
63          }
64          return builder.toString();
65      }
66  
67      /** For serialisation. */
68      private static final long serialVersionUID = -455455829914484243L;
69  
70      /**
71       * Causal issues.
72       */
73      private final Map<
74          ResourceDefinitionError,
75          Collection<ResourceDescription>> issues;
76  
77      /**
78       * Constructs an exception caused by the given issues.
79       * @param issues not null
80       */
81      public ResourceDefinitionException(
82              final Map<ResourceDefinitionError,
83              Collection<ResourceDescription>> issues) {
84          super(message(issues));
85          this.issues = Collections.unmodifiableMap(issues);
86      }
87  
88      /**
89       * Gets causal issues.
90       * @return the issues not null
91       */
92      public Map<
93                  ResourceDefinitionError,
94                  Collection<ResourceDescription>> getIssues() {
95          return issues;
96      }
97  }