View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.benerator;
28  
29  import com.rapiddweller.common.ConfigurationError;
30  
31  import java.util.Arrays;
32  import java.util.List;
33  
34  /**
35   * Indicates invalid setup of a Generator.<br/>
36   * <br/>
37   * Created: 21.12.2006 08:04:49
38   */
39  public class InvalidGeneratorSetupException extends ConfigurationError {
40  
41    private static final long serialVersionUID = 7613352958748575041L;
42  
43    private final List<PropertyMessage> propertyMessages;
44  
45    // constructors ----------------------------------------------------------------------------------------------------
46  
47    /**
48     * Instantiates a new Invalid generator setup exception.
49     *
50     * @param propertyName    the property name
51     * @param propertyMessage the property message
52     */
53    public InvalidGeneratorSetupException(String propertyName, String propertyMessage) {
54      this(new PropertyMessage(propertyName, propertyMessage));
55    }
56  
57    /**
58     * Instantiates a new Invalid generator setup exception.
59     *
60     * @param propertyMessages the property messages
61     */
62    public InvalidGeneratorSetupException(PropertyMessage... propertyMessages) {
63      this(null, null, propertyMessages);
64    }
65  
66    /**
67     * Instantiates a new Invalid generator setup exception.
68     *
69     * @param textMessage the text message
70     */
71    public InvalidGeneratorSetupException(String textMessage) {
72      this(textMessage, (Throwable) null);
73    }
74  
75    /**
76     * Instantiates a new Invalid generator setup exception.
77     *
78     * @param cause the cause
79     */
80    public InvalidGeneratorSetupException(Throwable cause) {
81      this(null, cause);
82    }
83  
84    /**
85     * Instantiates a new Invalid generator setup exception.
86     *
87     * @param textMessage the text message
88     * @param cause       the cause
89     */
90    public InvalidGeneratorSetupException(String textMessage, Throwable cause) {
91      this(textMessage, cause, new PropertyMessage[0]);
92    }
93  
94    /**
95     * Instantiates a new Invalid generator setup exception.
96     *
97     * @param textMessage      the text message
98     * @param cause            the cause
99     * @param propertyMessages the property messages
100    */
101   public InvalidGeneratorSetupException(String textMessage, Throwable cause, PropertyMessage... propertyMessages) {
102     super(formatMessage(textMessage, propertyMessages), cause);
103     this.propertyMessages = Arrays.asList(propertyMessages);
104   }
105 
106   // interface -------------------------------------------------------------------------------------------------------
107 
108   /**
109    * Get property messages property message [ ].
110    *
111    * @return the property message [ ]
112    */
113   public PropertyMessage[] getPropertyMessages() {
114     PropertyMessagee.html#PropertyMessage">PropertyMessage[] array = new PropertyMessage[propertyMessages.size()];
115     return propertyMessages.toArray(array);
116   }
117 
118   private static String formatMessage(String textMessage, PropertyMessage... propertyMessages) {
119     StringBuilder buffer = new StringBuilder();
120     if (textMessage != null) {
121       buffer.append(textMessage).append(": ");
122     }
123     for (int i = 0; i < propertyMessages.length; i++) {
124       PropertyMessage propertyMessage = propertyMessages[i];
125       buffer.append(propertyMessage);
126       if (i < propertyMessages.length - 1) {
127         buffer.append(", ");
128       }
129     }
130     return buffer.toString();
131   }
132 }