source: sample/hadoop-0.16/tw/org/nchc/tuple/ArrayListWritableComparable.java @ 70

Last change on this file since 70 was 21, checked in by waue, 16 years ago

hadoop 0.16

File size: 5.0 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.WritableComparable;
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 ArrayListWritableComparable<E extends WritableComparable> extends ArrayList<E> implements WritableComparable{
40
41    private static final long serialVersionUID = 1L;
42
43  /**
44   * Creates an ArrayListWritable object.
45   */
46  public ArrayListWritableComparable() {
47    super();
48  }
49 
50  /**
51   * Creates an ArrayListWritableComparable object from a regular ArrayList.
52   */
53  public ArrayListWritableComparable(ArrayList<E> array) {
54    super(array);
55  }
56
57 
58 
59  /**
60   * Deserializes the array.
61   *
62   * @param in
63   *            source for raw byte representation
64   */
65  @SuppressWarnings("unchecked")
66  public void readFields(DataInput in) throws IOException {
67
68    this.clear();
69
70    int numFields = in.readInt();
71    if(numFields==0) return;
72    String className = in.readUTF();
73    E obj;
74    try {
75      Class c = Class.forName(className);
76      for (int i = 0; i < numFields; i++) {
77        obj = (E) c.newInstance();
78        obj.readFields(in);
79        this.add(obj);
80      }
81
82    } catch (ClassNotFoundException e) {
83      e.printStackTrace();
84    } catch (IllegalAccessException e) {
85      e.printStackTrace();
86    } catch (InstantiationException e) {
87      e.printStackTrace();
88    }
89  }
90
91  /**
92   * Serializes this Tuple.
93   *
94   * @param out
95   *            where to write the raw byte representation
96   */
97  public void write(DataOutput out) throws IOException {
98    out.writeInt(this.size());
99    if(size()==0) return;
100    E obj=get(0);
101   
102    out.writeUTF(obj.getClass().getCanonicalName());
103
104    for (int i = 0; i < size(); i++) {
105      obj = get(i);
106      if (obj == null) {
107        throw new IOException("Cannot serialize null fields!");
108      }
109      obj.write(out);
110    }
111  }
112 
113  /**
114   * <p>
115   * Defines a natural sort order for the ListWritable class. Following
116   * standard convention, this method returns a value less than zero, a value
117   * greater than zero, or zero if this ListWritable should be sorted before,
118   * sorted after, or is equal to <code>obj</code>. The sort order is
119   * defined as follows:
120   * </p>
121   *
122   * <ul>
123   * <li>Each element in the list is compared sequentially from first to
124   * last.</li>
125   * <li>Lists are sorted with respect to the natural order of the current
126   * list element under consideration, by calling its <code>compareTo</code>
127   * method.</li>
128   * <li>If the current list elements are equal, the next set of elements are
129   * considered.</li>
130   * <li>If all compared elements are equal, but lists are different lengths,
131   * the shorter list is sorted first.</li>
132   * <li>If all list elements are equal and the lists are equal in length,
133   * then the lists are considered equal</li>
134   * </ul>
135   *
136   * @return a value less than zero, a value greater than zero, or zero if
137   *         this Tuple should be sorted before, sorted after, or is equal to
138   *         <code>obj</code>.
139   */
140  public int compareTo(Object obj) {
141    ArrayListWritableComparable<?> that = (ArrayListWritableComparable<?>) obj;
142
143    // iterate through the fields
144    for (int i = 0; i < this.size(); i++) {
145      // sort shorter list first
146      if (i >= that.size())
147        return 1;
148
149      @SuppressWarnings("unchecked")
150      Comparable<Object> thisField = this.get(i);
151      @SuppressWarnings("unchecked")
152      Comparable<Object> thatField = that.get(i);
153
154      if (thisField.equals(thatField)) {
155        // if we're down to the last field, sort shorter list first
156        if (i == this.size() - 1) {
157          if (this.size() > that.size())
158            return 1;
159
160          if (this.size() < that.size())
161            return -1;
162        }
163        // otherwise, move to next field
164      } else {
165        return thisField.compareTo(thatField);
166      }
167    }
168
169    return 0;
170  }
171
172
173  /**
174   * Generates human-readable String representation of this ArrayList.
175   *
176   * @return human-readable String representation of this ArrayList
177   */
178  public String toString() {
179    StringBuffer sb = new StringBuffer();
180    sb.append("[");
181    for (int i = 0; i < this.size(); i++){
182      if (i != 0)
183        sb.append(", ");
184      sb.append(this.get(i));
185    }
186    sb.append("]");
187
188    return sb.toString();
189  }
190 
191}
Note: See TracBrowser for help on using the repository browser.