View Javadoc
1   package guru.mikelue.jdut.datagrain;
2   
3   import java.sql.DatabaseMetaData;
4   import java.sql.JDBCType;
5   import java.util.Optional;
6   import java.util.function.Consumer;
7   
8   import org.apache.commons.lang3.StringUtils;
9   import org.apache.commons.lang3.Validate;
10  import org.apache.commons.lang3.builder.EqualsBuilder;
11  import org.apache.commons.lang3.builder.HashCodeBuilder;
12  
13  /**
14   * The definition of column of a table.
15   */
16  public class SchemaColumn {
17      private String name;
18      private Optional<JDBCType> jdbcType = Optional.empty();
19  	private Optional<Boolean> nullable = Optional.empty();
20  	private Optional<String> defaultValue = Optional.empty();
21  	private Optional<Boolean> autoIncremental = Optional.empty();
22  
23      /**
24       * This object is used with {@link Consumer} by {@link SchemaColumn#build}.
25       */
26      public class Builder {
27  		private Builder() {}
28  
29          /**
30           * Sets the name of column.
31           *
32           * @param newName The name of table
33           *
34           * @return cascading self
35           */
36          public Builder name(String newName)
37          {
38  			newName = StringUtils.trimToNull(newName);
39  
40              name = newName;
41              return this;
42          }
43          /**
44           * Sets the sql type of column.
45           *
46           * @param newJdbcType The value(nullable) of sql type, see {@link java.sql.Types}.
47           *
48           * @return cascading self
49           *
50           * @see java.sql.Types
51           */
52          public Builder jdbcType(JDBCType newJdbcType)
53          {
54              jdbcType = Optional.ofNullable(newJdbcType);
55              return this;
56          }
57  
58  		/**
59  		 * Sets whether or not this column is nullable
60  		 *
61  		 * @param newNullable The value of nullable
62  		 *
63  		 * @return cascading self
64  		 */
65  		public Builder nullable(boolean newNullable)
66  		{
67  			nullable = Optional.of(newNullable);
68  			return this;
69  		}
70  
71  		/**
72  		 * Sets whether or not this columns is auto-incremental.
73  		 *
74  		 * @param newAutoIncremental The value of auto-incremental
75  		 *
76  		 * @return cascading self
77  		 */
78  		public Builder autoIncremental(Boolean newAutoIncremental)
79  		{
80  			autoIncremental = Optional.ofNullable(newAutoIncremental);
81  			return this;
82  		}
83  
84  		/**
85  		 * Sets the default value of column.
86  		 *
87  		 * @param newDefaultValue the flag of having default value
88  		 *
89  		 * @return cascading self
90  		 */
91  		public Builder defaultValue(String newDefaultValue)
92  		{
93  			defaultValue = Optional.ofNullable(newDefaultValue);
94  			return this;
95  		}
96  
97  		private void validate()
98  		{
99  			Validate.notNull(name, "Need viable name of column");
100 		}
101     }
102 
103     private SchemaColumn() {}
104 
105     /**
106      * Builds a new column.
107      *
108      * @param builderConsumer The editor for column
109      *
110      * @return The new definition of column
111      */
112     public static SchemaColumn build(Consumer<Builder> builderConsumer)
113     {
114         SchemaColumnumn.html#SchemaColumn">SchemaColumn newColumn = new SchemaColumn();
115 		Builder newBuilder = newColumn.new Builder();
116 
117         builderConsumer.accept(newBuilder);
118 		newBuilder.validate();
119 
120         return newColumn.clone();
121     }
122 
123     /**
124      * Gets name of column
125      *
126      * @return The name of column
127      */
128     public String getName()
129     {
130         return name;
131     }
132 
133     /**
134      * Gets the value(optional) of sql type.
135      *
136      * @return The value of sql type
137 	 *
138 	 * @see DatabaseMetaData#getColumns getColumns.getInt("DATA_TYPE")
139      */
140     public Optional<JDBCType> getJdbcType()
141     {
142         return jdbcType;
143     }
144 
145 	/**
146 	 * Whether or not this column is nullable.
147 	 *
148 	 * @return may not be initialized
149 	 *
150 	 * @see DatabaseMetaData#getColumns getColumns.getInt("NULLABLE")
151 	 */
152 	public Optional<Boolean> getNullable()
153 	{
154 		return nullable;
155 	}
156 
157 	/**
158 	 * Gets value of default data.
159 	 *
160 	 * @return The optional default value
161 	 *
162 	 * @see DatabaseMetaData#getColumns getColumns.getString("COLUMN_DEF")
163 	 */
164 	public Optional<String> getDefaultValue()
165 	{
166 		return defaultValue;
167 	}
168 
169 	/**
170 	 * Checks whether or not this column is auto-incremental.
171 	 *
172 	 * @return The flag may be unknown
173 	 */
174 	public Optional<Boolean> getAutoIncremental()
175 	{
176 		return autoIncremental;
177 	}
178 
179 	/**
180 	 * Whether or not this column has default value.
181 	 *
182 	 * @return may not be initialized
183 	 *
184 	 * @see DatabaseMetaData#getColumns getColumns.getString("COLUMN_DEF")
185 	 */
186 	public boolean getHasDefaultValue()
187 	{
188 		return defaultValue.isPresent();
189 	}
190 
191     @Override
192     protected SchemaColumn clone()
193     {
194         SchemaColumn.html#SchemaColumn">SchemaColumn clonedObject = new SchemaColumn();
195         clonedObject.name = this.name;
196         clonedObject.jdbcType = this.jdbcType;
197 		clonedObject.nullable = this.nullable;
198 		clonedObject.defaultValue = this.defaultValue;
199 		clonedObject.autoIncremental = this.autoIncremental;
200 
201         return clonedObject;
202     }
203 
204 	/**
205 	 * Hashed code by name of column.
206 	 */
207 	@Override
208 	public int hashCode()
209 	{
210 		return new HashCodeBuilder(341242957, 596024461)
211 			.append(name)
212 		.toHashCode();
213 	}
214 
215 	/**
216 	 * Only compares the name of column.
217 	 */
218 	@Override
219 	public boolean equals(Object obj)
220 	{
221 		if (obj == null) { return false; }
222 		if (obj == this) { return true; }
223 		if (obj.getClass() != getClass()) {
224 			return false;
225 		}
226 
227 		SchemaColumn rhs = (SchemaColumn)obj;
228 		return new EqualsBuilder()
229 			.append(this.name, rhs.name)
230 			.isEquals();
231 	}
232 }