001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: Renamer.java,v 1.8.2.2 2008/01/07 15:14:19 cwl Exp $
007: */
008:
009: package com.sleepycat.persist.evolve;
010:
011: /**
012: * A mutation for renaming a class or field without changing the instance or
013: * field value. For example:
014: * <pre class="code">
015: * package my.package;
016: *
017: * // The old class. Version 0 is implied.
018: * //
019: * {@literal @Entity}
020: * class Person {
021: * String name;
022: * }
023: *
024: * // The new class. A new version number must be assigned.
025: * //
026: * {@literal @Entity(version=1)}
027: * class Human {
028: * String fullName;
029: * }
030: *
031: * // Add the mutations.
032: * //
033: * Mutations mutations = new Mutations();
034: *
035: * mutations.addRenamer(new Renamer("my.package.Person", 0,
036: * Human.class.getName()));
037: *
038: * mutations.addRenamer(new Renamer("my.package.Person", 0,
039: * "name", "fullName"));
040: *
041: * // Configure the mutations as described {@link Mutations here}.</pre>
042: *
043: * @see com.sleepycat.persist.evolve Class Evolution
044: * @author Mark Hayes
045: */
046: public class Renamer extends Mutation {
047:
048: private static final long serialVersionUID = 2238151684405810427L;
049:
050: private String newName;
051:
052: /**
053: * Creates a mutation for renaming the class of all instances of the given
054: * class version.
055: */
056: public Renamer(String fromClass, int fromVersion, String toClass) {
057: super (fromClass, fromVersion, null);
058: newName = toClass;
059: }
060:
061: /**
062: * Creates a mutation for renaming the given field for all instances of the
063: * given class version.
064: */
065: public Renamer(String declaringClass, int declaringClassVersion,
066: String fromField, String toField) {
067: super (declaringClass, declaringClassVersion, fromField);
068: newName = toField;
069: }
070:
071: /**
072: * Returns the new class or field name specified in the constructor.
073: */
074: public String getNewName() {
075: return newName;
076: }
077:
078: /**
079: * Returns true if the new class name is equal in this object and given
080: * object, and if the {@link Mutation#equals} method returns true.
081: */
082: @Override
083: public boolean equals(Object other) {
084: if (other instanceof Renamer) {
085: Renamer o = (Renamer) other;
086: return newName.equals(o.newName) && super .equals(other);
087: } else {
088: return false;
089: }
090: }
091:
092: @Override
093: public int hashCode() {
094: return newName.hashCode() + super .hashCode();
095: }
096:
097: @Override
098: public String toString() {
099: return "[Renamer " + super .toString() + " NewName: " + newName
100: + ']';
101: }
102: }
|