KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > evolve > Converter


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: Converter.java,v 1.9 2006/12/05 01:35:36 mark Exp $
7  */

8
9 package com.sleepycat.persist.evolve;
10
11 import java.lang.reflect.Method JavaDoc;
12
13 /**
14  * A mutation for converting an old version of an object value to conform to
15  * the current class or field definition. For example:
16  *
17  * <pre class="code">
18  * package my.package;
19  *
20  * // The old class. Version 0 is implied.
21  * //
22  * {@literal @Entity}
23  * class Person {
24  * // ...
25  * }
26  *
27  * // The new class. A new version number must be assigned.
28  * //
29  * {@literal @Entity(version=1)}
30  * class Person {
31  * // Incompatible changes were made here...
32  * }
33  *
34  * // Add a converter mutation.
35  * //
36  * Mutations mutations = new Mutations();
37  *
38  * mutations.addConverter(new Converter(Person.class.getName(), 0,
39  * new MyConversion()));
40  *
41  * // Configure the mutations as described {@link Mutations here}.</pre>
42  *
43  * <p>See {@link Conversion} for more information.</p>
44  *
45  * @see com.sleepycat.persist.evolve Class Evolution
46  * @author Mark Hayes
47  */

48 public class Converter extends Mutation {
49
50     private static final long serialVersionUID = 4558176842096181863L;
51
52     private Conversion conversion;
53
54     /**
55      * Creates a mutation for converting all instances of the given class
56      * version to the current version of the class.
57      */

58     public Converter(String JavaDoc className,
59                      int classVersion,
60                      Conversion conversion) {
61         this(className, classVersion, null, conversion);
62     }
63
64     /**
65      * Creates a mutation for converting all values of the given field in the
66      * given class version to a type compatible with the current declared type
67      * of the field.
68      */

69     public Converter(String JavaDoc declaringClassName,
70                      int declaringClassVersion,
71                      String JavaDoc fieldName,
72                      Conversion conversion) {
73         super(declaringClassName, declaringClassVersion, fieldName);
74         this.conversion = conversion;
75
76         /* Require explicit implementation of the equals method. */
77         Class JavaDoc cls = conversion.getClass();
78         try {
79             Method JavaDoc m = cls.getMethod("equals", Object JavaDoc.class);
80             if (m.getDeclaringClass() == Object JavaDoc.class) {
81                 throw new IllegalArgumentException JavaDoc
82                     ("Conversion class does not implement the equals method " +
83                      "explicitly (Object.equals is not sufficient): " +
84                      cls.getName());
85             }
86         } catch (NoSuchMethodException JavaDoc e) {
87             throw new IllegalStateException JavaDoc(e);
88         }
89     }
90
91     /**
92      * Returns the converter instance specified to the constructor.
93      */

94     public Conversion getConversion() {
95         return conversion;
96     }
97
98     /**
99      * Returns true if the conversion objects are equal in this object and
100      * given object, and if the {@link Mutation#equals} superclass method
101      * returns true.
102      */

103     @Override JavaDoc
104     public boolean equals(Object JavaDoc other) {
105         if (other instanceof Converter) {
106             Converter o = (Converter) other;
107             return conversion.equals(o.conversion) &&
108                    super.equals(other);
109         } else {
110             return false;
111         }
112     }
113
114     @Override JavaDoc
115     public int hashCode() {
116         return conversion.hashCode() + super.hashCode();
117     }
118
119     @Override JavaDoc
120     public String JavaDoc toString() {
121         return "[Converter " + super.toString() +
122                " Conversion: " + conversion + ']';
123     }
124 }
125
Popular Tags