Java Doc for Conversion.java in  » JMX » je » com » sleepycat » persist » evolve » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » JMX » je » com.sleepycat.persist.evolve 
Source Cross Reference  Class Diagram Java Document (Java Doc) 


com.sleepycat.persist.evolve.Conversion

Conversion
public interface Conversion extends Serializable(Code)
Converts an old version of an object value to conform to the current class or field definition.

The Conversion interface is implemented by the user. A Conversion instance is passed to the Converter.Converter constructor.

The Conversion interface extends Serializable and the Conversion instance is serialized for storage using standard Java serialization. Normally, the Conversion class should only have transient fields that are initialized in the Conversion.initialize method. While non-transient fields are allowed, care must be taken to only include fields that are serializable and will not pull in large amounts of data.

When a class conversion is specified, two special considerations apply:

  1. A class conversion is only applied when to instances of that class. The conversion will not be applied when the class when it appears as a superclass of the instance's class. In this case, a conversion for the instance's class must also be specified.
  2. Although field renaming (as well as all other changes) is handled by the conversion method, a field Renamer is still needed when a secondary key field is renamed and field Deleter is still needed when a secondary key field is deleted. This is necessary for evolution of the metadata; specifically, if the key name changes the database must be renamed and if the key field is deleted the secondary database must be deleted.

The Conversion class must implement the standard equals method. See Conversion.equals for more information.

Conversions of simple types are generally simple. For example, a String field that contains only integer values can be easily converted to an int field:

 // The old class.  Version 0 is implied.
 //
 class Address {
 String zipCode;
 ...
 }
 // The new class.  A new version number must be assigned.
 //
 class Address {
 int zipCode;
 ...
 }
 // The conversion class.
 //
 class MyConversion1 implements Conversion {
 public void initialize(EntityModel model) {
 // No initialization needed.
 }
 public Object convert(Object fromValue) {
 return Integer.valueOf((String) fromValue);
 }
  @Override  public boolean equals(Object o) {
 return o instanceof MyConversion1;
 }
 }
 // Create a field converter mutation.
 //
 Converter converter = new Converter(Address.class.getName(), 0,
 "zipCode", new MyConversion1());
 // Configure the converter as described 
Mutations here .

A conversion may perform arbitrary transformations on an object. For example, a conversion may transform a single String address field into an Address object containing four fields for street, city, state and zip code.

 // The old class.  Version 0 is implied.
 //
 class Person {
 String address;
 ...
 }
 // The new class.  A new version number must be assigned.
 //
 class Person {
 Address address;
 ...
 }
 // The new address class.
 //
 class Address {
 String street;
 String city;
 String state;
 int zipCode;
 ...
 }
 class MyConversion2 implements Conversion {
 private transient RawType addressType;
 public void initialize(EntityModel model) {
 addressType = model.getRawType(Address.class.getName());
 }
 public Object convert(Object fromValue) {
 // Parse the old address and populate the new address fields
 //
 String oldAddress = (String) fromValue;
 addressValues.put("street", parseStreet(oldAddress));
 addressValues.put("city", parseCity(oldAddress));
 addressValues.put("state", parseState(oldAddress));
 addressValues.put("zipCode", parseZipCode(oldAddress));
 // Return new raw Address object
 //
 return new RawObject(addressType, addressValues, null);
 }
  @Override  public boolean equals(Object o) {
 return o instanceof MyConversion2;
 }
 private String parseStreet(String oldAddress) { ... }
 private String parseCity(String oldAddress) { ... }
 private String parseState(String oldAddress) { ... }
 private Integer parseZipCode(String oldAddress) { ... }
 }
 // Create a field converter mutation.
 //
 Converter converter = new Converter(Person.class.getName(), 0,
 "address", new MyConversion2());
 // Configure the converter as described 
Mutations here .

Note that when a conversion returns a RawObject , it must return it with a RawType that is current as defined by the current class definitions. The proper types can be obtained from the EntityModel in the conversion's Conversion.initialize initialize method.

A variation on the example above is where several fields in a class (street, city, state and zipCode) are converted to a single field (address). In this case a class converter rather than a field converter is used.

 // The old class.  Version 0 is implied.
 //
 class Person {
 String street;
 String city;
 String state;
 int zipCode;
 ...
 }
 // The new class.  A new version number must be assigned.
 //
 class Person {
 Address address;
 ...
 }
 // The new address class.
 //
 class Address {
 String street;
 String city;
 String state;
 int zipCode;
 ...
 }
 class MyConversion3 implements Conversion {
 private transient RawType newPersonType;
 private transient RawType addressType;
 public void initialize(EntityModel model) {
 newPersonType = model.getRawType(Person.class.getName());
 addressType = model.getRawType(Address.class.getName());
 }
 public Object convert(Object fromValue) {
 // Get field value maps for old and new objects.
 //
 RawObject person = (RawObject) fromValue;
 RawObject address = new RawObject(addressType, addressValues, null);
 // Remove the old address fields and insert the new one.
 //
 addressValues.put("street", personValues.remove("street"));
 addressValues.put("city", personValues.remove("city"));
 addressValues.put("state", personValues.remove("state"));
 addressValues.put("zipCode", personValues.remove("zipCode"));
 personValues.put("address", address);
 return new RawObject(newPersonType, personValues, person.getSuper());
 }
  @Override  public boolean equals(Object o) {
 return o instanceof MyConversion3;
 }
 }
 // Create a class converter mutation.
 //
 Converter converter = new Converter(Person.class.getName(), 0,
 new MyConversion3());
 // Configure the converter as described 
