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