This project has retired. For details please refer to its Attic page.
KryoSimpleWrapper 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.kryo;
19  
20  import java.io.DataInput;
21  import java.io.DataOutput;
22  import java.io.IOException;
23  
24  import com.esotericsoftware.kryo.io.Input;
25  import com.esotericsoftware.kryo.io.Output;
26  import org.apache.hadoop.io.Writable;
27  
28  /**
29   * Generic wrapper object, making any object writable.
30   *
31   * Usage of this class is similar to KryoWritableWrapper but
32   * unlike KryoWritableWrapper, this class does not
33   * support recursive/nested objects to provide better
34   * performance.
35   *
36   * If the underlying stream is a kryo output stream than the read/write
37   * happens with a kryo object that doesn't track references, providing
38   * significantly better performance.
39   *
40   * @param <T> Object type
41   */
42  public class KryoSimpleWrapper<T> implements Writable, Boxed<T> {
43  
44    /** Wrapped object */
45    private T object;
46  
47    /**
48     * Create wrapper given an object.
49     * @param object Object instance
50     */
51    public KryoSimpleWrapper(T object) {
52      this.object = object;
53    }
54  
55    /**
56     * Creates wrapper initialized with null.
57     */
58    public KryoSimpleWrapper() {
59    }
60  
61    /**
62     * Unwrap the object value
63     * @return Object value
64     */
65    public T get() {
66      return object;
67    }
68  
69    /**
70     * Set wrapped object value
71     * @param object New object value
72     */
73    public void set(T object) {
74      this.object = object;
75    }
76  
77    @Override
78    public void readFields(DataInput in) throws java.io.IOException {
79      if (in instanceof Input) {
80        Input inp = (Input) in;
81        object = HadoopKryo.readWithKryo(HadoopKryo.getNontrackingKryo(), inp);
82      } else {
83        object = HadoopKryo.readClassAndObj(in);
84      }
85    }
86  
87    @Override
88    public void write(DataOutput out) throws IOException {
89      if (out instanceof Output) {
90        Output outp = (Output) out;
91        HadoopKryo.writeWithKryo(HadoopKryo.getNontrackingKryo(), outp, object);
92      } else {
93        HadoopKryo.writeClassAndObj(out, object);
94      }
95    }
96  }