001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 package org.apache.hadoop.io;
020
021 import java.io.*;
022 import java.lang.reflect.Array;
023
024 import org.apache.hadoop.classification.InterfaceAudience;
025 import org.apache.hadoop.classification.InterfaceStability;
026
027 /** A Writable for 2D arrays containing a matrix of instances of a class. */
028 @InterfaceAudience.Public
029 @InterfaceStability.Stable
030 public class TwoDArrayWritable implements Writable {
031 private Class valueClass;
032 private Writable[][] values;
033
034 public TwoDArrayWritable(Class valueClass) {
035 this.valueClass = valueClass;
036 }
037
038 public TwoDArrayWritable(Class valueClass, Writable[][] values) {
039 this(valueClass);
040 this.values = values;
041 }
042
043 public Object toArray() {
044 int dimensions[] = {values.length, 0};
045 Object result = Array.newInstance(valueClass, dimensions);
046 for (int i = 0; i < values.length; i++) {
047 Object resultRow = Array.newInstance(valueClass, values[i].length);
048 Array.set(result, i, resultRow);
049 for (int j = 0; j < values[i].length; j++) {
050 Array.set(resultRow, j, values[i][j]);
051 }
052 }
053 return result;
054 }
055
056 public void set(Writable[][] values) { this.values = values; }
057
058 public Writable[][] get() { return values; }
059
060 public void readFields(DataInput in) throws IOException {
061 // construct matrix
062 values = new Writable[in.readInt()][];
063 for (int i = 0; i < values.length; i++) {
064 values[i] = new Writable[in.readInt()];
065 }
066
067 // construct values
068 for (int i = 0; i < values.length; i++) {
069 for (int j = 0; j < values[i].length; j++) {
070 Writable value; // construct value
071 try {
072 value = (Writable)valueClass.newInstance();
073 } catch (InstantiationException e) {
074 throw new RuntimeException(e.toString());
075 } catch (IllegalAccessException e) {
076 throw new RuntimeException(e.toString());
077 }
078 value.readFields(in); // read a value
079 values[i][j] = value; // store it in values
080 }
081 }
082 }
083
084 public void write(DataOutput out) throws IOException {
085 out.writeInt(values.length); // write values
086 for (int i = 0; i < values.length; i++) {
087 out.writeInt(values[i].length);
088 }
089 for (int i = 0; i < values.length; i++) {
090 for (int j = 0; j < values[i].length; j++) {
091 values[i][j].write(out);
092 }
093 }
094 }
095 }
096