View Javadoc

1   /*
2    Copyright (C) 2008 Grid Systems, S.A.
3   
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8   
9    This library is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12   Lesser General Public License for more details.
13  
14   You should have received a copy of the GNU Lesser General Public
15   License along with this library; if not, write to the Free Software
16   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
17  */
18  package com.gridsystems.nextgrid.api.pom.ref;
19  
20  /**
21   * Reference to an arbitrary object. For being used only in specialised cases.
22   *
23   * @param <E> Class type
24   * @author Rodrigo Ruiz
25   */
26  public class ObjectReference<E> extends ReferenceSupport<E> {
27  
28    /**
29     * Serial Version UID.
30     */
31    private static final long serialVersionUID = -3017895741189696100L;
32  
33    /**
34     * Value type.
35     */
36    private final Class<E> type;
37  
38    /**
39     * The wrapped value.
40     */
41    private E value;
42  
43    /**
44     * Creates a new instance.
45     *
46     * @param type  Value type
47     */
48    public ObjectReference(Class<E> type) {
49      this.type = type;
50    }
51  
52    /**
53     * Creates a new instance.
54     *
55     * @param type  Value type
56     * @param value Value
57     */
58    public ObjectReference(Class<E> type, E value) {
59      this.type = type;
60      setValue(value);
61    }
62  
63    /**
64     * {@inheritDoc}
65     */
66    public boolean canCastFrom(Class<?> type) {
67      return type != null && this.type.isAssignableFrom(type);
68    }
69  
70    /**
71     * {@inheritDoc}
72     */
73    public boolean canCastTo(Class<?> type) {
74      return type != null && type.isAssignableFrom(this.type);
75    }
76  
77    /**
78     * {@inheritDoc}
79     */
80    public Object castTo(Class<?> type) {
81      return castTo(type, value);
82    }
83  
84    /**
85     * {@inheritDoc}
86     */
87    public E getValue() {
88      return this.value;
89    }
90  
91    /**
92     * {@inheritDoc}
93     */
94    public Class<E> getValueType() {
95      return this.type;
96    }
97  
98    /**
99     * {@inheritDoc}
100    */
101   @SuppressWarnings("unchecked")
102   public void setValue(Object value) {
103     this.value = (E)castTo(getValueType(), value);
104     setAvailable(value != null);
105   }
106 
107   /**
108    * Performs a cast of the given value to the specified type.
109    *
110    * @param type  The target type
111    * @param value The source value
112    * @return The target value
113    */
114   private Object castTo(Class<?> type, Object value) {
115     if (type.isAssignableFrom(String.class)) {
116       return (value == null) ? null : value.toString();
117     } else if (type.isAssignableFrom(this.type)) {
118       return value;
119     } else {
120       throw new ClassCastException("Cannot cast to " + type);
121     }
122   }
123 }