1 8 9 package collections.ship.sentity; 10 11 import com.sleepycat.bind.EntityBinding; 12 import com.sleepycat.bind.EntryBinding; 13 import com.sleepycat.bind.serial.ClassCatalog; 14 import com.sleepycat.bind.serial.TupleSerialBinding; 15 import com.sleepycat.bind.tuple.TupleBinding; 16 import com.sleepycat.bind.tuple.TupleInput; 17 import com.sleepycat.bind.tuple.TupleOutput; 18 import com.sleepycat.collections.StoredSortedMap; 19 import com.sleepycat.collections.StoredSortedValueSet; 20 21 27 public class SampleViews { 28 29 private StoredSortedMap partMap; 30 private StoredSortedMap supplierMap; 31 private StoredSortedMap shipmentMap; 32 private StoredSortedMap shipmentByPartMap; 33 private StoredSortedMap shipmentBySupplierMap; 34 private StoredSortedMap supplierByCityMap; 35 36 39 public SampleViews(SampleDatabase db) { 40 41 ClassCatalog catalog = db.getClassCatalog(); 49 EntryBinding partKeyBinding = 50 new PartKeyBinding(); 51 EntityBinding partDataBinding = 52 new PartBinding(catalog, Part.class); 53 EntryBinding supplierKeyBinding = 54 new SupplierKeyBinding(); 55 EntityBinding supplierDataBinding = 56 new SupplierBinding(catalog, Supplier.class); 57 EntryBinding shipmentKeyBinding = 58 new ShipmentKeyBinding(); 59 EntityBinding shipmentDataBinding = 60 new ShipmentBinding(catalog, Shipment.class); 61 EntryBinding cityKeyBinding = 62 TupleBinding.getPrimitiveBinding(String .class); 63 64 partMap = 69 new StoredSortedMap(db.getPartDatabase(), 70 partKeyBinding, partDataBinding, true); 71 supplierMap = 72 new StoredSortedMap(db.getSupplierDatabase(), 73 supplierKeyBinding, supplierDataBinding, true); 74 shipmentMap = 75 new StoredSortedMap(db.getShipmentDatabase(), 76 shipmentKeyBinding, shipmentDataBinding, true); 77 shipmentByPartMap = 78 new StoredSortedMap(db.getShipmentByPartDatabase(), 79 partKeyBinding, shipmentDataBinding, true); 80 shipmentBySupplierMap = 81 new StoredSortedMap(db.getShipmentBySupplierDatabase(), 82 supplierKeyBinding, shipmentDataBinding, true); 83 supplierByCityMap = 84 new StoredSortedMap(db.getSupplierByCityDatabase(), 85 cityKeyBinding, supplierDataBinding, true); 86 } 87 88 95 98 public StoredSortedMap getPartMap() { 99 100 return partMap; 101 } 102 103 106 public StoredSortedMap getSupplierMap() { 107 108 return supplierMap; 109 } 110 111 114 public StoredSortedMap getShipmentMap() { 115 116 return shipmentMap; 117 } 118 119 122 public StoredSortedValueSet getPartSet() { 123 124 return (StoredSortedValueSet) partMap.values(); 125 } 126 127 130 public StoredSortedValueSet getSupplierSet() { 131 132 return (StoredSortedValueSet) supplierMap.values(); 133 } 134 135 138 public StoredSortedValueSet getShipmentSet() { 139 140 return (StoredSortedValueSet) shipmentMap.values(); 141 } 142 143 146 public StoredSortedMap getShipmentByPartMap() { 147 148 return shipmentByPartMap; 149 } 150 151 154 public StoredSortedMap getShipmentBySupplierMap() { 155 156 return shipmentBySupplierMap; 157 } 158 159 162 public final StoredSortedMap getSupplierByCityMap() { 163 164 return supplierByCityMap; 165 } 166 167 171 private static class PartKeyBinding extends TupleBinding { 172 173 176 private PartKeyBinding() { 177 } 178 179 182 public Object entryToObject(TupleInput input) { 183 184 String number = input.readString(); 185 return new PartKey(number); 186 } 187 188 191 public void objectToEntry(Object object, TupleOutput output) { 192 193 PartKey key = (PartKey) object; 194 output.writeString(key.getNumber()); 195 } 196 } 197 198 208 private static class PartBinding extends TupleSerialBinding { 209 210 213 private PartBinding(ClassCatalog classCatalog, Class dataClass) { 214 215 super(classCatalog, dataClass); 216 } 217 218 223 public Object entryToObject(TupleInput keyInput, Object dataInput) { 224 225 String number = keyInput.readString(); 226 Part part = (Part) dataInput; 227 part.setKey(number); 228 return part; 229 } 230 231 234 public void objectToKey(Object object, TupleOutput output) { 235 236 Part part = (Part) object; 237 output.writeString(part.getNumber()); 238 } 239 240 244 public Object objectToData(Object object) { 245 246 return object; 247 } 248 } 249 250 254 private static class SupplierKeyBinding extends TupleBinding { 255 256 259 private SupplierKeyBinding() { 260 } 261 262 265 public Object entryToObject(TupleInput input) { 266 267 String number = input.readString(); 268 return new SupplierKey(number); 269 } 270 271 274 public void objectToEntry(Object object, TupleOutput output) { 275 276 SupplierKey key = (SupplierKey) object; 277 output.writeString(key.getNumber()); 278 } 279 } 280 281 291 private static class SupplierBinding extends TupleSerialBinding { 292 293 296 private SupplierBinding(ClassCatalog classCatalog, Class dataClass) { 297 298 super(classCatalog, dataClass); 299 } 300 301 306 public Object entryToObject(TupleInput keyInput, Object dataInput) { 307 308 String number = keyInput.readString(); 309 Supplier supplier = (Supplier) dataInput; 310 supplier.setKey(number); 311 return supplier; 312 } 313 314 317 public void objectToKey(Object object, TupleOutput output) { 318 319 Supplier supplier = (Supplier) object; 320 output.writeString(supplier.getNumber()); 321 } 322 323 327 public Object objectToData(Object object) { 328 329 return object; 330 } 331 } 332 333 337 private static class ShipmentKeyBinding extends TupleBinding { 338 339 342 private ShipmentKeyBinding() { 343 } 344 345 348 public Object entryToObject(TupleInput input) { 349 350 String partNumber = input.readString(); 351 String supplierNumber = input.readString(); 352 return new ShipmentKey(partNumber, supplierNumber); 353 } 354 355 358 public void objectToEntry(Object object, TupleOutput output) { 359 360 ShipmentKey key = (ShipmentKey) object; 361 output.writeString(key.getPartNumber()); 362 output.writeString(key.getSupplierNumber()); 363 } 364 } 365 366 376 private static class ShipmentBinding extends TupleSerialBinding { 377 378 381 private ShipmentBinding(ClassCatalog classCatalog, Class dataClass) { 382 383 super(classCatalog, dataClass); 384 } 385 386 391 public Object entryToObject(TupleInput keyInput, Object dataInput) { 392 393 String partNumber = keyInput.readString(); 394 String supplierNumber = keyInput.readString(); 395 Shipment shipment = (Shipment) dataInput; 396 shipment.setKey(partNumber, supplierNumber); 397 return shipment; 398 } 399 400 403 public void objectToKey(Object object, TupleOutput output) { 404 405 Shipment shipment = (Shipment) object; 406 output.writeString(shipment.getPartNumber()); 407 output.writeString(shipment.getSupplierNumber()); 408 } 409 410 414 public Object objectToData(Object object) { 415 416 return object; 417 } 418 } 419 } 420 | Popular Tags |