View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.properties.api.config.domain;
17  
18  import de.smartics.properties.api.config.domain.key.ConfigurationKey;
19  
20  /**
21   * Signals that a property has been encountered twice.
22   */
23  public class DuplicatePropertyException extends ConfigurationException
24  {
25    // ********************************* Fields *********************************
26  
27    // --- constants ------------------------------------------------------------
28  
29    /**
30     * The class version identifier.
31     * <p>
32     * The value of this constant is {@value}.
33     * </p>
34     */
35    private static final long serialVersionUID = 1L;
36  
37    // --- members --------------------------------------------------------------
38  
39    /**
40     * The currently active property.
41     *
42     * @serial
43     */
44    private final Property currentProperty;
45  
46    /**
47     * The new encountered property with the same key.
48     *
49     * @serial
50     */
51    private final Property newProperty;
52  
53    // ****************************** Initializer *******************************
54  
55    // ****************************** Constructors ******************************
56  
57    /**
58     * Default Constructor.
59     *
60     * @param key the key to the configuration that signaled problems.
61     * @param currentProperty the currently active property.
62     * @param newProperty the new encountered property with the same key.
63     */
64    public DuplicatePropertyException(final ConfigurationKey key,
65        final Property currentProperty, final Property newProperty)
66    {
67      super(createMessage(currentProperty, newProperty), null, key);
68  
69      this.currentProperty = currentProperty;
70      this.newProperty = newProperty;
71    }
72  
73    // ****************************** Inner Classes *****************************
74  
75    // ********************************* Methods ********************************
76  
77    // --- init -----------------------------------------------------------------
78  
79    private static String createMessage(final Property currentProperty,
80        final Property newProperty)
81    {
82      return String.format(
83          "Duplicate key '%s' with new value '%s' in\n  '%s'."
84              + "\nAlready found in\n  '%s'\nwith value '%s' (active).",
85          currentProperty.getName(), newProperty.getValue(),
86          newProperty.getSource(), currentProperty.getSource(),
87          currentProperty.getValue());
88    }
89  
90    // --- get&set --------------------------------------------------------------
91  
92    /**
93     * Returns the currently active property.
94     *
95     * @return the currently active property.
96     */
97    public final Property getCurrentProperty()
98    {
99      return currentProperty;
100   }
101 
102   /**
103    * Returns the new encountered property with the same key.
104    *
105    * @return the new encountered property with the same key.
106    */
107   public final Property getNewProperty()
108   {
109     return newProperty;
110   }
111 
112   // --- business -------------------------------------------------------------
113 
114   // --- object basics --------------------------------------------------------
115 
116 }