KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jga > fn > property > ConstructUnary


1 // ============================================================================
2
// $Id: ConstructUnary.java,v 1.16 2006/05/08 02:23:26 davidahall Exp $
3
// Copyright (c) 2003-2005 David A. Hall
4
// ============================================================================
5
// The contents of this file are subject to the Common Development and
6
// Distribution License (CDDL), Version 1.0 (the License); you may not use this
7
// file except in compliance with the License. You should have received a copy
8
// of the the License along with this file: if not, a copy of the License is
9
// available from Sun Microsystems, Inc.
10
//
11
// http://www.sun.com/cddl/cddl.html
12
//
13
// From time to time, the license steward (initially Sun Microsystems, Inc.) may
14
// publish revised and/or new versions of the License. You may not use,
15
// distribute, or otherwise make this file available under subsequent versions
16
// of the License.
17
//
18
// Alternatively, the contents of this file may be used under the terms of the
19
// GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which
20
// case the provisions of the LGPL are applicable instead of those above. If you
21
// wish to allow use of your version of this file only under the terms of the
22
// LGPL, and not to allow others to use your version of this file under the
23
// terms of the CDDL, indicate your decision by deleting the provisions above
24
// and replace them with the notice and other provisions required by the LGPL.
25
// If you do not delete the provisions above, a recipient may use your version
26
// of this file under the terms of either the CDDL or the LGPL.
27
//
28
// This library is distributed in the hope that it will be useful,
29
// but WITHOUT ANY WARRANTY; without even the implied warranty of
30
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
31
// ============================================================================
32

33 package net.sf.jga.fn.property;
34
35 import java.lang.reflect.Constructor JavaDoc;
36 import java.lang.reflect.InvocationTargetException JavaDoc;
37 import java.text.MessageFormat JavaDoc;
38 import net.sf.jga.fn.EvaluationException;
39 import net.sf.jga.fn.UnaryFunctor;
40
41 /**
42  * Unary Functor that constructs an object of the given class via a
43  * one-argument constructor.
44  * <p>
45  * Copyright &copy; 2003-2005 David A. Hall
46  *
47  * @author <a HREF="mailto:davidahall@users.sourceforge.net">David A. Hall</a>
48  **/

49
50 public class ConstructUnary<T,R> extends UnaryFunctor<T,R> {
51     
52     static final long serialVersionUID = -6046849006655934333L;
53
54     // the type of the object built by the constructor
55
private Class JavaDoc<R> _objclass;
56
57     // the argument type for the constructor
58
private Class JavaDoc<T> _argclass;
59
60     // the constructor being invoked
61
private transient Constructor JavaDoc<R> _ctor;
62
63     /**
64      * Builds a functor that will build an object of class
65      * <code>ctorclass</code>, given an argument of class <code>argclass</code>.
66      * The classes passed to the constructor must be assignable to the generic
67      * arguments (when generics are in use):
68      * <code>
69      * ConstructUnary<String,Integer> ctor =
70      * new ConstructUnary<String,Integer>(String.class,Integer.class);
71      * </code>
72      * @throws IllegalArgumentException if either argument is omitted, or if the
73      * constructor cannot be found.
74      */

75     
76     public ConstructUnary(Class JavaDoc<T> argclass, Class JavaDoc<R> ctorclass) {
77         if (argclass == null) {
78             String JavaDoc msg = "Argument Class must be specified";
79             throw new IllegalArgumentException JavaDoc(msg);
80         }
81
82         if (ctorclass == null) {
83             String JavaDoc msg = "Class to be constructed must be specified";
84             throw new IllegalArgumentException JavaDoc(msg);
85         }
86         
87         try {
88             _argclass = argclass;
89             _objclass = ctorclass;
90             _ctor = getConstructor();
91         }
92         catch (NoSuchMethodException JavaDoc x) {
93             String JavaDoc msg = "No constructor found for class {0} that takes an argument of class {1}";
94             Object JavaDoc args[] = new Object JavaDoc[] { ctorclass, argclass };
95             
96             IllegalArgumentException JavaDoc x1 =
97                 new IllegalArgumentException JavaDoc(MessageFormat.format(msg, args));
98             x1.initCause(x);
99             throw x1;
100         }
101     }
102
103     
104     /**
105      * Lazily loads the constructor
106      */

107     public synchronized Constructor JavaDoc<R> getConstructor() throws NoSuchMethodException JavaDoc {
108         if (_ctor == null)
109             _ctor = _objclass.getConstructor(new Class JavaDoc[]{_argclass});
110
111         return _ctor;
112     }
113
114     
115     // UnaryFunctor interface
116

117     /**
118      * Builds an object via a one-arg constructor, passing the given value.
119      * <p>
120      * @return the object built by the constructor
121      */

122     public R fn(T arg) {
123         
124         try {
125             R val = (R) getConstructor().newInstance(new Object JavaDoc[]{arg});
126             return val;
127         }
128         catch (NoSuchMethodException JavaDoc x) {
129             String JavaDoc msg = "No constructor for class {0} that takes an argument of type {1}";
130             Object JavaDoc[] args = new Object JavaDoc[]{_objclass.getName(), _argclass.getName()};
131             throw new EvaluationException(MessageFormat.format(msg,args), x);
132         }
133         catch (InstantiationException JavaDoc x) {
134             String JavaDoc msg = "class {0} is abstract: cannot be constructed";
135             Object JavaDoc[] args = new Object JavaDoc[]{_objclass.getName()};
136             throw new EvaluationException(MessageFormat.format(msg,args), x);
137         }
138         catch (IllegalArgumentException JavaDoc x) {
139             String JavaDoc msg = "class {0} ctor({1}) cannot be called with {2}";
140             Object JavaDoc[] args = new Object JavaDoc[]{_objclass.getName(), _argclass.getName(), arg};
141             throw new EvaluationException(MessageFormat.format(msg,args), x);
142         }
143         catch (IllegalAccessException JavaDoc x) {
144             String JavaDoc msg = "class {0} ctor({1}) is not accessible";
145             Object JavaDoc[] args = new Object JavaDoc[]{_objclass.getName(), _argclass.getName()};
146             throw new EvaluationException(MessageFormat.format(msg,args), x);
147         }
148         catch (InvocationTargetException JavaDoc x) {
149             String JavaDoc msg = "class {0} ctor({1}) failed: "+x.getMessage();
150             Object JavaDoc[] args = new Object JavaDoc[]{_objclass.getName(), _argclass.getName()};
151             throw new EvaluationException(MessageFormat.format(msg,args), x);
152         }
153     }
154
155     
156     /**
157      * Calls the Visitor's <code>visit(ConstructUnary)</code> method, if it
158      * implements the nested Visitor interface.
159      */

160     public void accept(net.sf.jga.fn.Visitor v) {
161         if (v instanceof ConstructUnary.Visitor)
162             ((ConstructUnary.Visitor)v).visit(this);
163         else
164             v.visit(this);
165     }
166
167     // Object overrides
168

169     public String JavaDoc toString() {
170         return "new "+_objclass.getName()+"(["+_argclass.getName()+"])";
171     }
172     
173     // Acyclic Visitor
174

175     /**
176      * Interface for classes that may interpret a <b>ConstructUnary</b>
177      * predicate.
178      */

179     public interface Visitor extends net.sf.jga.fn.Visitor {
180         public void visit(ConstructUnary host);
181     }
182 }
183
Popular Tags