View Javadoc

1   /*
2    * Copyright 2008 - 2008 JEuclid, http://jeuclid.sf.net
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  
17  /* $Id: AbstractSimpleTypeWrapper.java,v 03dc0884e86f 2008/06/21 10:53:35 maxberger $ */
18  
19  package net.sourceforge.jeuclid.context.typewrapper;
20  
21  /**
22   * Basic (and simple) implementation of TypeWrapper. Maintains an instance of
23   * type being wrapped as well as provides reasonable default implementations
24   * for all the operations.
25   * 
26   * @version $Revision: 03dc0884e86f $
27   */
28  public abstract class AbstractSimpleTypeWrapper implements TypeWrapper {
29      /**
30       * 
31       */
32      private static final long serialVersionUID = 1L;
33  
34      /** the class instance being wrapped */
35      private final Class<?> valueType;
36  
37      /**
38       * @param valType
39       *            a Class object
40       */
41      protected AbstractSimpleTypeWrapper(final Class<?> valType) {
42          this.valueType = valType;
43      }
44  
45      /** {@inheritDoc} */
46      public Class<?> getValueType() {
47          return this.valueType;
48      }
49  
50      /** {@inheritDoc} */
51      public boolean valid(final Object o) {
52          return this.valueType.isInstance(o);
53      }
54  
55      /** {@inheritDoc} */
56      public Object fromString(final String value) {
57          if (value == null) {
58              return null;
59          }
60          throw new IllegalArgumentException(TypeWrapper.FAILED_TO_CONVERT
61                  + value + TypeWrapper.TO + this.valueType);
62      }
63  
64      /** {@inheritDoc} */
65      public String toString(final Object value) {
66          if (value == null) {
67              return null;
68          } else {
69              return value.toString();
70          }
71      }
72  }