1 21 package net.sf.hajdbc.sql; 22 23 import java.util.Properties ; 24 25 import net.sf.hajdbc.Database; 26 27 33 38 43 public abstract class AbstractDatabase<T> implements Database<T> 44 { 45 protected String id; 46 protected String user; 47 protected String password; 48 protected Properties properties = new Properties (); 49 protected int weight = 1; 50 protected boolean dirty = false; 51 52 55 public String getId() 56 { 57 return this.id; 58 } 59 60 63 public void setId(String id) 64 { 65 this.checkDirty(this.id, id); 66 this.id = id; 67 } 68 69 72 public String getUser() 73 { 74 return this.user; 75 } 76 77 80 public void setUser(String user) 81 { 82 this.checkDirty(this.user, user); 83 this.user = user; 84 } 85 86 89 public String getPassword() 90 { 91 return this.password; 92 } 93 94 97 public void setPassword(String password) 98 { 99 this.checkDirty(this.password, password); 100 this.password = password; 101 } 102 103 106 public int getWeight() 107 { 108 return this.weight; 109 } 110 111 114 public void setWeight(int weight) 115 { 116 if (weight < 0) 117 { 118 throw new IllegalArgumentException (); 119 } 120 121 this.checkDirty(this.weight, weight); 122 this.weight = weight; 123 } 124 125 128 @Override 129 public int hashCode() 130 { 131 return this.id.hashCode(); 132 } 133 134 137 @Override 138 public boolean equals(Object object) 139 { 140 if ((object == null) || !Database.class.isInstance(object)) 141 { 142 return false; 143 } 144 145 Database database = (Database) object; 146 147 return this.id.equals(database.getId()); 148 } 149 150 153 @Override 154 public String toString() 155 { 156 return this.id; 157 } 158 159 162 public Properties getProperties() 163 { 164 return this.properties; 165 } 166 167 170 public void setProperties(Properties properties) 171 { 172 this.checkDirty(this.properties, properties); 173 this.properties = properties; 174 } 175 176 179 public void removeProperty(String name) 180 { 181 this.dirty |= this.properties.containsKey(name); 182 this.properties.remove(name); 183 } 184 185 188 public void setProperty(String name, String value) 189 { 190 this.checkDirty(this.properties.getProperty(name), value); 191 this.properties.setProperty(name, value); 192 } 193 194 197 public void clean() 198 { 199 this.dirty = false; 200 } 201 202 205 public boolean isDirty() 206 { 207 return this.dirty; 208 } 209 210 215 protected void checkDirty(Object oldValue, Object newValue) 216 { 217 this.dirty |= ((oldValue != null) && (newValue != null)) ? !oldValue.equals(newValue) : (oldValue != newValue); 218 } 219 220 223 public int compareTo(Database database) 224 { 225 return this.id.compareTo(database.getId()); 226 } 227 } 228 | Popular Tags |