source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/io/WritableName.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 2.9 KB
Line 
1/**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.io;
20
21import java.util.HashMap;
22import java.io.IOException;
23
24import org.apache.hadoop.conf.Configuration;
25
26/** Utility to permit renaming of Writable implementation classes without
27 * invalidiating files that contain their class name.
28 */
29public class WritableName {
30  private static HashMap<String, Class<?>> NAME_TO_CLASS =
31    new HashMap<String, Class<?>>();
32  private static HashMap<Class<?>, String> CLASS_TO_NAME =
33    new HashMap<Class<?>, String>();
34
35  static {                                        // define important types
36    WritableName.setName(NullWritable.class, "null");
37    WritableName.setName(LongWritable.class, "long");
38    WritableName.setName(UTF8.class, "UTF8");
39    WritableName.setName(MD5Hash.class, "MD5Hash");
40  }
41
42  private WritableName() {}                      // no public ctor
43
44  /** Set the name that a class should be known as to something other than the
45   * class name. */
46  public static synchronized void setName(Class writableClass, String name) {
47    CLASS_TO_NAME.put(writableClass, name);
48    NAME_TO_CLASS.put(name, writableClass);
49  }
50
51  /** Add an alternate name for a class. */
52  public static synchronized void addName(Class writableClass, String name) {
53    NAME_TO_CLASS.put(name, writableClass);
54  }
55
56  /** Return the name for a class.  Default is {@link Class#getName()}. */
57  public static synchronized String getName(Class writableClass) {
58    String name = CLASS_TO_NAME.get(writableClass);
59    if (name != null)
60      return name;
61    return writableClass.getName();
62  }
63
64  /** Return the class for a name.  Default is {@link Class#forName(String)}.*/
65  public static synchronized Class<?> getClass(String name, Configuration conf
66                                            ) throws IOException {
67    Class<?> writableClass = NAME_TO_CLASS.get(name);
68    if (writableClass != null)
69      return writableClass.asSubclass(Writable.class);
70    try {
71      return conf.getClassByName(name);
72    } catch (ClassNotFoundException e) {
73      IOException newE = new IOException("WritableName can't load class: " + name);
74      newE.initCause(e);
75      throw newE;
76    }
77  }
78
79}
Note: See TracBrowser for help on using the repository browser.