gu.sql2java.BaseRow Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sql2java-base Show documentation
Show all versions of sql2java-base Show documentation
sql2java common class package
package gu.sql2java;
import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.Map.Entry;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.base.Throwables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.google.common.primitives.Ints;
import static com.google.common.base.Preconditions.*;
/**
* abstract implementation of {@link BaseBean}
* @author guyadong
*
*/
public abstract class BaseRow implements BaseBean,Comparable,Cloneable {
protected final RowMetaData metaData;
private volatile Map mapView;
protected BaseRow(RowMetaData metaData) {
this.metaData = checkNotNull(metaData,"metaData is null");
}
protected BaseRow() {
this.metaData = checkNotNull(RowMetaData.getMetaData(getClass()),"metaData is null");
}
@Override
public final boolean isInitialized(String column) {
return isInitialized(metaData.columnIDOf(column));
}
@Override
public boolean beModified() {
for(int i=0; i T getValue(String column) {
return getValue(metaData.columnIDOf(column));
}
@Override
public final T getValueChecked(int columnID) {
T value = getValue(columnID);
return Preconditions.checkNotNull(value,"value of columnid %s IS NULL", columnID);
}
@Override
public final T getValueChecked(String column) {
T value = getValue(column);
return Preconditions.checkNotNull(value,"value of column %s IS NULL", column);
}
@Override
public final void setValue(String column, T value) {
setValue(metaData.columnIDOf(column),value);
}
@Override
public final void setValueIfNonNull(String column, T value)
{
if(value != null){
setValue(column,value);
}
}
@SuppressWarnings("unchecked")
@Override
public T getValue(int columnID)
{
try {
return (T) metaData.getterMethods.get(columnID).invoke(this);
} catch (IndexOutOfBoundsException e) {
return null;
}catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
@Override
public void setValue(int columnID,T value)
{
try {
metaData.setterMethods.get(columnID).invoke(this, value);
} catch (IndexOutOfBoundsException e) {
return ;
}catch (Exception e) {
Throwables.throwIfUnchecked(e);
throw new RuntimeException(e);
}
}
@Override
public final void setValueIfNonNull(int columnID,T value)
{
if(value != null){
setValue(columnID,value);
}
}
@Override
public Object[] primaryValues()
{
Object[] values = new Object[metaData.primaryKeyCount];
for(int i = 0; i < values.length; ++i) {
values[i] = getValue(metaData.primaryKeyIds[i]);
}
return values;
}
@Override
public T primaryValue()
{
if(metaData.primaryKeyCount != 1) {
throw new UnsupportedOperationException();
}
return getValue(metaData.primaryKeyIds[0]);
}
@Override
public Object[] asValueArray(int...columnIds)
{
if(null == columnIds || columnIds.length == 0){
columnIds = metaData.defaultColumnIdList;
}
Object[] v = new Object[columnIds.length];
for(int i=0; i< v.length; ++i){
v[i] = getValue(columnIds[i]);
}
return v;
}
@Override
public Map asNameValueMap(){
// double check
if(mapView == null){
synchronized (this) {
if(mapView == null){
mapView = new RowMapView(this);
}
}
}
return mapView;
}
@Override
public Map asNameValueMap(boolean ignoreNull,String ...ignoreColumns){
return asNameValueMap(ignoreNull, false,null != ignoreColumns ? Arrays.asList(ignoreColumns) : Collections.emptySet());
}
@Override
public Map asNameValueMap(boolean ignoreNull,IterableignoreColumns){
return asNameValueMap(ignoreNull,false,ignoreColumns);
}
@Override
public Map asNameValueMap(boolean ignoreNull,boolean include,String ...includeColumns){
return asNameValueMap(ignoreNull, include,null != includeColumns ? Arrays.asList(includeColumns) : Collections.emptySet());
}
@Override
public Map asNameValueMap(boolean ignoreNull,final boolean include,Iterablecolumns){
Map map = asNameValueMap();
if(ignoreNull){
map = Maps.filterValues(map, Predicates.notNull());
}
if(null != columns){
final Set names = Sets.filter(Sets.newHashSet(columns),Predicates.notNull());
map = Maps.filterKeys(map, new Predicate() {
@Override
public boolean apply(String input) {
return include ? names.contains(input) : !names.contains(input);
}
});
}
return Maps.newLinkedHashMap(map);
}
@Override
public B copy(B bean)
{
return copy(bean,new int[0]);
}
@SuppressWarnings("unchecked")
@Override
public B copy(B bean, int... fieldList)
{
if(bean != null && bean != this){
if (null == fieldList || 0 == fieldList.length){
fieldList = metaData.defaultColumnIdList;
}
for (int columnId:fieldList) {
if( bean.isInitialized(columnId) && !Objects.deepEquals(bean.getValue(columnId), getValue(columnId))){
setValue(columnId, bean.getValue(columnId));
}
}
}
return (B)this;
}
@Override
public B copy(B bean, String... fieldList)
{
return copy(bean,
(null == fieldList)
? null
: Ints.toArray(Lists.transform(Arrays.asList(fieldList), metaData.COLUMNID_FUN)));
}
@Override
@SuppressWarnings("unchecked")
public B copy(F from,Map columnsMap){
for(Map.Entry entry : checkNotNull(columnsMap,"columnsMap is null").entrySet()){
setValue(entry.getValue(), null == from ? null : from.getValue(entry.getKey()));
}
return (B) this;
}
@SuppressWarnings("unchecked")
public B copy(Mapvalues){
if(null != values){
for(Entry entry : values.entrySet()){
setValue(entry.getKey().intValue(), entry.getValue());
}
}
return (B) this;
}
@Override
public final String tableName() {
return metaData.tablename;
}
/**
* cast byte array to HEX string
*
* @param input
* @return {@code null} if {@code input} is null
*/
private static final String toHex(byte[] input) {
if (null == input){
return null;
}
StringBuffer sb = new StringBuffer(input.length * 2);
for (int i = 0; i < input.length; i++) {
sb.append(Character.forDigit((input[i] & 240) >> 4, 16));
sb.append(Character.forDigit(input[i] & 15, 16));
}
return sb.toString();
}
private static final StringBuilder append(StringBuilder buffer,boolean full,byte[] value){
if(full || null == value){
buffer.append(toHex(value));
}else{
buffer.append(value.length).append(" bytes");
}
return buffer;
}
private static int stringLimit = 64;
private static final int MINIMUM_LIMIT = 16;
private static final StringBuilder append(StringBuilder buffer,boolean full,String value){
if(full || null == value || value.length() <= stringLimit){
buffer.append(value);
}else{
buffer.append(value.substring(0,stringLimit - 8)).append(" ...").append(value.substring(stringLimit-4,stringLimit));
}
return buffer;
}
private static final StringBuilder append(StringBuilder buffer,boolean full,Object value){
if(value instanceof String){
return append(buffer, full, (String)value);
}
if(value instanceof byte[]){
return append(buffer, full, (byte[])value);
}
return buffer.append(value);
}
public static final void setStringLimit(int limit){
checkArgument(limit >= MINIMUM_LIMIT, "INVALID limit %s,minimum value %s",limit,MINIMUM_LIMIT);
stringLimit = limit;
}
@Override
public String toString(boolean notNull, boolean fullIfStringOrBytes) {
StringBuilder builder = new StringBuilder(this.getClass().getName()).append("@").append(Integer.toHexString(this.hashCode())).append("[");
int count = 0;
for(int i = 0; i < metaData.columnCount; ++i){
Object value = getValue(i);
if( !notNull || null != value){
if(count > 0){
builder.append(",");
}
builder.append(metaData.columnNameOf(i)).append("=");
append(builder,fullIfStringOrBytes,value);
++count;
}
}
builder.append("]");
return builder.toString();
}
@Override
public boolean equals(Object object) {
if(!metaData.beanType.isInstance(object)){
return false;
}
BaseBean bean = (BaseBean)object;
EqualsBuilder equalsBuilder = new EqualsBuilder();
for(int i=0; i 0 ? metaData.primaryKeyIds : metaData.defaultColumnIdList;
for(int i=0; i
© 2015 - 2025 Weber Informatics LLC | Privacy Policy