ErrorMessages.java

  1. /*
  2.  * Copyright © 2017, Salesforce.com, Inc
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions are met:
  7.  *     * Redistributions of source code must retain the above copyright
  8.  *       notice, this list of conditions and the following disclaimer.
  9.  *     * Redistributions in binary form must reproduce the above copyright
  10.  *       notice, this list of conditions and the following disclaimer in the
  11.  *       documentation and/or other materials provided with the distribution.
  12.  *     * Neither the name of the <organization> nor the
  13.  *       names of its contributors may be used to endorse or promote products
  14.  *       derived from this software without specific prior written permission.
  15.  *
  16.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19.  * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
  20.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26.  */
  27. package com.salesforce.apt.graph.model.errors;

  28. import java.util.ArrayList;
  29. import java.util.Arrays;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;

  33. public class ErrorMessages {

  34.   private final Map<ErrorType, String> errorMessages = new HashMap<>();
  35.  
  36.   public String getMessage(ErrorType type) {
  37.     return errorMessages.get(type);
  38.   }
  39.  
  40.   public static Builder builder() {
  41.     return new Builder();
  42.   }
  43.  
  44.   public static class Builder {
  45.    
  46.     private final Map<ErrorType, String> errorMessages = new HashMap<>();
  47.    
  48.     public Builder cycleInDefinitionSources(String message) {
  49.       errorMessages.put(ErrorType.CYCLE_IN_DEFINITION_SOURCES, message);
  50.       return this;
  51.     }
  52.    
  53.     public Builder cycleInObjectDefinitions(String message) {
  54.       errorMessages.put(ErrorType.CYCLE_IN_OBJECT_DEFINITIONS, message);
  55.       return this;
  56.     }

  57.     public Builder duplicateObjectDefinitions(String message) {
  58.       errorMessages.put(ErrorType.DUPLICATE_OBJECT_DEFINITIONS, message);
  59.       return this;
  60.     }

  61.     public Builder nonLiteralStaticMemberVariables(String message) {
  62.       errorMessages.put(ErrorType.NONLITERAL_STATIC_MEMBER_VARIABLES, message);
  63.       return this;
  64.     }

  65.     public Builder knownDamagingClass(String message) {
  66.       errorMessages.put(ErrorType.KNOWN_DAMAGING_CLASS, message);
  67.       return this;
  68.     }

  69.     public Builder missingBeanDefinitions(String message) {
  70.       errorMessages.put(ErrorType.MISSING_BEAN_DEFINITIONS, message);
  71.       return this;
  72.     }
  73.    
  74.     public Builder missingRelevantAnnotations(String message) {
  75.       errorMessages.put(ErrorType.MISSING_NECESSARY_ANNOTATIONS, message);
  76.       return this;
  77.     }

  78.     public Builder unmatchedTypes(String message) {
  79.       errorMessages.put(ErrorType.UNMATCHED_TYPES, message);
  80.       return this;
  81.     }

  82.     public Builder unusedExpected(String message) {
  83.       errorMessages.put(ErrorType.UNUSED_EXPECTED, message);
  84.       return this;
  85.     }
  86.    
  87.     public Builder noMatchingDefinition(String message) {
  88.       errorMessages.put(ErrorType.NO_MATCHING_DEFINITIONS, message);
  89.       return this;
  90.     }
  91.    
  92.     public Builder duplicatedMatchingDependencies(String message) {
  93.       errorMessages.put(ErrorType.DUPLICATED_MATCHING_DEPENDENCIES, message);
  94.       return this;
  95.     }
  96.    
  97.     public Builder duplicatedMatchingDefinitions(String message) {
  98.       errorMessages.put(ErrorType.DUPLICATED_MATCHING_DEFINITIONS, message);
  99.       return this;
  100.     }
  101.    

  102.     public Builder couldNotStore(String message) {
  103.       errorMessages.put(ErrorType.COULD_NOT_STORE, message);
  104.       return this;
  105.     }

  106.     public Builder couldNotRead(String message) {
  107.       errorMessages.put(ErrorType.COULD_NOT_READ, message);
  108.       return this;
  109.     }
  110.    
  111.     public Builder dependencyShaMismatch(String message) {
  112.       errorMessages.put(ErrorType.DEPENDENCY_SHA_MISMATCH, message);
  113.       return this;
  114.     }

  115.     public Builder rootNodeImported(String message) {
  116.       errorMessages.put(ErrorType.ROOT_NODE_IMPORTED, message);
  117.       return this;
  118.     }
  119.    
  120.     public ErrorMessages build() {
  121.       verifyAllSet();
  122.       ErrorMessages output = new ErrorMessages();
  123.       output.errorMessages.putAll(errorMessages);
  124.       return output;
  125.     }
  126.    
  127.     private void verifyAllSet() {
  128.       List<ErrorType> missing =  new ArrayList<>(Arrays.asList(ErrorType.values()));
  129.       missing.removeAll(errorMessages.keySet());
  130.       if (missing.size() != 0) {
  131.         throw new IllegalStateException("Missing error messages for: " + missing);
  132.       }
  133.     }
  134.   }
  135.  
  136. }