Mutations here .

A conversion can also handle changes to class hierarchies. For example, if a "name" field originally declared in class A is moved to its superclass B, a conversion can move the field value accordingly:

 // The old classes.  Version 0 is implied.
 //
 class A extends B {
 String name;
 ...
 }
 abstract class B {
 ...
 }
 // The new classes.  A new version number must be assigned.
 //
 class A extends B {
 ...
 }
 abstract class B {
 String name;
 ...
 }
 class MyConversion4 implements Conversion {
 private transient RawType newAType;
 private transient RawType newBType;
 public void initialize(EntityModel model) {
 newAType = model.getRawType(A.class.getName());
 newBType = model.getRawType(B.class.getName());
 }
 public Object convert(Object fromValue) {
 RawObject oldA = (RawObject) fromValue;
 RawObject oldB = oldA.getSuper();
 bValues.put("name", aValues.remove("name"));
 RawObject newB = new RawObject(newBType, bValues, oldB.getSuper());
 RawObject newA = new RawObject(newAType, aValues, newB);
 return newA;
 }
  @Override  public boolean equals(Object o) {
 return o instanceof MyConversion4;
 }
 }
 // Create a class converter mutation.
 //
 Converter converter = new Converter(A.class.getName(), 0,
 new MyConversion4());
 // Configure the converter as described 
Mutations here .

A conversion may return an instance of a different class entirely, as long as it conforms to current class definitions and is the type expected in the given context (a subtype of the old type, or a type compatible with the new field type). For example, a field that is used to discriminate between two types of objects could be removed and replaced by two new subclasses:

 // The old class.  Version 0 is implied.
 //
 class Pet {
 boolean isCatNotDog;
 ...
 }
 // The new classes.  A new version number must be assigned to the Pet class.
 //
 class Pet {
 ...
 }
 class Cat extends Pet {
 ...
 }
 class Dog extends Pet {
 ...
 }
 class MyConversion5 implements Conversion {
 private transient RawType newPetType;
 private transient RawType dogType;
 private transient RawType catType;
 public void initialize(EntityModel model) {
 newPetType = model.getRawType(Pet.class.getName());
 dogType = model.getRawType(Dog.class.getName());
 catType = model.getRawType(Cat.class.getName());
 }
 public Object convert(Object fromValue) {
 RawObject pet = (RawObject) fromValue;
 Boolean isCat = (Boolean) petValues.remove("isCatNotDog");
 RawObject newPet = new RawObject(newPetType, petValues,
 pet.getSuper());
 RawType newSubType = isCat ? catType : dogType;
 return new RawObject(newSubType, Collections.emptyMap(), newPet);
 }
  @Override  public boolean equals(Object o) {
 return o instanceof MyConversion5;
 }
 }
 // Create a class converter mutation.
 //
 Converter converter = new Converter(Pet.class.getName(), 0,
 new MyConversion5());
 // Configure the converter as described 
Mutations here .

The primary limitation of a conversion is that it may access at most a single entity instance at one time. Conversions involving multiple entities at once may be made by performing a store conversion.


See Also:   com.sleepycat.persist.evolve
See Also:    Class Evolution
author:
   Mark Hayes




Method Summary
 Objectconvert(Object fromValue)
     Converts an old version of an object value to conform to the current class or field definition.

If a RuntimeException is thrown by this method, it will be thrown to the original caller.

 booleanequals(Object other)
     The standard equals method that must be implemented by conversion class.

When mutations are specified when opening a store, the specified and previously stored mutations are compared for equality.

 voidinitialize(EntityModel model)
     Initializes the conversion, allowing it to obtain raw type information from the entity model.



Method Detail
convert
Object convert(Object fromValue)(Code)
Converts an old version of an object value to conform to the current class or field definition.

If a RuntimeException is thrown by this method, it will be thrown to the original caller. Similarly, a IllegalArgumentException will be thrown to the original caller if the object returned by this method does not conform to current class definitions.

The class of the input and output object may be one of the simple types or RawObject . For primitive types, the primitive wrapper class is used.


Parameters:
  fromValue - the object value being converted. The type of thisvalue is defined by the old class version that is being converted. the converted object. The type of this value must conform toa current class definition. If this is a class conversion, it mustbe the current version of the class. If this is a field conversion, itmust be of a type compatible with the current declared type of thefield.



equals
boolean equals(Object other)(Code)
The standard equals method that must be implemented by conversion class.

When mutations are specified when opening a store, the specified and previously stored mutations are compared for equality. If they are equal, there is no need to replace the existing mutations in the stored catalog. To accurately determine equality, the conversion class must implement the equals method.

If the equals method is not explicitly implemented by the conversion class or a superclass other than Object , IllegalArgumentException will be thrown when the store is opened.

Normally whenever equals is implemented the hashCode method should also be implemented to support hash sets and maps. However, hash sets and maps containing Conversion objects are not used by the DPL and therefore the DPL does not require hashCode to be implemented.




initialize
void initialize(EntityModel model)(Code)
Initializes the conversion, allowing it to obtain raw type information from the entity model.



www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.