View Javadoc

1   /*
2    * Copyright 2011-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.util.adapt;
17  
18  /**
19   * Signals that an implementation is not adaptable to the desired type.
20   */
21  public final class AdaptableException extends RuntimeException
22  {
23    // ********************************* Fields *********************************
24  
25    // --- constants ------------------------------------------------------------
26  
27    /**
28     * The class version identifier.
29     * <p>
30     * The value of this constant is {@value}.
31     */
32    private static final long serialVersionUID = 1L;
33  
34    // --- members --------------------------------------------------------------
35  
36    /**
37     * The type of the instance that does not provide the requested adapter. May
38     * be <code>null</code>.
39     *
40     * @serial
41     */
42    private final Class<?> instanceType;
43  
44    /**
45     * The type of the requested adapter the instance cannot provided. May be
46     * <code>null</code>.
47     *
48     * @serial
49     */
50    private final Class<?> adapterType;
51  
52    // ****************************** Initializer *******************************
53  
54    // ****************************** Constructors ******************************
55  
56    /**
57     * Convenience constructor without a root cause.
58     *
59     * @param instanceType the type of the instance that does not provide the
60     *          requested adapter.
61     * @param adapterType the type of the requested adapter the instance cannot
62     *          provided.
63     */
64    public AdaptableException(final Class<?> instanceType,
65        final Class<?> adapterType)
66    {
67      this(null, instanceType, adapterType);
68    }
69  
70    /**
71     * Default constructor.
72     *
73     * @param cause the root cause to this exception.
74     * @param instanceType the type of the instance that does not provide the
75     *          requested adapter.
76     * @param adapterType the type of the requested adapter the instance cannot
77     *          provided.
78     */
79    public AdaptableException(final Throwable cause, final Class<?> instanceType,
80        final Class<?> adapterType)
81    {
82      super(createMessage(instanceType, adapterType), cause);
83  
84      this.instanceType = instanceType;
85      this.adapterType = adapterType;
86    }
87  
88    // ****************************** Inner Classes *****************************
89  
90    // ********************************* Methods ********************************
91  
92    // --- init -----------------------------------------------------------------
93  
94    private static String createMessage(final Class<?> instanceType,
95        final Class<?> adapterType)
96    {
97      final StringBuilder buffer = new StringBuilder(128);
98      if (instanceType != null)
99      {
100       buffer.append(instanceType.getName());
101     }
102     else
103     {
104       buffer.append("Instance of unknown type");
105     }
106 
107     buffer.append(" cannot be assigned to ");
108 
109     if (adapterType != null)
110     {
111       buffer.append(adapterType.getName());
112     }
113     else
114     {
115       buffer.append("unknown type");
116     }
117     buffer.append('.');
118 
119     return buffer.toString();
120   }
121 
122   // --- get&set --------------------------------------------------------------
123 
124   /**
125    * Returns the type of the instance that does not provide the requested
126    * adapter.
127    *
128    * @return the type of the instance that does not provide the requested
129    *         adapter. May be <code>null</code>.
130    */
131   public Class<?> getInstanceType()
132   {
133     return instanceType;
134   }
135 
136   /**
137    * Returns the type of the requested adapter the instance cannot provided.
138    *
139    * @return the type of the requested adapter the instance cannot provided. May
140    *         be <code>null</code>.
141    */
142   public Class<?> getAdapterType()
143   {
144     return adapterType;
145   }
146 
147   // --- business -------------------------------------------------------------
148 
149   // --- object basics --------------------------------------------------------
150 
151 }