1 2 3 package com.db4odoc.f1.refactoring; 4 5 import java.io.File ; 6 7 import com.db4o.Db4o; 8 import com.db4o.ObjectContainer; 9 import com.db4o.ObjectSet; 10 11 public class RefactoringExample { 12 public final static String YAPFILENAME="formula1.yap"; 13 public static void main(String [] args) { 14 System.out.println("Correct sequence of actions: "); 15 setObjects(); 16 checkDB(); 17 changeClass(); 18 setNewObjects(); 19 retrievePilotNew(); 20 21 27 28 } 29 31 public static void setObjects(){ 32 new File (YAPFILENAME).delete(); 33 ObjectContainer oc = Db4o.openFile(YAPFILENAME); 34 try { 35 Pilot pilot = new Pilot("Rubens Barrichello"); 36 oc.set(pilot); 37 pilot = new Pilot("Michael Schumacher"); 38 oc.set(pilot); 39 } finally { 40 oc.close(); 41 } 42 } 43 45 public static void setNewObjects(){ 46 ObjectContainer oc = Db4o.openFile(YAPFILENAME); 47 try { 48 PilotNew pilot = new PilotNew("Rubens Barrichello",99); 49 oc.set(pilot); 50 pilot = new PilotNew("Michael Schumacher",100); 51 oc.set(pilot); 52 } finally { 53 oc.close(); 54 } 55 } 56 58 public static void checkDB(){ 59 ObjectContainer oc = Db4o.openFile(YAPFILENAME); 60 try { 61 ObjectSet result=oc.get(new Object ()); 62 listResult(result); 63 } finally { 64 oc.close(); 65 } 66 } 67 69 public static void changeClass(){ 70 Db4o.configure().objectClass(Pilot.class).rename("com.db4odoc.f1.refactoring.PilotNew"); 71 Db4o.configure().objectClass(PilotNew.class).objectField("name").rename("identity"); 72 ObjectContainer oc = Db4o.openFile(YAPFILENAME); 73 oc.close(); 74 } 75 77 public static void retrievePilotNew(){ 78 ObjectContainer oc = Db4o.openFile(YAPFILENAME); 79 try { 80 ObjectSet result = oc.query(PilotNew.class); 81 listResult(result); 82 } finally { 83 oc.close(); 84 } 85 } 86 88 public static void listResult(ObjectSet result) { 89 System.out.println(result.size()); 90 while(result.hasNext()) { 91 System.out.println(result.next()); 92 } 93 } 94 } 96 | Popular Tags |