HADOOP-16678: Review of ArrayWritable (#1692)

This commit is contained in:
belugabehr 2019-11-04 17:24:44 -05:00 committed by Anu Engineer
parent 2ffec347eb
commit 6f0190d8e4
2 changed files with 25 additions and 26 deletions

View File

@ -18,8 +18,10 @@
package org.apache.hadoop.io; package org.apache.hadoop.io;
import java.io.*; import java.io.DataInput;
import java.lang.reflect.Array; import java.io.DataOutput;
import java.io.IOException;
import java.util.Arrays;
import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.classification.InterfaceStability; import org.apache.hadoop.classification.InterfaceStability;
@ -42,7 +44,7 @@
@InterfaceAudience.Public @InterfaceAudience.Public
@InterfaceStability.Stable @InterfaceStability.Stable
public class ArrayWritable implements Writable { public class ArrayWritable implements Writable {
private Class<? extends Writable> valueClass; private final Class<? extends Writable> valueClass;
private Writable[] values; private Writable[] values;
public ArrayWritable(Class<? extends Writable> valueClass) { public ArrayWritable(Class<? extends Writable> valueClass) {
@ -64,7 +66,7 @@ public ArrayWritable(String[] strings) {
} }
} }
public Class getValueClass() { public Class<? extends Writable> getValueClass() {
return valueClass; return valueClass;
} }
@ -77,16 +79,16 @@ public String[] toStrings() {
} }
public Object toArray() { public Object toArray() {
Object result = Array.newInstance(valueClass, values.length); return Arrays.copyOf(values, values.length);
for (int i = 0; i < values.length; i++) {
Array.set(result, i, values[i]);
}
return result;
} }
public void set(Writable[] values) { this.values = values; } public void set(Writable[] values) {
this.values = values;
}
public Writable[] get() { return values; } public Writable[] get() {
return values;
}
@Override @Override
public void readFields(DataInput in) throws IOException { public void readFields(DataInput in) throws IOException {
@ -106,5 +108,11 @@ public void write(DataOutput out) throws IOException {
} }
} }
@Override
public String toString() {
return "ArrayWritable [valueClass=" + valueClass + ", values="
+ Arrays.toString(values) + "]";
}
} }

View File

@ -18,12 +18,12 @@
package org.apache.hadoop.io; package org.apache.hadoop.io;
import java.io.*; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.junit.Assert.assertArrayEquals; import java.io.IOException;
import org.junit.Test; import org.junit.Test;
@ -84,23 +84,14 @@ public void testArrayWritableToArray() {
/** /**
* test {@link ArrayWritable} constructor with null * test {@link ArrayWritable} constructor with null
*/ */
@Test @Test(expected = IllegalArgumentException.class)
public void testNullArgument() { public void testNullArgument() {
try { new ArrayWritable((Class<? extends Writable>) null);
Class<? extends Writable> valueClass = null;
new ArrayWritable(valueClass);
fail("testNullArgument error !!!");
} catch (IllegalArgumentException exp) {
//should be for test pass
} catch (Exception e) {
fail("testNullArgument error !!!");
}
} }
/** /**
* test {@link ArrayWritable} constructor with {@code String[]} as a parameter * test {@link ArrayWritable} constructor with {@code String[]} as a parameter
*/ */
@SuppressWarnings("deprecation")
@Test @Test
public void testArrayWritableStringConstructor() { public void testArrayWritableStringConstructor() {
String[] original = { "test1", "test2", "test3" }; String[] original = { "test1", "test2", "test3" };