DefaultConverterFactory implements ConverterFactory { @Override public Converter newConverter(ObjectInspector inspector) { switch (inspector.getCategory()) { case PRIMITIVE: switch (((PrimitiveObjectInspector) inspector).getPrimitiveCategory()) { case STRING: return new StringConverter(); case BOOLEAN: return new BooleanConverter(); case BYTE: return new ByteConverter(); case SHORT: return new ShortConverter(); case INT: return new IntegerConverter(); case LONG: return new LongConverter(); case FLOAT: return new FloatConverter(); case DOUBLE: return new DoubleConverter(); case TIMESTAMP: return new TimestampConverter(); case DATE: return new DateConverter(); case BINARY: return new BinaryConverter(); case CHAR: return new CharConverter(); case VARCHAR: return new VarcharConverter(); case DECIMAL: return new DecimalConverter(); default: throw new IllegalArgumentException( "Unknown Primitive Category: " + ((PrimitiveObjectInspector) inspector).getPrimitiveCategory()); } case STRUCT: return new StructConverter(this, (SettableStructObjectInspector) inspector); case LIST: return new ListConverter(this, (ListObjectInspector) inspector); case MAP: return new MapConverter(this, (MapObjectInspector) inspector); case UNION: return new UnionConverter(this, (UnionObjectInspector) inspector); default: throw new IllegalArgumentException("Unknown Category: " + inspector.getCategory()); } }  @Override Converter newConverter(ObjectInspector inspector);  }
@Test public void stringJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector); assertThat(converter.toJavaObject(new Text("x")), is((Object) "x")); }
@Test public void stringToStringJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector); assertThat(converter.toJavaObject(new Double(2.0)), is((Object) "2.0")); }
@Test public void stringWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector); assertThat(converter.toWritableObject("x"), is((Object) new Text("x"))); }
@Test public void stringNullJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector); assertThat(converter.toJavaObject(null), is(nullValue())); }
@Test public void stringNullWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaStringObjectInspector); assertThat(converter.toWritableObject(null), is(nullValue())); }
@Test public void booleanJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector); assertThat(converter.toJavaObject(new BooleanWritable(true)), is((Object) true)); }
@Test public void booleanWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaBooleanObjectInspector); assertThat(converter.toWritableObject(true), is((Object) new BooleanWritable(true))); }
@Test public void byteJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaByteObjectInspector); assertThat(converter.toJavaObject(new ByteWritable((byte) 1)), is((Object) (byte) 1)); }
@Test public void byteWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaByteObjectInspector); assertThat(converter.toWritableObject((byte) 1), is((Object) new ByteWritable((byte) 1))); }
@Test public void shortJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaShortObjectInspector); assertThat(converter.toJavaObject(new ShortWritable((short) 1)), is((Object) (short) 1)); }
@Test public void shortWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaShortObjectInspector); assertThat(converter.toWritableObject((short) 1), is((Object) new ShortWritable((short) 1))); }
@Test public void intJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector); assertThat(converter.toJavaObject(new IntWritable(1)), is((Object) 1)); }
@Test public void intWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaIntObjectInspector); assertThat(converter.toWritableObject(1), is((Object) new IntWritable(1))); }
@Test public void longJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaLongObjectInspector); assertThat(converter.toJavaObject(new LongWritable(1L)), is((Object) 1L)); }
@Test public void longWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaLongObjectInspector); assertThat(converter.toWritableObject(1L), is((Object) new LongWritable(1L))); }
@Test public void floatJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaFloatObjectInspector); assertThat(converter.toJavaObject(new FloatWritable(1.0F)), is((Object) 1.0F)); }
@Test public void floatWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaFloatObjectInspector); assertThat(converter.toWritableObject(1.0F), is((Object) new FloatWritable(1.0F))); }
@Test public void doubleJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaDoubleObjectInspector); assertThat(converter.toJavaObject(new DoubleWritable(1.0D)), is((Object) 1.0D)); }
@Test public void doubleWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaDoubleObjectInspector); assertThat(converter.toWritableObject(1.0D), is((Object) new DoubleWritable(1.0D))); }
@Test public void timestampJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaTimestampObjectInspector); assertThat(converter.toJavaObject(new TimestampWritable(new Timestamp(0L))), is((Object) new Timestamp(0L))); }
@Test public void timestampWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaTimestampObjectInspector); assertThat(converter.toWritableObject(new Timestamp(0L)), is((Object) new TimestampWritable(new Timestamp(0L)))); }
@Test public void dateJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaDateObjectInspector); assertThat(((Date) converter.toJavaObject(new DateWritable(Date.valueOf("1970-01-01")))).getTime(), is(Date.valueOf("1970-01-01").getTime())); }
@Test public void dateWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaDateObjectInspector); assertThat(converter.toWritableObject(new Date(0L)), is((Object) new DateWritable(new Date(0L)))); }
@Test public void binaryJava() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector); assertThat(converter.toJavaObject(new BytesWritable(new byte[] { 0, 1 })), is((Object) new byte[] { 0, 1 })); }
@Test public void binaryWritable() throws UnexpectedTypeException { Converter converter = factory.newConverter(PrimitiveObjectInspectorFactory.javaByteArrayObjectInspector); assertThat(converter.toWritableObject(new byte[] { 0, 1 }), is((Object) new BytesWritable(new byte[] { 0, 1 }))); }
@Test public void toJava() throws UnexpectedTypeException { StructTypeInfo nested = new StructTypeInfoBuilder().add("char1", TypeInfoFactory.getCharTypeInfo(1)).build(); TypeInfo typeInfo = new StructTypeInfoBuilder() .add("char1", TypeInfoFactory.getCharTypeInfo(1)) .add("struct_char1", nested) .build(); SettableStructObjectInspector inspector = (SettableStructObjectInspector) OrcStruct.createObjectInspector(typeInfo); Object struct = inspector.create(); inspector.setStructFieldData(struct, inspector.getStructFieldRef("char1"), new HiveCharWritable(new HiveChar("a", -1))); SettableStructObjectInspector nestedInspector = (SettableStructObjectInspector) OrcStruct .createObjectInspector(nested); Object nestedStruct = inspector.create(); nestedInspector.setStructFieldData(nestedStruct, nestedInspector.getStructFieldRef("char1"), new HiveCharWritable(new HiveChar("b", -1))); inspector.setStructFieldData(struct, inspector.getStructFieldRef("struct_char1"), nestedStruct); List<Object> list = new ArrayList<>(); list.add(new HiveChar("a", -1)); list.add(Arrays.asList(new HiveChar("b", -1))); Converter converter = factory.newConverter(inspector); Object convertedList = converter.toJavaObject(struct); assertThat(convertedList, is((Object) list)); Object convertedStruct = converter.toWritableObject(list); assertThat(convertedStruct, is(struct)); }