001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: Converter.java,v 1.9.2.2 2008/01/07 15:14:19 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.evolve;
010:
011: import java.lang.reflect.Method;
012:
013: /**
014: * A mutation for converting an old version of an object value to conform to
015: * the current class or field definition. For example:
016: *
017: * <pre class="code">
018: * package my.package;
019: *
020: * // The old class. Version 0 is implied.
021: * //
022: * {@literal @Entity}
023: * class Person {
024: * // ...
025: * }
026: *
027: * // The new class. A new version number must be assigned.
028: * //
029: * {@literal @Entity(version=1)}
030: * class Person {
031: * // Incompatible changes were made here...
032: * }
033: *
034: * // Add a converter mutation.
035: * //
036: * Mutations mutations = new Mutations();
037: *
038: * mutations.addConverter(new Converter(Person.class.getName(), 0,
039: * new MyConversion()));
040: *
041: * // Configure the mutations as described {@link Mutations here}.</pre>
042: *
043: * <p>See {@link Conversion} for more information.</p>
044: *
045: * @see com.sleepycat.persist.evolve Class Evolution
046: * @author Mark Hayes
047: */
048: public class Converter extends Mutation {
049:
050: private static final long serialVersionUID = 4558176842096181863L;
051:
052: private Conversion conversion;
053:
054: /**
055: * Creates a mutation for converting all instances of the given class
056: * version to the current version of the class.
057: */
058: public Converter(String className, int classVersion,
059: Conversion conversion) {
060: this (className, classVersion, null, conversion);
061: }
062:
063: /**
064: * Creates a mutation for converting all values of the given field in the
065: * given class version to a type compatible with the current declared type
066: * of the field.
067: */
068: public Converter(String declaringClassName,
069: int declaringClassVersion, String fieldName,
070: Conversion conversion) {
071: super (declaringClassName, declaringClassVersion, fieldName);
072: this .conversion = conversion;
073:
074: /* Require explicit implementation of the equals method. */
075: Class cls = conversion.getClass();
076: try {
077: Method m = cls.getMethod("equals", Object.class);
078: if (m.getDeclaringClass() == Object.class) {
079: throw new IllegalArgumentException(
080: "Conversion class does not implement the equals method "
081: + "explicitly (Object.equals is not sufficient): "
082: + cls.getName());
083: }
084: } catch (NoSuchMethodException e) {
085: throw new IllegalStateException(e);
086: }
087: }
088:
089: /**
090: * Returns the converter instance specified to the constructor.
091: */
092: public Conversion getConversion() {
093: return conversion;
094: }
095:
096: /**
097: * Returns true if the conversion objects are equal in this object and
098: * given object, and if the {@link Mutation#equals} superclass method
099: * returns true.
100: */
101: @Override
102: public boolean equals(Object other) {
103: if (other instanceof Converter) {
104: Converter o = (Converter) other;
105: return conversion.equals(o.conversion)
106: && super .equals(other);
107: } else {
108: return false;
109: }
110: }
111:
112: @Override
113: public int hashCode() {
114: return conversion.hashCode() + super .hashCode();
115: }
116:
117: @Override
118: public String toString() {
119: return "[Converter " + super .toString() + " Conversion: "
120: + conversion + ']';
121: }
122: }
|