Add some unit testing of the CfbSchema.
[cfb.git] / prod / net / jaekl / cfb / db / Column.java
1 package net.jaekl.cfb.db;
2
3 import net.jaekl.cfb.util.Util;
4
5 // Copyright (C) 2015 Christian Jaekl
6
7 public class Column {
8         public enum Type {
9                 CHAR, INTEGER, TIMESTAMPTZ, VARCHAR 
10         };
11         public enum Null {
12                 NOT_NULL, NULL
13         }
14         
15         String m_name;
16         Type m_type;
17         int m_width;
18         Null m_null;
19         
20         public Column(String name, Type type, int width, Null canBeNull) 
21         {
22                 m_name = name;
23                 m_type = type;
24                 m_width = width;
25                 m_null = canBeNull;
26         }
27         
28         public String getName() { return m_name; }
29         public Type getType() { return m_type; }
30         public int getWidth() { return m_width; }
31         public Null getNull() { return m_null; }
32         
33         // Create a column based on an array of Objects
34         // Input format:  { name, type, width, can_be_null } 
35         public static Column construct(Object[] spec) {
36                 assert(null != spec);
37                 assert(4 == spec.length);
38                 assert(spec[0] instanceof String);
39                 assert(spec[1] instanceof Type);
40                 assert(spec[2] instanceof Number);
41                 assert(spec[3] instanceof Null);
42                 
43                 String name = (String)(spec[0]);
44                 Type type = (Type)(spec[1]);
45                 Number width = (Number)(spec[2]);
46                 Null canBeNull = (Null)(spec[3]);
47                 
48                 return new Column(name, type, width.intValue(), canBeNull);
49         }
50         
51         @Override 
52         public boolean equals(Object obj) 
53         {
54                 if (null == obj) {
55                         return false;
56                 }
57                 if (! (obj instanceof Column)) {
58                         return false;
59                 }
60                 Column other = (Column)obj;
61
62                 if (! Util.objsAreEqual(this.getName(), other.getName())) {
63                         return false;
64                 }
65                 if (! Util.objsAreEqual(this.getType(), other.getType())) {
66                         return false;
67                 }
68                 if (this.getWidth() != other.getWidth()) {
69                         return false;
70                 }
71                 if (! Util.objsAreEqual(this.getNull(), other.getNull())) {
72                         return false;
73                 }
74                 
75                 return true;
76         }
77         
78         @Override
79         public int hashCode() {
80                 int code = Util.objHashCode(getName())
81                                  ^ Util.objHashCode(getType())
82                                  ^ getWidth()
83                                  ^ Util.objHashCode(getNull());
84                 return code;
85         }
86 }