1 24 25 package com.mckoi.database; 26 27 40 41 public final class Variable implements java.io.Serializable , Cloneable { 42 43 static final long serialVersionUID = -8772800465139383297L; 44 45 48 private static final TableName UNKNOWN_TABLE_NAME = 49 new TableName("##UNKNOWN_TABLE_NAME##"); 50 51 55 private TableName table_name; 56 57 60 private String column_name; 61 62 65 public Variable(TableName table_name, String column_name) { 66 if (table_name == null || column_name == null) { 67 throw new NullPointerException (); 68 } 69 this.table_name = table_name; 70 this.column_name = column_name; 71 } 72 73 public Variable(String column_name) { 74 this(UNKNOWN_TABLE_NAME, column_name); 75 } 76 77 public Variable(Variable v) { 78 this.table_name = v.table_name; 79 this.column_name = v.column_name; 80 } 81 82 85 public TableName getTableName() { 86 if (!(table_name.equals(UNKNOWN_TABLE_NAME))) { 87 return table_name; 88 } 89 return null; 90 } 91 92 95 public String getName() { 96 return column_name; 97 } 98 99 103 public static Variable resolve(String name) { 104 int div = name.lastIndexOf("."); 105 if (div != -1) { 106 String column_name = name.substring(div + 1); 108 TableName table_name = TableName.resolve(name.substring(0, div)); 110 return new Variable(table_name, column_name); 112 } 113 else { 114 return new Variable(name); 116 } 117 } 118 119 124 public static Variable resolve(TableName tname, String name) { 125 Variable v = resolve(name); 126 if (v.getTableName() == null) { 127 return new Variable(tname, v.getName()); 128 } 129 else if (v.getTableName().getSchema() == null) { 130 return new Variable( 131 new TableName(tname.getSchema(), v.getTableName().getName()), 132 v.getName()); 133 } 134 return v; 135 } 136 137 141 public Variable resolveTableName(TableName tablen) { 142 if (table_name.equals(UNKNOWN_TABLE_NAME)) { 143 return new Variable(tablen, getName()); 144 } 145 else { 146 return new Variable(table_name.resolveSchema(tablen.getSchema()), 147 getName()); 148 } 149 } 150 151 154 public Variable set(Variable from) { 155 this.table_name = from.table_name; 156 this.column_name = from.column_name; 157 return this; 158 } 159 160 164 public void setColumnName(String column_name) { 165 if (column_name == null) { 166 throw new NullPointerException (); 167 } 168 this.column_name = column_name; 169 } 170 171 174 public void setTableName(TableName tname) { 175 if (table_name == null) { 176 throw new NullPointerException (); 177 } 178 this.table_name = tname; 179 } 180 181 182 184 187 public Object clone() throws CloneNotSupportedException { 188 return super.clone(); 189 } 190 191 194 public String toString() { 195 if (getTableName() != null) { 196 return getTableName() + "." + getName(); 197 } 198 return getName(); 199 } 200 201 204 public String toTechString() { 205 TableName tn = getTableName(); 206 if (tn != null) { 207 return tn.getSchema() + "^" + tn.getName() + "^" + getName(); 208 } 209 return getName(); 210 } 211 212 215 public boolean equals(Object ob) { 216 Variable cn = (Variable) ob; 217 return cn.table_name.equals(table_name) && 218 cn.column_name.equals(column_name); 219 } 220 221 224 public int compareTo(Object ob) { 225 Variable cn = (Variable) ob; 226 int v = table_name.compareTo(cn.table_name); 227 if (v == 0) { 228 return column_name.compareTo(cn.column_name); 229 } 230 return v; 231 } 232 233 236 public int hashCode() { 237 return table_name.hashCode() + column_name.hashCode(); 238 } 239 240 241 274 } 275 | Popular Tags |