Created
May 23, 2018 11:29
-
-
Save bdarfler/6a1baf68513be6cd02409a29c0de5edc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static com.google.common.collect.Collections2.filter; | |
import static com.google.common.collect.Collections2.transform; | |
import static org.apache.commons.lang.StringUtils.join; | |
import static org.apache.commons.lang.StringUtils.lowerCase; | |
import static org.apache.commons.lang.StringUtils.splitByCharacterTypeCamelCase; | |
import static org.jvnet.inflector.Noun.pluralOf; | |
import java.util.Collection; | |
import java.util.Map; | |
import java.util.concurrent.ConcurrentHashMap; | |
import java.util.concurrent.ConcurrentMap; | |
import org.apache.commons.beanutils.BeanMap; | |
import org.apache.commons.lang.builder.EqualsBuilder; | |
import org.apache.commons.lang.builder.HashCodeBuilder; | |
import org.apache.commons.lang.builder.ReflectionToStringBuilder; | |
import org.apache.commons.lang.builder.ToStringStyle; | |
import com.google.common.base.Function; | |
import com.google.common.base.Predicate; | |
import com.google.common.collect.ImmutableSet; | |
public abstract class AbstractDbObj implements DbObj { | |
private static final String[] EXCLUDED_FIELD_NAMES = new String[] { “columnNames”, “map”, “fieldNames”, “tableName” }; | |
private static final Predicate<String> IS_COLUMN = new Predicate<String>() { | |
private final ImmutableSet<String> notColumns = ImmutableSet.of( “class”, “tableName”, “columnNames”, “fieldNames” ); | |
public boolean apply( final String propName ) { | |
return !notColumns.contains( propName ); | |
} | |
}; | |
private static final ConcurrentMap<Class<? extends LocaDbObj>, String> TABLE_NAME_LOOKUP = new ConcurrentHashMap<Class<? extends LocaDbObj>, String>(); | |
private static final Function<String, String> TO_COLUMN = new Function<String, String>() { | |
public String apply( final String fieldName ) { | |
return underscore( fieldName ); | |
} | |
}; | |
private static String tableName( final Class<? extends LocaDbObj> clazz ) { | |
return pluralOf( underscore( clazz.getSimpleName() ) ); | |
} | |
protected static String underscore( final String camelCasedWord ) { | |
return lowerCase( join( splitByCharacterTypeCamelCase( camelCasedWord ), ‘_’ ) ); | |
} | |
private Integer id; | |
private final Collection<String> fieldNames; | |
private final String tableName; | |
private final Map<String, Object> map; | |
private final Collection<String> columnNames; | |
@SuppressWarnings( “unchecked” ) | |
protected AbstractLocaDbObj() { | |
super(); | |
final Class<? extends LocaDbObj> clazz = getClass(); | |
String tblName = TABLE_NAME_LOOKUP.get( clazz ); | |
if ( tblName == null ) { | |
tblName = tableName( clazz ); | |
TABLE_NAME_LOOKUP.put( clazz, tblName ); | |
} | |
tableName = tblName; | |
final BeanMap beanMap = new BeanMap( this ); | |
fieldNames = filter( beanMap.keySet(), IS_COLUMN ); | |
columnNames = transform( fieldNames, TO_COLUMN ); | |
map = beanMap; | |
} | |
@SuppressWarnings( “unchecked” ) | |
protected AbstractLocaDbObj( final String tableName ) { | |
super(); | |
this.tableName = tableName; | |
final BeanMap beanMap = new BeanMap( this ); | |
fieldNames = filter( beanMap.keySet(), IS_COLUMN ); | |
columnNames = transform( fieldNames, TO_COLUMN ); | |
map = beanMap; | |
} | |
@Override | |
public boolean equals( final Object obj ) { | |
return EqualsBuilder.reflectionEquals( this, obj, EXCLUDED_FIELD_NAMES ); | |
} | |
public String getColumnName( final String fieldName ) { | |
return underscore( fieldName ); | |
} | |
public Collection<String> getColumnNames() { | |
return columnNames; | |
} | |
public Collection<String> getFieldNames() { | |
return fieldNames; | |
} | |
public Integer getId() { | |
return id; | |
} | |
public String getTableName() { | |
return tableName; | |
} | |
@Override | |
public int hashCode() { | |
return HashCodeBuilder.reflectionHashCode( this, EXCLUDED_FIELD_NAMES ); | |
} | |
public void setId( final Integer id ) { | |
this.id = id; | |
} | |
public Map<String, Object> toMap() { | |
return map; | |
} | |
@Override | |
public String toString() { | |
return new ReflectionToStringBuilder( this ).setExcludeFieldNames(EXCLUDED_FIELD_NAMES ).toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment