KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > persist > evolve > Renamer


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: Renamer.java,v 1.8 2006/12/05 01:35:37 mark Exp $
7  */

8
9 package 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  */

46 public class Renamer extends Mutation {
47
48     private static final long serialVersionUID = 2238151684405810427L;
49
50     private String JavaDoc 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 JavaDoc fromClass, int fromVersion, String JavaDoc 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 JavaDoc declaringClass, int declaringClassVersion,
66                    String JavaDoc fromField, String JavaDoc 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 JavaDoc 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 JavaDoc
83     public boolean equals(Object JavaDoc 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 JavaDoc
94     public int hashCode() {
95         return newName.hashCode() + super.hashCode();
96     }
97
98     @Override JavaDoc
99     public String JavaDoc toString() {
100         return "[Renamer " + super.toString() +
101                " NewName: " + newName + ']';
102     }
103 }
104
Popular Tags