blob: fea56a05c0cee14d35a52fe3a15bd3234c9e099f [file] [log] [blame]
/**
* 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 com.datatorrent.contrib.hbase;
import java.util.List;
import com.datatorrent.lib.util.FieldValueGenerator;
import com.datatorrent.lib.util.PojoUtils;
/**
* A {@link FieldValueGenerator} implementation for {@link HBaseFieldInfo}
*
* @since 3.5.0
*/
public class HBaseFieldValueGenerator extends FieldValueGenerator<HBaseFieldInfo>
{
public static final String COLON = ":";
@SuppressWarnings("unchecked")
protected HBaseFieldValueGenerator(final Class<?> clazz, List<HBaseFieldInfo> fieldInfos)
{
for (HBaseFieldInfo fieldInfo : fieldInfos) {
fieldInfoMap.put(fieldInfo.getFamilyName() + COLON + fieldInfo.getColumnName(), fieldInfo);
PojoUtils.Getter<Object, Object> getter =
PojoUtils.createGetter(clazz, fieldInfo.getPojoFieldExpression(), fieldInfo.getType().getJavaType());
fieldGetterMap.put(fieldInfo, getter);
}
for (HBaseFieldInfo fieldInfo : fieldInfos) {
PojoUtils.Setter<Object, Object> setter =
PojoUtils.createSetter(clazz, fieldInfo.getPojoFieldExpression(), fieldInfo.getType().getJavaType());
fieldSetterMap.put(fieldInfo, setter);
}
}
public void setColumnValue(Object instance, String columnName, String columnFamily, Object value,
ValueConverter<HBaseFieldInfo> valueConverter)
{
HBaseFieldInfo fieldInfo = fieldInfoMap.get(columnFamily + COLON + columnName);
PojoUtils.Setter<Object, Object> setter = fieldSetterMap.get(fieldInfo);
setter.set(instance, valueConverter == null ? value : valueConverter.convertValue(fieldInfo, value));
}
}