01: /*=============================================================================
02: * Copyright Texas Instruments 2002. All Rights Reserved.
03: *
04: * This program is free software; you can redistribute it and/or modify
05: * it under the terms of the GNU General Public License as published by
06: * the Free Software Foundation; either version 2 of the License, or
07: * (at your option) any later version.
08: *
09: * This program 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
12: * GNU General Public License for more details.
13: *
14: * You should have received a copy of the GNU General Public License
15: * along with this program; if not, write to the Free Software
16: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17: */
18:
19: package ti.chimera.registry;
20:
21: /**
22: * A {@link NodeContract} which enforces that a value is an <code>instanceof</code>
23: * a specified class.
24: *
25: * @author ;Rob Clark;a0873619;San Diego;;
26: * @version 0.1
27: */
28: public class TypeNodeContract implements NodeContract {
29: private Class type;
30:
31: /**
32: * Class Constructor.
33: *
34: * @param type the type
35: */
36: public TypeNodeContract(Class type) {
37: if (type == null)
38: throw new ti.exceptions.ProgrammingErrorException(
39: "type should not be null!");
40:
41: this .type = type;
42: }
43:
44: /**
45: * Determine if the specified <code>value</code> meets this contract.
46: *
47: * @param value the value to check
48: * @return <code>true</code> if meets contract
49: */
50: public boolean accepts(Object value) {
51: if (value == null)
52: return true; // null can be assign to any type
53: return type.isAssignableFrom(value.getClass());
54: }
55:
56: /**
57: * Get the type.
58: */
59: public Class getType() {
60: return type;
61: }
62:
63: /**
64: * The contract implementation should overload <code>toString</code> so
65: * the contract can be displayed to the user in a sane format, for use
66: * in error messages, etc.
67: */
68: public String toString() {
69: return "(value instanceof " + type.getName() + ")";
70: }
71: }
72:
73: /*
74: * Local Variables:
75: * tab-width: 2
76: * indent-tabs-mode: nil
77: * mode: java
78: * c-indentation-style: java
79: * c-basic-offset: 2
80: * eval: (c-set-offset 'substatement-open '0)
81: * eval: (c-set-offset 'case-label '+)
82: * eval: (c-set-offset 'inclass '+)
83: * eval: (c-set-offset 'inline-open '0)
84: * End:
85: */
|