com.cloudera.crunch.type.writable.GenericArrayWritable.java Source code

Java tutorial

Introduction

Here is the source code for com.cloudera.crunch.type.writable.GenericArrayWritable.java

Source

/**
 * Copyright (c) 2011, Cloudera, Inc. All Rights Reserved.
 *
 * Cloudera, Inc. licenses this file to you under the Apache License,
 * Version 2.0 (the "License"). You may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
 * CONDITIONS OF ANY KIND, either express or implied. See the License for
 * the specific language governing permissions and limitations under the
 * License.
 */
package com.cloudera.crunch.type.writable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
import org.apache.hadoop.io.WritableFactories;

import com.cloudera.crunch.impl.mr.run.CrunchRuntimeException;

public class GenericArrayWritable<T> implements Writable {
    private Writable[] values;
    private Class<? extends Writable> valueClass;

    public GenericArrayWritable(Class<? extends Writable> valueClass) {
        this.valueClass = valueClass;
    }

    public GenericArrayWritable() {
        // for deserialization
    }

    public void set(Writable[] values) {
        this.values = values;
    }

    public Writable[] get() {
        return values;
    }

    public void readFields(DataInput in) throws IOException {
        values = new Writable[in.readInt()]; // construct values
        if (values.length > 0) {
            String valueType = Text.readString(in);
            setValueType(valueType);
            for (int i = 0; i < values.length; i++) {
                Writable value = WritableFactories.newInstance(valueClass);
                value.readFields(in); // read a value
                values[i] = value; // store it in values
            }
        }
    }

    protected void setValueType(String valueType) {
        if (valueClass == null) {
            try {
                valueClass = Class.forName(valueType).asSubclass(Writable.class);
            } catch (ClassNotFoundException e) {
                throw new CrunchRuntimeException(e);
            }
        } else if (!valueType.equals(valueClass.getName())) {
            throw new IllegalStateException("Incoming " + valueType + " is not " + valueClass);
        }
    }

    public void write(DataOutput out) throws IOException {
        out.writeInt(values.length); // write values;
        if (values.length > 0) {
            if (valueClass == null) {
                throw new IllegalStateException("Value class not set by constructor or read");
            }
            Text.writeString(out, valueClass.getName());
            for (int i = 0; i < values.length; i++) {
                values[i].write(out);
            }
        }
    }

    @Override
    public int hashCode() {
        HashCodeBuilder hcb = new HashCodeBuilder();
        return hcb.append(values).toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        GenericArrayWritable other = (GenericArrayWritable) obj;
        if (!Arrays.equals(values, other.values))
            return false;
        return true;
    }

    @Override
    public String toString() {
        return Arrays.toString(values);
    }
}