1/*- 2 * See the file LICENSE for redistribution information. 3 * 4 * Copyright (c) 2002,2008 Oracle. All rights reserved. 5 * 6 * $Id: Renamer.java,v 1.1 2008/02/07 17:12:27 mark Exp $ 7 */ 8 9package com.sleepycat.persist.evolve; 10 11/** 12 * A mutation for renaming a class or field without changing the instance or 13 * field value. For example: 14 * <pre class="code"> 15 * package my.package; 16 * 17 * // The old class. Version 0 is implied. 18 * // 19 * {@literal @Entity} 20 * class Person { 21 * String name; 22 * } 23 * 24 * // The new class. A new version number must be assigned. 25 * // 26 * {@literal @Entity(version=1)} 27 * class Human { 28 * String fullName; 29 * } 30 * 31 * // Add the mutations. 32 * // 33 * Mutations mutations = new Mutations(); 34 * 35 * mutations.addRenamer(new Renamer("my.package.Person", 0, 36 * Human.class.getName())); 37 * 38 * mutations.addRenamer(new Renamer("my.package.Person", 0, 39 * "name", "fullName")); 40 * 41 * // Configure the mutations as described {@link Mutations here}.</pre> 42 * 43 * @see com.sleepycat.persist.evolve Class Evolution 44 * @author Mark Hayes 45 */ 46public 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} 104