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.BooleanWritable;
21  import org.apache.hadoop.io.ByteWritable;
22  import org.apache.hadoop.io.DoubleWritable;
23  import org.apache.hadoop.io.FloatWritable;
24  import org.apache.hadoop.io.IntWritable;
25  import org.apache.hadoop.io.LongWritable;
26  import org.apache.hadoop.io.Writable;
27  
28  import com.google.common.base.Objects;
29  
30  /**
31   * Holder for java and writable class pair.
32   *
33   * @param <W> writable class type
34   * @param <J> java class type
35   */
36  public class JavaWritablePair<W extends Writable, J> {
37    /** Boolean,BooleanWritable */
38    public static final JavaWritablePair<BooleanWritable, Boolean>
39    BOOLEAN_BOOLEAN_WRITABLE = create(BooleanWritable.class, Boolean.class);
40    /** Byte,ByteWritable */
41    public static final JavaWritablePair<ByteWritable, Byte>
42    BYTE_BYTE_WRITABLE = create(ByteWritable.class, Byte.class);
43    /** Byte,IntWritable */
44    public static final JavaWritablePair<IntWritable, Byte>
45    BYTE_INT_WRITABLE = create(IntWritable.class, Byte.class);
46    /** Byte,LongWritable */
47    public static final JavaWritablePair<LongWritable, Byte>
48    BYTE_LONG_WRITABLE = create(LongWritable.class, Byte.class);
49    /** Double,FloatWritable */
50    public static final JavaWritablePair<FloatWritable, Double>
51    DOUBLE_FLOAT_WRITABLE = create(FloatWritable.class, Double.class);
52    /** Double,DoubleWritable */
53    public static final JavaWritablePair<DoubleWritable, Double>
54    DOUBLE_DOUBLE_WRITABLE = create(DoubleWritable.class, Double.class);
55    /** Float,FloatWritable */
56    public static final JavaWritablePair<FloatWritable, Float>
57    FLOAT_FLOAT_WRITABLE = create(FloatWritable.class, Float.class);
58    /** Float,DoubleWritable */
59    public static final JavaWritablePair<DoubleWritable, Float>
60    FLOAT_DOUBLE_WRITABLE = create(DoubleWritable.class, Float.class);
61    /** Integer,ByteWritable */
62    public static final JavaWritablePair<ByteWritable, Integer>
63    INT_BYTE_WRITABLE = create(ByteWritable.class, Integer.class);
64    /** Integer,IntWritable */
65    public static final JavaWritablePair<IntWritable, Integer>
66    INT_INT_WRITABLE = create(IntWritable.class, Integer.class);
67    /** Integer,LongWritable */
68    public static final JavaWritablePair<LongWritable, Integer>
69    INT_LONG_WRITABLE = create(LongWritable.class, Integer.class);
70    /** Long,ByteWritable */
71    public static final JavaWritablePair<ByteWritable, Long>
72    LONG_BYTE_WRITABLE = create(ByteWritable.class, Long.class);
73    /** Long,IntWritable */
74    public static final JavaWritablePair<IntWritable, Long>
75    LONG_INT_WRITABLE = create(IntWritable.class, Long.class);
76    /** Long,LongWritable */
77    public static final JavaWritablePair<LongWritable, Long>
78    LONG_LONG_WRITABLE = create(LongWritable.class, Long.class);
79    /** Short,ByteWritable */
80    public static final JavaWritablePair<ByteWritable, Short>
81    SHORT_BYTE_WRITABLE = create(ByteWritable.class, Short.class);
82    /** Short,IntWritable */
83    public static final JavaWritablePair<IntWritable, Short>
84    SHORT_INT_WRITABLE = create(IntWritable.class, Short.class);
85    /** Short,LongWritable */
86    public static final JavaWritablePair<LongWritable, Short>
87    SHORT_LONG_WRITABLE = create(LongWritable.class, Short.class);
88  
89    /** java class */
90    private final Class<J> javaClass;
91    /** writable class */
92    private final Class<W> writableClass;
93  
94    /**
95     * Constructor
96     *
97     * @param javaClass java class
98     * @param writableClass writable class
99     */
100   private JavaWritablePair(Class<W> writableClass, Class<J> javaClass) {
101     this.javaClass = javaClass;
102     this.writableClass = writableClass;
103   }
104 
105   /**
106    * Create holder for classes
107    *
108    * @param writableClass writable class
109    * @param javaClass java class
110    * @param <W> writable class type
111    * @param <J> java class type
112    * @return JavaAndWritableClasses
113    */
114   public static <W extends Writable, J> JavaWritablePair<W, J> create(
115       Class<W> writableClass, Class<J> javaClass) {
116     return new JavaWritablePair<W, J>(writableClass, javaClass);
117   }
118 
119   @Override
120   public boolean equals(Object obj) {
121     if (this == obj) {
122       return true;
123     }
124     if (obj instanceof JavaWritablePair) {
125       JavaWritablePair other = (JavaWritablePair) obj;
126       return Objects.equal(javaClass, other.javaClass) &&
127           Objects.equal(writableClass, other.writableClass);
128     }
129     return false;
130   }
131 
132   @Override
133   public int hashCode() {
134     return Objects.hashCode(javaClass, writableClass);
135   }
136 
137   @Override
138   public String toString() {
139     return Objects.toStringHelper(this)
140         .add("javaClass", javaClass.getSimpleName())
141         .add("writableClass", writableClass.getSimpleName())
142         .toString();
143   }
144 
145   public Class<J> getJavaClass() {
146     return javaClass;
147   }
148 
149   public Class<W> getWritableClass() {
150     return writableClass;
151   }
152 }