/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF 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
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License 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 org.apache.hadoop.hive.serde2.objectinspector.primitive;

import org.apache.hadoop.hive.common.type.HiveVarchar;
import org.apache.hadoop.hive.serde2.io.HiveVarcharWritable;
import org.apache.hadoop.hive.serde2.typeinfo.VarcharTypeInfo;
import org.apache.hadoop.hive.serde2.typeinfo.BaseCharUtils;

public class JavaHiveVarcharObjectInspector extends AbstractPrimitiveJavaObjectInspector
implements SettableHiveVarcharObjectInspector {

  // no-arg ctor required for Kyro serialization
  public JavaHiveVarcharObjectInspector() {
  }

  public JavaHiveVarcharObjectInspector(VarcharTypeInfo typeInfo) {
    super(typeInfo);
  }

  @Override
  public HiveVarchar getPrimitiveJavaObject(Object o) {
    if (o == null) {
      return null;
    }
    HiveVarchar value = (HiveVarchar)o;
    if (BaseCharUtils.doesPrimitiveMatchTypeParams(
        value, (VarcharTypeInfo)typeInfo)) {
      return value;
    }
    // value needs to be converted to match the type params (length, etc).
    return getPrimitiveWithParams(value);
  }

  @Override
  public HiveVarcharWritable getPrimitiveWritableObject(Object o) {
    if (o == null) {
      return null;
    }
    return getWritableWithParams((HiveVarchar)o);
  }

  private HiveVarchar getPrimitiveWithParams(HiveVarchar val) {
    HiveVarchar hv = new HiveVarchar(val, getMaxLength());
    return hv;
  }

  private HiveVarcharWritable getWritableWithParams(HiveVarchar val) {
    HiveVarcharWritable newValue = new HiveVarcharWritable();
    newValue.set(val, getMaxLength());
    return newValue;
  }

  @Override
  public Object set(Object o, HiveVarchar value) {
    if (BaseCharUtils.doesPrimitiveMatchTypeParams(
        value, (VarcharTypeInfo)typeInfo)) {
      return o = value;
    } else {
      // Otherwise value may be too long, convert to appropriate value based on params
      return o = new HiveVarchar(value,  getMaxLength());
    }
  }

  @Override
  public Object set(Object o, String value) {
    return o = new HiveVarchar(value, getMaxLength());
  }

  @Override
  public Object create(HiveVarchar value) {
    HiveVarchar hc = new HiveVarchar(value, getMaxLength());
    return hc;
  }

  public int getMaxLength() {
    VarcharTypeInfo ti = (VarcharTypeInfo) typeInfo;
    return ti.getLength();
  }

}

@@ -1028,31 +1028,29 @@
 ypeinfo.
-VarcharTypeInfo
+BaseCharUtils
 ;%0Aimport
@@ -1089,29 +1089,31 @@
 ypeinfo.
-BaseCharUtils
+VarcharTypeInfo
 ;%0A%0Apubli
@@ -1196,16 +1196,18 @@
 spector%0A
+  
 implemen
@@ -1590,16 +1590,17 @@
 Varchar)
+ 
 o;%0A    i
@@ -1641,33 +1641,24 @@
 hTypeParams(
-%0A        
 value, (Varc
@@ -1661,32 +1661,33 @@
 VarcharTypeInfo)
+ 
 typeInfo)) %7B%0A   
@@ -2007,351 +2007,11 @@
 har)
+ 
 o)
-;%0A  %7D%0A%0A  private HiveVarchar getPrimitiveWithParams(HiveVarchar val) %7B%0A    HiveVarchar hv = new HiveVarchar(val, getMaxLength());%0A    return hv;%0A  %7D%0A%0A  private HiveVarcharWritable getWritableWithParams(HiveVarchar val) %7B%0A    HiveVarcharWritable newValue = new HiveVarcharWritable();%0A    newValue.set(val, getMaxLength());%0A    return newValue
 ;%0A  
@@ -2131,17 +2131,8 @@
 ams(
-%0A        
 valu
@@ -2151,16 +2151,17 @@
 ypeInfo)
+ 
 typeInfo
@@ -2177,20 +2177,16 @@
   return
- o =
  value;%0A
@@ -2289,36 +2289,32 @@
 ams%0A      return
- o =
  new HiveVarchar
@@ -2320,17 +2320,16 @@
 r(value,
- 
  getMaxL
@@ -2421,12 +2421,8 @@
 turn
- o =
  new
@@ -2527,24 +2527,14 @@
     
-HiveVarchar hc =
+return
  new
@@ -2574,23 +2574,8 @@
 ));%0A
-    return hc;%0A
   %7D%0A
@@ -2690,10 +2690,326 @@
 );%0A  %7D%0A%0A
+  private HiveVarchar getPrimitiveWithParams(HiveVarchar val) %7B%0A    return new HiveVarchar(val, getMaxLength());%0A  %7D%0A%0A  private HiveVarcharWritable getWritableWithParams(HiveVarchar val) %7B%0A    HiveVarcharWritable newValue = new HiveVarcharWritable();%0A    newValue.set(val, getMaxLength());%0A    return newValue;%0A  %7D%0A%0A
 %7D%0A

Fix confusing dead assignment in return statement (JavaHiveVarcharObjectInspector) (Lars Francke via Szehon)