1 8 9 package collections.ship.tuple; 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(); 48 EntryBinding partKeyBinding = 49 new PartKeyBinding(); 50 EntityBinding partDataBinding = 51 new PartBinding(catalog, PartData.class); 52 EntryBinding supplierKeyBinding = 53 new SupplierKeyBinding(); 54 EntityBinding supplierDataBinding = 55 new SupplierBinding(catalog, SupplierData.class); 56 EntryBinding shipmentKeyBinding = 57 new ShipmentKeyBinding(); 58 EntityBinding shipmentDataBinding = 59 new ShipmentBinding(catalog, ShipmentData.class); 60 EntryBinding cityKeyBinding = 61 TupleBinding.getPrimitiveBinding(String .class); 62 63 partMap = 68 new StoredSortedMap(db.getPartDatabase(), 69 partKeyBinding, partDataBinding, true); 70 supplierMap = 71 new StoredSortedMap(db.getSupplierDatabase(), 72 supplierKeyBinding, supplierDataBinding, true); 73 shipmentMap = 74 new StoredSortedMap(db.getShipmentDatabase(), 75 shipmentKeyBinding, shipmentDataBinding, true); 76 shipmentByPartMap = 77 new StoredSortedMap(db.getShipmentByPartDatabase(), 78 partKeyBinding, shipmentDataBinding, true); 79 shipmentBySupplierMap = 80 new StoredSortedMap(db.getShipmentBySupplierDatabase(), 81 supplierKeyBinding, shipmentDataBinding, true); 82 supplierByCityMap = 83 new StoredSortedMap(db.getSupplierByCityDatabase(), 84 cityKeyBinding, supplierDataBinding, true); 85 } 86 87 94 97 public StoredSortedMap getPartMap() { 98 99 return partMap; 100 } 101 102 105 public StoredSortedMap getSupplierMap() { 106 107 return supplierMap; 108 } 109 110 113 public StoredSortedMap getShipmentMap() { 114 115 return shipmentMap; 116 } 117 118 121 public StoredSortedValueSet getPartSet() { 122 123 return (StoredSortedValueSet) partMap.values(); 124 } 125 126 129 public StoredSortedValueSet getSupplierSet() { 130 131 return (StoredSortedValueSet) supplierMap.values(); 132 } 133 134 137 public StoredSortedValueSet getShipmentSet() { 138 139 return (StoredSortedValueSet) shipmentMap.values(); 140 } 141 142 145 public StoredSortedMap getShipmentByPartMap() { 146 147 return shipmentByPartMap; 148 } 149 150 153 public StoredSortedMap getShipmentBySupplierMap() { 154 155 return shipmentBySupplierMap; 156 } 157 158 161 public final StoredSortedMap getSupplierByCityMap() { 162 163 return supplierByCityMap; 164 } 165 166 170 private static class PartKeyBinding extends TupleBinding { 171 172 175 private PartKeyBinding() { 176 } 177 178 181 public Object entryToObject(TupleInput input) { 182 183 String number = input.readString(); 184 return new PartKey(number); 185 } 186 187 190 public void objectToEntry(Object object, TupleOutput output) { 191 192 PartKey key = (PartKey) object; 193 output.writeString(key.getNumber()); 194 } 195 } 196 197 201 private static class PartBinding extends TupleSerialBinding { 202 203 206 private PartBinding(ClassCatalog classCatalog, Class dataClass) { 207 208 super(classCatalog, dataClass); 209 } 210 211 214 public Object entryToObject(TupleInput keyInput, Object dataInput) { 215 216 String number = keyInput.readString(); 217 PartData data = (PartData) dataInput; 218 return new Part(number, data.getName(), data.getColor(), 219 data.getWeight(), data.getCity()); 220 } 221 222 225 public void objectToKey(Object object, TupleOutput output) { 226 227 Part part = (Part) object; 228 output.writeString(part.getNumber()); 229 } 230 231 234 public Object objectToData(Object object) { 235 236 Part part = (Part) object; 237 return new PartData(part.getName(), part.getColor(), 238 part.getWeight(), part.getCity()); 239 } 240 } 241 242 246 private static class SupplierKeyBinding extends TupleBinding { 247 248 251 private SupplierKeyBinding() { 252 } 253 254 257 public Object entryToObject(TupleInput input) { 258 259 String number = input.readString(); 260 return new SupplierKey(number); 261 } 262 263 266 public void objectToEntry(Object object, TupleOutput output) { 267 268 SupplierKey key = (SupplierKey) object; 269 output.writeString(key.getNumber()); 270 } 271 } 272 273 277 private static class SupplierBinding extends TupleSerialBinding { 278 279 282 private SupplierBinding(ClassCatalog classCatalog, Class dataClass) { 283 284 super(classCatalog, dataClass); 285 } 286 287 290 public Object entryToObject(TupleInput keyInput, Object dataInput) { 291 292 String number = keyInput.readString(); 293 SupplierData data = (SupplierData) dataInput; 294 return new Supplier(number, data.getName(), 295 data.getStatus(), data.getCity()); 296 } 297 298 301 public void objectToKey(Object object, TupleOutput output) { 302 303 Supplier supplier = (Supplier) object; 304 output.writeString(supplier.getNumber()); 305 } 306 307 310 public Object objectToData(Object object) { 311 312 Supplier supplier = (Supplier) object; 313 return new SupplierData(supplier.getName(), supplier.getStatus(), 314 supplier.getCity()); 315 } 316 } 317 318 322 private static class ShipmentKeyBinding extends TupleBinding { 323 324 327 private ShipmentKeyBinding() { 328 } 329 330 333 public Object entryToObject(TupleInput input) { 334 335 String partNumber = input.readString(); 336 String supplierNumber = input.readString(); 337 return new ShipmentKey(partNumber, supplierNumber); 338 } 339 340 343 public void objectToEntry(Object object, TupleOutput output) { 344 345 ShipmentKey key = (ShipmentKey) object; 346 output.writeString(key.getPartNumber()); 347 output.writeString(key.getSupplierNumber()); 348 } 349 } 350 351 355 private static class ShipmentBinding extends TupleSerialBinding { 356 357 360 private ShipmentBinding(ClassCatalog classCatalog, Class dataClass) { 361 362 super(classCatalog, dataClass); 363 } 364 365 368 public Object entryToObject(TupleInput keyInput, Object dataInput) { 369 370 String partNumber = keyInput.readString(); 371 String supplierNumber = keyInput.readString(); 372 ShipmentData data = (ShipmentData) dataInput; 373 return new Shipment(partNumber, supplierNumber, 374 data.getQuantity()); 375 } 376 377 380 public void objectToKey(Object object, TupleOutput output) { 381 382 Shipment shipment = (Shipment) object; 383 output.writeString(shipment.getPartNumber()); 384 output.writeString(shipment.getSupplierNumber()); 385 } 386 387 390 public Object objectToData(Object object) { 391 392 Shipment shipment = (Shipment) object; 393 return new ShipmentData(shipment.getQuantity()); 394 } 395 } 396 } 397 | Popular Tags |