This project has retired. For details please refer to its Attic page.
PairWritable 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.writable.tuple;
19  
20  import java.io.DataInput;
21  import java.io.DataOutput;
22  import java.io.IOException;
23  
24  import org.apache.commons.lang3.tuple.Pair;
25  import org.apache.hadoop.io.Writable;
26  
27  /**
28   * Pair Writable class, that knows types upfront and can deserialize itself.
29   *
30   * PairWritable knows types, as instances are passed through constructor, and
31   * are references are immutable (values themselves are mutable).
32   *
33   * Child classes specify no-arg constructor that passes concrete types in.
34   *
35   * Extends Pair, not ImmutablePair, since later class is final. Code is
36   * copied from it.
37   *
38   * @param <L> Type of the left element
39   * @param <R> Type of the right element
40   */
41  public class PairWritable<L extends Writable, R extends Writable>
42      extends Pair<L, R> implements Writable {
43    /** Left object */
44    private final L left;
45    /** Right object */
46    private final R right;
47  
48    /**
49     * Create a new pair instance.
50     *
51     * @param left  the left value
52     * @param right  the right value
53     */
54    public PairWritable(L left, R right) {
55      this.left = left;
56      this.right = right;
57    }
58  
59    /**
60     * <p>
61     * Obtains an immutable pair of from two objects inferring
62     * the generic types.</p>
63     *
64     * <p>This factory allows the pair to be created using inference to
65     * obtain the generic types.</p>
66     *
67     * @param <L> the left element type
68     * @param <R> the right element type
69     * @param left  the left element, may be null
70     * @param right  the right element, may be null
71     * @return a pair formed from the two parameters, not null
72     */
73    public static <L extends Writable, R extends Writable>
74    PairWritable<L, R> of(L left, R right) {
75      return new PairWritable<L, R>(left, right);
76    }
77  
78    @Override
79    public final L getLeft() {
80      return left;
81    }
82  
83    @Override
84    public final R getRight() {
85      return right;
86    }
87  
88    /**
89     * <p>Throws {@code UnsupportedOperationException}.</p>
90     *
91     * <p>This pair is immutable, so this operation is not supported.</p>
92     *
93     * @param value  the value to set
94     * @return never
95     * @throws UnsupportedOperationException as this operation is not supported
96     */
97    @Override
98    public final R setValue(R value) {
99      throw new UnsupportedOperationException();
100   }
101 
102   @Override
103   public final void write(DataOutput out) throws IOException {
104     left.write(out);
105     right.write(out);
106   }
107 
108   @Override
109   public final void readFields(DataInput in) throws IOException {
110     left.readFields(in);
111     right.readFields(in);
112   }
113 }