View Javadoc

1   /*
2    * Copyright (c) 2001-2004,
3    * RedVerst Group, ISP RAS http://www.ispras.ru
4    * All rights reserved.
5    *
6    * Redistribution and use in source and binary forms, with or without
7    * modification, are permitted provided that the following conditions are met:
8    *
9    * 1. Redistributions of source code must retain the above copyright notice, this
10   *    list of conditions and the following disclaimer.
11   *
12   * 2. Redistributions in binary form must reproduce the above copyright notice,
13   *    this list of conditions and the following disclaimer in the documentation
14   *    and/or other materials provided with the distribution.
15   *
16   * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17   *    may not be used to endorse or promote products derived from this software
18   *    without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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/*String*/ getAttributeNames()
150     {
151         Set/*String*/ s = new HashSet/*String*/();
152         s.addAll( super.getAttributeNames() );
153         s.addAll( Accessor.getBeanPropertyNames( this ) );
154         return s;
155     }
156 }