source: sample/hadoop-0.17/tw/org/nchc/tuple/ArrayListWritable.java @ 60

Last change on this file since 60 was 20, checked in by waue, 16 years ago

將改完的 hadoop 0.17版package 放來備份
目前繼續開發 hadoop 0.16 + hbase 1.3

File size: 3.1 KB
Line 
1/*
2 * Cloud9: A MapReduce Library for Hadoop
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you
5 * may not use this file except in compliance with the License. You may
6 * obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 * implied. See the License for the specific language governing
14 * permissions and limitations under the License.
15 */
16
17package tw.org.nchc.tuple;
18
19import java.io.DataInput;
20import java.io.DataOutput;
21import java.io.IOException;
22import java.util.ArrayList;
23import org.apache.hadoop.io.Writable;
24
25/**
26 * <p>
27 * Class that represents an array list in Hadoop's data type system. It extends ArrayList class,
28 * hence supports all services provided by ArrayList.
29 * Elements in the list must be homogeneous and must implement Hadoop's Writable interface.
30 * This class, combined with {@link Tuple}, allows the user to
31 * define arbitrarily complex data structures.
32 * </p>
33 *
34 * @see Tuple
35 * @param <E>
36 *            type of list element
37 */
38
39public class ArrayListWritable<E extends Writable> extends ArrayList<E> implements Writable{
40
41    private static final long serialVersionUID = 1L;
42
43  /**
44   * Creates an ArrayListWritable object.
45   */
46  public ArrayListWritable() {
47    super();
48  }
49 
50  /**
51   * Creates an ArrayListWritable object from a regular ArrayList.
52   */
53  public ArrayListWritable(ArrayList<E> array) {
54    super(array);
55  }
56
57  /**
58   * Deserializes the array.
59   *
60   * @param in
61   *            source for raw byte representation
62   */
63  @SuppressWarnings("unchecked")
64  public void readFields(DataInput in) throws IOException {
65
66    this.clear();
67
68    int numFields = in.readInt();
69    if(numFields==0) return;
70    String className = in.readUTF();
71    E obj;
72    try {
73      Class c = Class.forName(className);
74      for (int i = 0; i < numFields; i++) {
75        obj = (E) c.newInstance();
76        obj.readFields(in);
77        this.add(obj);
78      }
79
80    } catch (ClassNotFoundException e) {
81      e.printStackTrace();
82    } catch (IllegalAccessException e) {
83      e.printStackTrace();
84    } catch (InstantiationException e) {
85      e.printStackTrace();
86    }
87  }
88 
89  /**
90   * Serializes this array.
91   *
92   * @param out
93   *            where to write the raw byte representation
94   */
95  public void write(DataOutput out) throws IOException {
96    out.writeInt(this.size());
97    if(size()==0) return;
98    E obj=get(0);
99   
100    out.writeUTF(obj.getClass().getCanonicalName());
101
102    for (int i = 0; i < size(); i++) {
103      obj = get(i);
104      if (obj == null) {
105        throw new IOException("Cannot serialize null fields!");
106      }
107      obj.write(out);
108    }
109  }
110
111  /**
112   * Generates human-readable String representation of this ArrayList.
113   *
114   * @return human-readable String representation of this ArrayList
115   */
116  public String toString() {
117    StringBuffer sb = new StringBuffer();
118    sb.append("[");
119    for (int i = 0; i < this.size(); i++) {
120      if (i != 0)
121        sb.append(", ");
122      sb.append(this.get(i));
123    }
124    sb.append("]");
125
126    return sb.toString();
127  }
128 
129}
Note: See TracBrowser for help on using the repository browser.