View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
10   */
11  package org.csveed.bean;
12  
13  import java.beans.PropertyDescriptor;
14  import java.lang.reflect.Field;
15  
16  import org.csveed.bean.conversion.Converter;
17  
18  /**
19   * The Class BeanProperty.
20   */
21  public class BeanProperty {
22  
23      /** The property descriptor. */
24      private PropertyDescriptor propertyDescriptor;
25  
26      /** The field. */
27      private Field field;
28  
29      /** The converter. */
30      private Converter converter;
31  
32      /** The column name. */
33      private String columnName;
34  
35      /** The column index. */
36      private int columnIndex = -1;
37  
38      /** The required. */
39      private boolean required;
40  
41      /** The dynamic column property. */
42      private boolean dynamicColumnProperty;
43  
44      /**
45       * Gets the number class.
46       *
47       * @return the number class
48       */
49      @SuppressWarnings("unchecked")
50      public Class<? extends Number> getNumberClass() {
51          if (Number.class.isAssignableFrom(propertyDescriptor.getPropertyType())) {
52              return (Class<? extends Number>) propertyDescriptor.getPropertyType();
53          }
54          return null;
55      }
56  
57      /**
58       * Gets the property descriptor.
59       *
60       * @return the property descriptor
61       */
62      public PropertyDescriptor getPropertyDescriptor() {
63          return propertyDescriptor;
64      }
65  
66      /**
67       * Sets the property descriptor.
68       *
69       * @param propertyDescriptor
70       *            the new property descriptor
71       */
72      public void setPropertyDescriptor(PropertyDescriptor propertyDescriptor) {
73          this.propertyDescriptor = propertyDescriptor;
74      }
75  
76      /**
77       * Gets the converter.
78       *
79       * @return the converter
80       */
81      public Converter getConverter() {
82          return converter;
83      }
84  
85      /**
86       * Sets the converter.
87       *
88       * @param converter
89       *            the new converter
90       */
91      public void setConverter(Converter converter) {
92          this.converter = converter;
93      }
94  
95      /**
96       * Gets the column name.
97       *
98       * @return the column name
99       */
100     public String getColumnName() {
101         return columnName;
102     }
103 
104     /**
105      * Sets the column name.
106      *
107      * @param columnName
108      *            the new column name
109      */
110     public void setColumnName(String columnName) {
111         this.columnName = columnName;
112     }
113 
114     /**
115      * Checks if is required.
116      *
117      * @return true, if is required
118      */
119     public boolean isRequired() {
120         return required;
121     }
122 
123     /**
124      * Sets the required.
125      *
126      * @param required
127      *            the new required
128      */
129     public void setRequired(boolean required) {
130         this.required = required;
131     }
132 
133     /**
134      * Gets the column index.
135      *
136      * @return the column index
137      */
138     public int getColumnIndex() {
139         return columnIndex;
140     }
141 
142     /**
143      * Sets the column index.
144      *
145      * @param columnIndex
146      *            the new column index
147      */
148     public void setColumnIndex(int columnIndex) {
149         this.columnIndex = columnIndex;
150     }
151 
152     /**
153      * Gets the field.
154      *
155      * @return the field
156      */
157     public Field getField() {
158         return field;
159     }
160 
161     /**
162      * Sets the field.
163      *
164      * @param field
165      *            the new field
166      */
167     public void setField(Field field) {
168         this.field = field;
169     }
170 
171     /**
172      * Gets the property name.
173      *
174      * @return the property name
175      */
176     public String getPropertyName() {
177         return propertyDescriptor.getName();
178     }
179 
180     /**
181      * Sets the dynamic column property.
182      *
183      * @param dynamicColumnProperty
184      *            the new dynamic column property
185      */
186     public void setDynamicColumnProperty(boolean dynamicColumnProperty) {
187         this.dynamicColumnProperty = dynamicColumnProperty;
188     }
189 
190     /**
191      * Checks if is dynamic column property.
192      *
193      * @return true, if is dynamic column property
194      */
195     public boolean isDynamicColumnProperty() {
196         return this.dynamicColumnProperty;
197     }
198 
199     @Override
200     public int hashCode() {
201         return getPropertyName().hashCode();
202     }
203 
204     @Override
205     public boolean equals(Object obj) {
206         if (!(obj instanceof BeanProperty)) {
207             return false;
208         }
209         BeanProperty other = (BeanProperty) obj;
210         return getPropertyName().equals(other.getPropertyName());
211     }
212 
213     /**
214      * Gets the property type.
215      *
216      * @return the property type
217      */
218     public String getPropertyType() {
219         return getConverter() != null ? getConverter().infoOnType()
220                 : getPropertyDescriptor().getPropertyType().getName();
221     }
222 
223 }