| 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 |
/** |
| 11 |
* A mutation for renaming a class or field without changing the instance or |
| 12 |
* field value. For example: |
| 13 |
* <pre class="code"> |
| 14 |
* package my.package; |
| 15 |
* |
| 16 |
* // The old class. Version 0 is implied. |
| 17 |
* // |
| 18 |
* {@literal @Entity} |
| 19 |
* class Person { |
| 20 |
* String name; |
| 21 |
* } |
| 22 |
* |
| 23 |
* // The new class. A new version number must be assigned. |
| 24 |
* // |
| 25 |
* {@literal @Entity(version=1)} |
| 26 |
* class Human { |
| 27 |
* String fullName; |
| 28 |
* } |
| 29 |
* |
| 30 |
* // Add the mutations. |
| 31 |
* // |
| 32 |
* Mutations mutations = new Mutations(); |
| 33 |
* |
| 34 |
* mutations.addRenamer(new Renamer("my.package.Person", 0, |
| 35 |
* Human.class.getName())); |
| 36 |
* |
| 37 |
* mutations.addRenamer(new Renamer("my.package.Person", 0, |
| 38 |
* "name", "fullName")); |
| 39 |
* |
| 40 |
* // Configure the mutations as described {@link Mutations here}.</pre> |
| 41 |
* |
| 42 |
* |
| 43 |
* @see com.sleepycat.persist.evolve Class Evolution |
| 44 |
* @author Mark Hayes |
| 45 |
*/ |
| 46 |
public class Renamer extends Mutation { |
| 47 |
|
| 48 |
private static final long serialVersionUID = 2238151684405810427L; |
| 49 |
|
| 50 |
private String newName; |
| 51 |
|
| 52 |
/** |
| 53 |
* Creates a mutation for renaming the class of all instances of the given |
| 54 |
* class version. |
| 55 |
*/ |
| 56 |
public Renamer(String fromClass, int fromVersion, String toClass) { |
| 57 |
super(fromClass, fromVersion, null); |
| 58 |
newName = toClass; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Creates a mutation for renaming the given field for all instances of the |
| 63 |
* given class version. |
| 64 |
*/ |
| 65 |
public Renamer(String declaringClass, int declaringClassVersion, |
| 66 |
String fromField, String toField) { |
| 67 |
super(declaringClass, declaringClassVersion, fromField); |
| 68 |
newName = toField; |
| 69 |
} |
| 70 |
|
| 71 |
/** |
| 72 |
* Returns the new class or field name specified in the constructor. |
| 73 |
*/ |
| 74 |
public String getNewName() { |
| 75 |
return newName; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Returns true if the new class name is equal in this object and given |
| 80 |
* object, and if the {@link Mutation#equals} method returns true. |
| 81 |
*/ |
| 82 |
@Override |
| 83 |
public boolean equals(Object other) { |
| 84 |
if (other instanceof Renamer) { |
| 85 |
Renamer o = (Renamer) other; |
| 86 |
return newName.equals(o.newName) && |
| 87 |
super.equals(other); |
| 88 |
} else { |
| 89 |
return false; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
@Override |
| 94 |
public int hashCode() { |
| 95 |
return newName.hashCode() + super.hashCode(); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public String toString() { |
| 100 |
return "[Renamer " + super.toString() + |
| 101 |
" NewName: " + newName + ']'; |
| 102 |
} |
| 103 |
} |