View Javadoc

1   /*
2    * Copyright (C) 2004 TiongHiang Lee
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17   *
18   * Email: thlee@onemindsoft.org
19   */
20  
21  package org.onemind.commons.relabean;
22  
23  import java.io.IOException;
24  import java.io.Writer;
25  import java.util.Collection;
26  import java.util.HashMap;
27  import java.util.Iterator;
28  import java.util.LinkedHashMap;
29  /***
30   * Describes the properties of a bean class and the relations of the bean class to other classes
31   * 
32   * @author TiongHiang Lee (thlee@onemindsoft.org)
33   * @version $Id: RelationalBeanInfo.java,v 1.2 2004/08/26 16:28:58 thlee Exp $ $Name:  $
34   */
35  public class RelationalBeanInfo
36  {
37  
38      /*** the bean class * */
39      private Class _beanClass;
40  
41      /*** the properties * */
42      private HashMap _properties = new LinkedHashMap();
43  
44      /*** the relations * */
45      private HashMap _relations = new LinkedHashMap();
46  
47      /***
48       * Constructor {@inheritDoc}
49       */
50      public RelationalBeanInfo(Class beanClass)
51      {
52          _beanClass = beanClass;
53      }
54  
55      /***
56       * Add a new property
57       * 
58       * @param prop the property descriptor
59       */
60      public void addProperty(PropertyDescriptor prop)
61      {
62          _properties.put(prop.getName(), prop);
63      }
64  
65      /***
66       * Add a new relation
67       * 
68       * @param prop the property relation
69       */
70      public void addRelation(RelationDescriptor prop)
71      {
72          _relations.put(prop.getName(), prop);
73      }
74  
75      /***
76       * Returns the beanClass.
77       * 
78       * @return the bean class
79       */
80      public Class getBeanClass()
81      {
82          return _beanClass;
83      }
84  
85      /***
86       * Returns the properties.
87       * 
88       * @return the properties
89       */
90      public Collection getProperties()
91      {
92          return _properties.values();
93      }
94  
95      /***
96       * Get the PropertyDescriptor by the property name
97       * 
98       * @param name the name
99       * @return the descriptor, or null
100      */
101     public PropertyDescriptor getProperty(String name)
102     {
103         return (PropertyDescriptor) _properties.get(name);
104     }
105 
106     /***
107      * Get the relation descriptor by the relation name
108      * 
109      * @param name the name
110      * @return the descriptor, or null
111      */
112     public RelationDescriptor getRelation(String name)
113     {
114         return (RelationDescriptor) _relations.get(name);
115     }
116 
117     /***
118      * Returns the relations.
119      * 
120      * @return the relations
121      */
122     public Collection getRelations()
123     {
124         return _relations.values();
125     }
126 
127     /***
128      * {@inheritDoc}
129      */
130     public String toString()
131     {
132         return "RelationalBeanInfo[" + _beanClass + "]";
133     }
134 
135     /***
136      * Dump the property
137      * 
138      * @param prefix the prefix
139      * @param writer the writer
140      * @throws IOException if there's IO problem
141      */
142     public void dump(String prefix, Writer writer) throws IOException
143     {
144         writer.write(prefix + toString() + "\n");
145         Iterator it = getProperties().iterator();
146         while (it.hasNext())
147         {
148             PropertyDescriptor desc = (PropertyDescriptor) it.next();
149             desc.dump(prefix + "\t", writer);
150         }
151         it = getRelations().iterator();
152         while (it.hasNext())
153         {
154             RelationDescriptor desc = (RelationDescriptor) it.next();
155             desc.dump(prefix + "\t", writer);
156         }
157     }
158 }