source: proiecte/HadoopJUnit/hadoop-0.20.1/src/core/org/apache/hadoop/io/TwoDArrayWritable.java @ 120

Last change on this file since 120 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 3.0 KB
Line 
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
19package org.apache.hadoop.io;
20
21import java.io.*;
22import java.lang.reflect.Array;
23
24/** A Writable for 2D arrays containing a matrix of instances of a class. */
25public class TwoDArrayWritable implements Writable {
26  private Class valueClass;
27  private Writable[][] values;
28
29  public TwoDArrayWritable(Class valueClass) {
30    this.valueClass = valueClass;
31  }
32
33  public TwoDArrayWritable(Class valueClass, Writable[][] values) {
34    this(valueClass);
35    this.values = values;
36  }
37
38  public Object toArray() {
39    int dimensions[] = {values.length, 0};
40    Object result = Array.newInstance(valueClass, dimensions);
41    for (int i = 0; i < values.length; i++) {
42      Object resultRow = Array.newInstance(valueClass, values[i].length);
43      Array.set(result, i, resultRow);
44      for (int j = 0; j < values[i].length; j++) {
45        Array.set(resultRow, j, values[i][j]);
46      }
47    }
48    return result;
49  }
50
51  public void set(Writable[][] values) { this.values = values; }
52
53  public Writable[][] get() { return values; }
54
55  public void readFields(DataInput in) throws IOException {
56    // construct matrix
57    values = new Writable[in.readInt()][];         
58    for (int i = 0; i < values.length; i++) {
59      values[i] = new Writable[in.readInt()];
60    }
61
62    // construct values
63    for (int i = 0; i < values.length; i++) {
64      for (int j = 0; j < values[i].length; j++) {
65        Writable value;                             // construct value
66        try {
67          value = (Writable)valueClass.newInstance();
68        } catch (InstantiationException e) {
69          throw new RuntimeException(e.toString());
70        } catch (IllegalAccessException e) {
71          throw new RuntimeException(e.toString());
72        }
73        value.readFields(in);                       // read a value
74        values[i][j] = value;                       // store it in values
75      }
76    }
77  }
78
79  public void write(DataOutput out) throws IOException {
80    out.writeInt(values.length);                 // write values
81    for (int i = 0; i < values.length; i++) {
82      out.writeInt(values[i].length);
83    }
84    for (int i = 0; i < values.length; i++) {
85      for (int j = 0; j < values[i].length; j++) {
86        values[i][j].write(out);
87      }
88    }
89  }
90}
91
Note: See TracBrowser for help on using the repository browser.