KickJava   Java API By Example, From Geeks To Geeks.

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


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

8
9 package com.sleepycat.persist.evolve;
10
11 import java.io.Serializable JavaDoc;
12 import java.util.Collection JavaDoc;
13 import java.util.HashMap JavaDoc;
14 import java.util.Map JavaDoc;
15
16 import com.sleepycat.persist.EntityStore;
17 import com.sleepycat.persist.StoreConfig;
18
19 /**
20  * A collection of mutations for configuring class evolution.
21  *
22  * <p>Mutations are configured when a store is opened via {@link
23  * StoreConfig#setMutations StoreConfig.setMutations}. For example:</p>
24  *
25  * <pre class="code">
26  * Mutations mutations = new Mutations();
27  * // Add mutations...
28  * StoreConfig config = new StoreConfig();
29  * config.setMutations(mutations);
30  * EntityStore store = new EntityStore(env, "myStore", config);</pre>
31  *
32  * <p>Mutations cause data conversion to occur lazily as instances are read
33  * from the store. The {@link EntityStore#evolve EntityStore.evolve} method
34  * may also be used to perform eager conversion.</p>
35  *
36  * <p>Not all incompatible class changes can be handled via mutations. For
37  * example, complex refactoring may require a transformation that manipulates
38  * multiple entity instances at once. Such changes are not possible with
39  * mutations but can made by performing a <a
40  * HREF="package-summary.html#storeConversion">store conversion</a>.</p>
41  *
42  * @see com.sleepycat.persist.evolve Class Evolution
43  * @author Mark Hayes
44  */

45 public class Mutations implements Serializable JavaDoc {
46
47     private static final long serialVersionUID = -1744401530444812916L;
48
49     private Map JavaDoc<Mutation,Renamer> renamers;
50     private Map JavaDoc<Mutation,Deleter> deleters;
51     private Map JavaDoc<Mutation,Converter> converters;
52
53     /**
54      * Creates an empty set of mutations.
55      */

56     public Mutations() {
57         renamers = new HashMap JavaDoc<Mutation,Renamer>();
58         deleters = new HashMap JavaDoc<Mutation,Deleter>();
59         converters = new HashMap JavaDoc<Mutation,Converter>();
60     }
61
62     /**
63      * Returns true if no mutations are present.
64      */

65     public boolean isEmpty() {
66         return renamers.isEmpty() &&
67                deleters.isEmpty() &&
68                converters.isEmpty();
69     }
70
71     /**
72      * Adds a renamer mutation.
73      */

74     public void addRenamer(Renamer renamer) {
75         renamers.put(new Key(renamer), renamer);
76     }
77
78     /**
79      * Returns the renamer mutation for the given class, version and field, or
80      * null if none exists. A null field name should be specified to get a
81      * class renamer.
82      */

83     public Renamer getRenamer(String JavaDoc className,
84                               int classVersion,
85                               String JavaDoc fieldName) {
86         return renamers.get(new Key(className, classVersion, fieldName));
87     }
88
89     /**
90      * Returns an unmodifiable collection of all renamer mutations.
91      */

92     public Collection JavaDoc<Renamer> getRenamers() {
93         return renamers.values();
94     }
95
96     /**
97      * Adds a deleter mutation.
98      */

99     public void addDeleter(Deleter deleter) {
100         deleters.put(new Key(deleter), deleter);
101     }
102
103     /**
104      * Returns the deleter mutation for the given class, version and field, or
105      * null if none exists. A null field name should be specified to get a
106      * class deleter.
107      */

108     public Deleter getDeleter(String JavaDoc className,
109                               int classVersion,
110                               String JavaDoc fieldName) {
111         return deleters.get(new Key(className, classVersion, fieldName));
112     }
113
114     /**
115      * Returns an unmodifiable collection of all deleter mutations.
116      */

117     public Collection JavaDoc<Deleter> getDeleters() {
118         return deleters.values();
119     }
120
121     /**
122      * Adds a converter mutation.
123      */

124     public void addConverter(Converter converter) {
125         converters.put(new Key(converter), converter);
126     }
127
128     /**
129      * Returns the converter mutation for the given class, version and field,
130      * or null if none exists. A null field name should be specified to get a
131      * class converter.
132      */

133     public Converter getConverter(String JavaDoc className,
134                                   int classVersion,
135                                   String JavaDoc fieldName) {
136         return converters.get(new Key(className, classVersion, fieldName));
137     }
138
139     /**
140      * Returns an unmodifiable collection of all converter mutations.
141      */

142     public Collection JavaDoc<Converter> getConverters() {
143         return converters.values();
144     }
145
146     private static class Key extends Mutation {
147
148         Key(String JavaDoc className, int classVersion, String JavaDoc fieldName) {
149             super(className, classVersion, fieldName);
150         }
151
152         Key(Mutation mutation) {
153             super(mutation.getClassName(),
154                   mutation.getClassVersion(),
155                   mutation.getFieldName());
156         }
157     }
158
159     /**
160      * Returns true if this collection has the same set of mutations as the
161      * given collection and all mutations are equal.
162      */

163     @Override JavaDoc
164     public boolean equals(Object JavaDoc other) {
165         if (other instanceof Mutations) {
166             Mutations o = (Mutations) other;
167             return renamers.equals(o.renamers) &&
168                    deleters.equals(o.deleters) &&
169                    converters.equals(o.converters);
170         } else {
171             return false;
172         }
173     }
174
175     @Override JavaDoc
176     public int hashCode() {
177         return renamers.hashCode() +
178                deleters.hashCode() +
179                converters.hashCode();
180     }
181
182     @Override JavaDoc
183     public String JavaDoc toString() {
184         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
185         if (renamers.size() > 0) {
186             buf.append(renamers.values());
187         }
188         if (deleters.size() > 0) {
189             buf.append(deleters.values());
190         }
191         if (converters.size() > 0) {
192             buf.append(converters.values());
193         }
194         if (buf.length() > 0) {
195             return buf.toString();
196         } else {
197             return "[Empty Mutations]";
198         }
199     }
200 }
201
Popular Tags