View Javadoc

1   package uba.db.sql.language;
2   
3   import org.apache.commons.lang.builder.EqualsBuilder;
4   import org.apache.commons.lang.builder.HashCodeBuilder;
5   
6   /***
7    * Contiene los constraints que puede poseer una columna de forma individual.
8    * (por ejemplo si puede o no ser null, si es un primary key, etc).
9    * 
10   * @version $Revision: 1.4 $
11   */
12  public class ColumnConstraintDeclaration {
13      /***
14       * Instancia que representa el constraint NOT NULL
15       */
16      public static final ColumnConstraintDeclaration NOT_NULL = new ColumnConstraintDeclaration(
17              true, false, false);
18  
19      /***
20       * Instancia que representa el constraint por DEFAUL (es decir no hay constraints por
21       * nulos y de primary key).
22       */
23      public static final ColumnConstraintDeclaration DEFAULT = new ColumnConstraintDeclaration(
24              false, false, false);;
25  
26      /***
27       * Instancia que representa el constraint PRIMARY KEY
28       */
29      public static final ColumnConstraintDeclaration PRIMARY_KEY = new ColumnConstraintDeclaration(
30              true, true, true);
31  
32      private boolean notNull;
33      private boolean unique;
34      private boolean primaryKey;
35  
36      public ColumnConstraintDeclaration(boolean notNull, boolean unique, boolean primaryKey) {
37          this.notNull = notNull;
38          this.unique = unique;
39          this.primaryKey = primaryKey;
40          if (primaryKey) {
41              this.notNull = true;
42              this.unique = true;
43          }
44      }
45  
46      /***
47       * @see java.lang.Object#equals(java.lang.Object)
48       */
49      public boolean equals(Object obj) {
50          return EqualsBuilder.reflectionEquals(this, obj);
51      }
52  
53      /***
54       * @see java.lang.Object#hashCode()
55       */
56      public int hashCode() {
57          return HashCodeBuilder.reflectionHashCode(this);
58      }
59  
60      /***
61       * @see java.lang.Object#toString()
62       */
63      public String toString() {
64          StringBuffer buff = new StringBuffer();
65          if (!primaryKey) {
66              if (unique) {
67                  buff.append(" UNIQUE");
68              }
69              if (notNull) {
70                  buff.append(" NOT NULL");
71              }
72          } else {
73              buff.append(" PRIMARY KEY");
74          }
75          return buff.toString();
76      }
77  
78      /***
79       * Retorna <i>true</i> si la columna no puede contener valores nulos.
80       */
81      public boolean notNull() {
82          return notNull;
83      }
84  }