This project has retired. For details please refer to its Attic page.
WritableWrappers xref
View Javadoc

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  package org.apache.giraph.types;
19  
20  import org.apache.hadoop.io.Writable;
21  
22  import com.google.common.collect.Maps;
23  
24  import java.util.Map;
25  
26  import static org.apache.giraph.types.JavaWritablePair.BOOLEAN_BOOLEAN_WRITABLE;
27  import static org.apache.giraph.types.JavaWritablePair.BYTE_BYTE_WRITABLE;
28  import static org.apache.giraph.types.JavaWritablePair.BYTE_INT_WRITABLE;
29  import static org.apache.giraph.types.JavaWritablePair.BYTE_LONG_WRITABLE;
30  import static org.apache.giraph.types.JavaWritablePair.DOUBLE_DOUBLE_WRITABLE;
31  import static org.apache.giraph.types.JavaWritablePair.FLOAT_DOUBLE_WRITABLE;
32  import static org.apache.giraph.types.JavaWritablePair.FLOAT_FLOAT_WRITABLE;
33  import static org.apache.giraph.types.JavaWritablePair.INT_INT_WRITABLE;
34  import static org.apache.giraph.types.JavaWritablePair.INT_LONG_WRITABLE;
35  import static org.apache.giraph.types.JavaWritablePair.LONG_LONG_WRITABLE;
36  import static org.apache.giraph.types.JavaWritablePair.SHORT_INT_WRITABLE;
37  import static org.apache.giraph.types.JavaWritablePair.SHORT_LONG_WRITABLE;
38  
39  /**
40   * Mapping of all the known Writable wrappers.
41   */
42  public class WritableWrappers {
43    /** Map of (Writable,Java)-type pair to wrapper for those types */
44    private static final Map<JavaWritablePair, WritableWrapper> MAP;
45  
46    static {
47      MAP = Maps.newHashMap();
48      MAP.put(BOOLEAN_BOOLEAN_WRITABLE, new BooleanToBooleanWritableWrapper());
49      MAP.put(BYTE_BYTE_WRITABLE, new ByteToByteWritableWrapper());
50      MAP.put(BYTE_INT_WRITABLE, new ByteToIntWritableWrapper());
51      MAP.put(BYTE_LONG_WRITABLE, new ByteToLongWritableWrapper());
52      MAP.put(DOUBLE_DOUBLE_WRITABLE, new DoubleToDoubleWritableWrapper());
53      MAP.put(FLOAT_DOUBLE_WRITABLE, new FloatToDoubleWritableWrapper());
54      MAP.put(FLOAT_FLOAT_WRITABLE, new FloatToFloatWritableWrapper());
55      MAP.put(INT_INT_WRITABLE, new IntToIntWritableWrapper());
56      MAP.put(INT_LONG_WRITABLE, new IntToLongWritableWrapper());
57      MAP.put(LONG_LONG_WRITABLE, new LongToLongWritableWrapper());
58      MAP.put(SHORT_INT_WRITABLE, new ShortToIntWritableWrapper());
59      MAP.put(SHORT_LONG_WRITABLE, new ShortToLongWritableWrapper());
60    }
61  
62    /** Don't construct */
63    private WritableWrappers() { }
64  
65    /**
66     * Lookup type converter
67     *
68     * @param writableClass class of Writable
69     * @param javaClass class of Java type
70     * @param <W> Writable type
71     * @param <J> Java type
72     * @return {@link WritableWrapper}
73     */
74    public static <W extends Writable, J> WritableWrapper<W, J> lookup(
75        Class<W> writableClass, Class<J> javaClass) {
76      return lookup(JavaWritablePair.create(writableClass, javaClass));
77    }
78  
79    /**
80     * Lookup type converter
81     *
82     * @param classes JavaAndWritableClasses
83     * @param <W> Writable type
84     * @param <J> Java type
85     * @return {@link WritableWrapper}
86     */
87    public static <W extends Writable, J> WritableWrapper<W, J> lookup(
88        JavaWritablePair<W, J> classes) {
89      return MAP.get(classes);
90    }
91  }