1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package com.unitesk.atp.dynattrs;
33
34 import java.util.HashSet;
35 import java.util.Map;
36 import java.util.Set;
37
38 /***
39 * The implementation of {@link Attributed} interface that accesses attributes
40 * through <I>getter</I> and <I>setter</I> methods according to JavaBeans
41 * naming conventions and stores additional attributes in {@link Map}.
42 *
43 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
44 * @version $Id: BeanMapAttributed.java,v 1.1 2004/10/09 06:19:04 all-x Exp $
45 */
46 public class BeanMapAttributed extends MapAttributed
47 {
48 /***
49 * Class constructor with {@link java.util.HashMap} as the default map.
50 */
51 public BeanMapAttributed()
52 {
53 super();
54 }
55
56 /***
57 * Class constructor with the specified map.
58 *
59 * @param map The map to store attributes.
60 */
61 public BeanMapAttributed( Map map )
62 {
63 super( map );
64 }
65
66 /***
67 * {@inheritDoc}
68 */
69 public boolean hasAttribute( String name )
70 {
71 return Accessor.hasBeanProperty( this, name )
72 || super.hasAttribute( name );
73 }
74
75 /***
76 * {@inheritDoc}
77 */
78 public boolean isRemovable( String name )
79 {
80 if( Accessor.hasBeanProperty( this, name ) )
81 {
82 return false;
83 }
84 return super.isRemovable( name );
85 }
86
87 /***
88 * {@inheritDoc}
89 */
90 public boolean isWritable( String name )
91 {
92 if( Accessor.hasBeanProperty( this, name ) )
93 {
94 return Accessor.isBeanPropertyWritable( this, name );
95 }
96 return super.isWritable( name );
97 }
98
99
100
101 /***
102 * {@inheritDoc}
103 */
104 public void removeAttribute( String name )
105 {
106 if( !hasAttribute( name ) ) return;
107
108 if( !isRemovable( name ) )
109 {
110 throw new AttributeException( this, name );
111 }
112
113 super.removeAttribute( name );
114 }
115
116
117
118 /***
119 * {@inheritDoc}
120 */
121 public Object getAttribute( String name )
122 {
123 if( Accessor.hasBeanProperty( this, name ) )
124 {
125 return Accessor.getBeanProperty( this, name );
126 } else {
127 return super.getAttribute( name );
128 }
129 }
130
131 /***
132 * {@inheritDoc}
133 */
134 public void setAttribute( String name, Object value )
135 {
136 if( Accessor.hasBeanProperty( this, name ) )
137 {
138 Accessor.setBeanProperty( this, name, value );
139 } else {
140 super.setAttribute( name, value );
141 }
142 }
143
144
145
146 /***
147 * {@inheritDoc}
148 */
149 public Set
150 {
151 Set
152 s.addAll( super.getAttributeNames() );
153 s.addAll( Accessor.getBeanPropertyNames( this ) );
154 return s;
155 }
156 }