View Javadoc
1   /*
2    * Copyright © 2017, Saleforce.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  
29  import java.text.MessageFormat;
30  import java.util.Collections;
31  import java.util.List;
32  import java.util.function.Function;
33  import java.util.stream.Collectors;
34  
35  import com.salesforce.apt.graph.model.AbstractModel;
36  
37  public class ErrorModel {
38  
39    private final ErrorType message;
40    private final List<AbstractModel> causes;
41    private final List<AbstractModel> involved;
42    
43    public ErrorType getMessage() {
44      return message;
45    }
46  
47    public List<AbstractModel> getCauses() {
48      return causes;
49    }
50    
51    public List<AbstractModel> getInvolved() {
52      return involved;
53    }
54    
55    public boolean isCyclic() {
56      return message.isCycle();
57    }
58    
59    public String toString() {
60      return message.name() + " on " + involved.toString() + " caused by " + causes;
61    }
62    
63    /**
64     * A non cyclic error
65     * @param message {@link ErrorType} of this represents
66     * @param causes models at fault.
67     * @param involved models the error should be displayed on.
68     */
69    public ErrorModel(ErrorType message, List<? extends AbstractModel> causes, List<? extends AbstractModel> involved) {
70      super();
71      this.message = message;
72      if (message.isCycle() && !causes.equals(involved)) {
73        throw new IllegalArgumentException("Malformed ErrorModel");
74      }
75      this.causes = Collections.unmodifiableList(causes);
76      this.involved = Collections.unmodifiableList(involved);   
77    }
78    
79    /**
80     * Returns a contextualized error message for the error, from the model's perspective.
81     * 
82     * @param on an AbstractModel that is "involved" in this error
83     * @param converter presents an error message for this error model to be displayed on the AbstractModel, on,
84     *     that contributed to the error.
85     * @return an error message from the context of the failed model member.
86     */
87    public String getMessageOn(AbstractModel on, Function<ErrorType, String> converter) {
88      if (!isCyclic()) {
89        if (getCauses().size() == 1) {
90          return MessageFormat.format(converter.apply(getMessage()), getCauses().get(0));
91        } else {
92          return MessageFormat.format(converter.apply(getMessage()), 
93              getCauses().stream().filter(m -> !m.equals(on)).map(m -> m.toString()).collect(Collectors.joining(", "))); 
94        }
95      }
96      StringBuilder builder = new StringBuilder();
97      int index = getInvolved().indexOf(on);
98      if (index != -1) {
99        for (int i = index; i < getInvolved().size(); i++) {
100         builder.append(getInvolved().get(i).toString());
101         if (i + 1 < getInvolved().size() || index != 0) {
102           builder.append(" -> ");
103         }
104       }
105       for (int i = 0; i < index; i++) {
106         builder.append(getInvolved().get(i).toString());
107         if (i + 1 < index) {
108           builder.append(" -> ");
109         }
110       }
111     }
112     return MessageFormat.format(converter.apply(getMessage()), builder.toString());
113   }
114 
115 }