1 package uba.db.column;
2
3 import java.io.DataInput;
4 import java.io.DataOutput;
5
6 import org.apache.commons.lang.builder.EqualsBuilder;
7 import org.apache.commons.lang.builder.HashCodeBuilder;
8
9 import uba.db.column.io.CharColumnReader;
10 import uba.db.column.io.CharColumnWriter;
11 import uba.db.column.io.ColumnReader;
12 import uba.db.column.io.ColumnWriter;
13
14 /***
15 * Representa una columna del tipo CHAR.
16 *
17 * @version $Revision: 1.3 $
18 */
19 public class CharColumnSpecification extends ColumnSpecificationBehavior {
20 private static final long serialVersionUID = 3544956541212832312L;
21
22 private int maxChars;
23
24 /***
25 * Longitud maxima que por default tiene la columna.
26 */
27 public static final int DEFAULT_MAX_LENGTH = 1;
28
29 /***
30 * String utilizado para mostrar el tipo de datos que representa esta
31 * especificación de columna.
32 *
33 * @see ColumnSpecification#dataTypeDisplayString()
34 */
35 public static final String DATATYPE_DISPLAY_STRING = "CHAR";
36
37 /***
38 * Crea una instancia que representa una columna del tipo CHAR.<br>
39 * Utilizando la longitud por default que es 1 caracter.
40 *
41 * @param name
42 * nombre de la columna.
43 * @param constraint
44 * restricciones para esta columna (por ejemplo si puede o no ser
45 * null, etc).
46 */
47 public CharColumnSpecification(String name, ColumnConstraint constraint) {
48 super(DATATYPE_DISPLAY_STRING, name, constraint);
49 this.maxChars = DEFAULT_MAX_LENGTH;
50 }
51
52 /***
53 * Crea una instancia que representa una columna del tipo CHAR.<br>
54 * Utilizando la longitud por default que es 1 caracter. Y con los
55 * constraints por default.
56 *
57 * @see ColumnConstraint#DEFAULT
58 *
59 * @param name
60 * nombre de la columna.
61 */
62 public CharColumnSpecification(String name) {
63 this(name, DEFAULT_MAX_LENGTH);
64 }
65
66 /***
67 * Crea una instancia que representa una columna del tipo CHAR.
68 *
69 * @param name
70 * nombre de la columna.
71 * @param maxLength
72 * cantidad máxima de caracteres.
73 * @param constraint
74 * restricciones.
75 */
76 public CharColumnSpecification(String name, int maxLength, ColumnConstraint constraint) {
77 super(DATATYPE_DISPLAY_STRING, name, constraint);
78 this.maxChars = maxLength;
79 }
80
81 /***
82 * Crea una instancia que representa una columna del tipo CHAR.<br>
83 * Con los constraints por default.
84 *
85 * @see ColumnConstraint#DEFAULT
86 *
87 * @param name
88 * nombre de la columna.
89 * @param maxLength
90 * cantidad máxima de caracteres.
91 */
92 public CharColumnSpecification(String name, int maxLength) {
93 this(name, maxLength, ColumnConstraint.DEFAULT);
94 }
95
96 /***
97 * @see java.lang.Object#equals(java.lang.Object)
98 */
99 public boolean equals(Object obj) {
100 return EqualsBuilder.reflectionEquals(this, obj);
101 }
102
103 /***
104 * @see java.lang.Object#hashCode()
105 */
106 public int hashCode() {
107 return HashCodeBuilder.reflectionHashCode(this);
108 }
109
110 /***
111 * Retorna la cantidad máxima de caracteres que puede tener esta columna.
112 */
113 public int maxChars() {
114 return maxChars;
115 }
116
117 /***
118 * @see uba.db.column.ColumnSpecification#readerFor(DataInput)
119 */
120 public ColumnReader readerFor(DataInput in) {
121 return new CharColumnReader(this, in);
122 }
123
124 /***
125 * @see uba.db.column.ColumnSpecification#writerFor(DataOutput)
126 */
127 public ColumnWriter writerFor(DataOutput out) {
128 return new CharColumnWriter(this, out);
129 }
130
131 /***
132 * @see java.lang.Object#toString()
133 */
134 public String toString() {
135 return name() + " " + dataTypeDisplayString() + "(" + maxChars() + ")";
136 }
137 }