| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 2002, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
package com.sleepycat.persist.evolve; |
| 9 |
|
| 10 |
import java.lang.reflect.Method; |
| 11 |
|
| 12 |
import com.sleepycat.compat.DbCompat; |
| 13 |
|
| 14 |
/** |
| 15 |
* A mutation for converting an old version of an object value to conform to |
| 16 |
* the current class or field definition. For example: |
| 17 |
* |
| 18 |
* <pre class="code"> |
| 19 |
* package my.package; |
| 20 |
* |
| 21 |
* // The old class. Version 0 is implied. |
| 22 |
* // |
| 23 |
* {@literal @Entity} |
| 24 |
* class Person { |
| 25 |
* // ... |
| 26 |
* } |
| 27 |
* |
| 28 |
* // The new class. A new version number must be assigned. |
| 29 |
* // |
| 30 |
* {@literal @Entity(version=1)} |
| 31 |
* class Person { |
| 32 |
* // Incompatible changes were made here... |
| 33 |
* } |
| 34 |
* |
| 35 |
* // Add a converter mutation. |
| 36 |
* // |
| 37 |
* Mutations mutations = new Mutations(); |
| 38 |
* |
| 39 |
* mutations.addConverter(new Converter(Person.class.getName(), 0, |
| 40 |
* new MyConversion())); |
| 41 |
* |
| 42 |
* // Configure the mutations as described {@link Mutations here}.</pre> |
| 43 |
* |
| 44 |
* <p>See {@link Conversion} for more information.</p> |
| 45 |
* |
| 46 |
* @see com.sleepycat.persist.evolve Class Evolution |
| 47 |
* @author Mark Hayes |
| 48 |
*/ |
| 49 |
public class Converter extends Mutation { |
| 50 |
|
| 51 |
private static final long serialVersionUID = 4558176842096181863L; |
| 52 |
|
| 53 |
private Conversion conversion; |
| 54 |
|
| 55 |
/** |
| 56 |
* Creates a mutation for converting all instances of the given class |
| 57 |
* version to the current version of the class. |
| 58 |
*/ |
| 59 |
public Converter(String className, |
| 60 |
int classVersion, |
| 61 |
Conversion conversion) { |
| 62 |
this(className, classVersion, null, conversion); |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Creates a mutation for converting all values of the given field in the |
| 67 |
* given class version to a type compatible with the current declared type |
| 68 |
* of the field. |
| 69 |
*/ |
| 70 |
public Converter(String declaringClassName, |
| 71 |
int declaringClassVersion, |
| 72 |
String fieldName, |
| 73 |
Conversion conversion) { |
| 74 |
super(declaringClassName, declaringClassVersion, fieldName); |
| 75 |
this.conversion = conversion; |
| 76 |
|
| 77 |
/* Require explicit implementation of the equals method. */ |
| 78 |
Class cls = conversion.getClass(); |
| 79 |
try { |
| 80 |
Method m = cls.getMethod("equals", Object.class); |
| 81 |
if (m.getDeclaringClass() == Object.class) { |
| 82 |
throw new IllegalArgumentException |
| 83 |
("Conversion class does not implement the equals method " + |
| 84 |
"explicitly (Object.equals is not sufficient): " + |
| 85 |
cls.getName()); |
| 86 |
} |
| 87 |
} catch (NoSuchMethodException e) { |
| 88 |
throw DbCompat.unexpectedException(e); |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Returns the converter instance specified to the constructor. |
| 94 |
*/ |
| 95 |
public Conversion getConversion() { |
| 96 |
return conversion; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns true if the conversion objects are equal in this object and |
| 101 |
* given object, and if the {@link Mutation#equals} superclass method |
| 102 |
* returns true. |
| 103 |
*/ |
| 104 |
@Override |
| 105 |
public boolean equals(Object other) { |
| 106 |
if (other instanceof Converter) { |
| 107 |
Converter o = (Converter) other; |
| 108 |
return conversion.equals(o.conversion) && |
| 109 |
super.equals(other); |
| 110 |
} else { |
| 111 |
return false; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public int hashCode() { |
| 117 |
return conversion.hashCode() + super.hashCode(); |
| 118 |
} |
| 119 |
|
| 120 |
@Override |
| 121 |
public String toString() { |
| 122 |
return "[Converter " + super.toString() + |
| 123 |
" Conversion: " + conversion + ']'; |
| 124 |
} |
| 125 |
} |