More use of Preconditions.

This commit is contained in:
Michael Bayne
2010-12-08 20:24:36 +00:00
parent 84758df725
commit ce71fa01fa
9 changed files with 70 additions and 123 deletions
@@ -62,6 +62,7 @@ import com.samskivert.depot.impl.clause.UpdateClause;
import com.samskivert.depot.impl.expression.ValueExp;
import com.samskivert.depot.impl.util.SeqImpl;
import static com.google.common.base.Preconditions.checkArgument;
import static com.samskivert.depot.Log.log;
/**
@@ -275,7 +276,6 @@ public abstract class DepotRepository
}
if (cache == CacheStrategy.BEST) {
cache = (reason != null) ? CacheStrategy.NONE : CacheStrategy.SHORT_KEYS;
} else if (reason != null) {
// if user explicitly asked for a strategy we can't do, protest
throw new IllegalArgumentException(
@@ -413,9 +413,7 @@ public abstract class DepotRepository
requireNotComputed(pClass, "update");
DepotMarshaller<? extends PersistentRecord> marsh = _ctx.getMarshaller(pClass);
Key<? extends PersistentRecord> key = marsh.getPrimaryKey(record);
if (key == null) {
throw new IllegalArgumentException("Can't update record with null primary key.");
}
checkArgument(key != null, "Can't update record with null primary key.");
return doUpdate(key, new UpdateClause(pClass, key, marsh.getColumnFieldNames(), record));
}
@@ -436,9 +434,7 @@ public abstract class DepotRepository
requireNotComputed(pClass, "update");
DepotMarshaller<T> marsh = _ctx.getMarshaller(pClass);
Key<T> key = marsh.getPrimaryKey(record);
if (key == null) {
throw new IllegalArgumentException("Can't update record with null primary key.");
}
checkArgument(key != null, "Can't update record with null primary key.");
return doUpdate(key, new UpdateClause(pClass, key, modifiedFields, record));
}
@@ -667,9 +663,7 @@ public abstract class DepotRepository
{
@SuppressWarnings("unchecked") Class<T> type = (Class<T>)record.getClass();
Key<T> primaryKey = _ctx.getMarshaller(type).getPrimaryKey(record);
if (primaryKey == null) {
throw new IllegalArgumentException("Can't delete record with null primary key.");
}
checkArgument(primaryKey != null, "Can't delete record with null primary key.");
return delete(primaryKey);
}
@@ -98,6 +98,7 @@ import com.samskivert.depot.impl.operator.IsNull;
import com.samskivert.depot.impl.operator.MultiOperator;
import com.samskivert.depot.impl.operator.Not;
import static com.google.common.base.Preconditions.checkArgument;
import static com.samskivert.Log.log;
/**
@@ -355,10 +356,8 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
}
_builder.append("select ");
if (_definitions.containsKey(pClass)) {
throw new IllegalArgumentException(
checkArgument(!_definitions.containsKey(pClass),
"Can not yet nest SELECTs on the same persistent record.");
}
Map<String, FieldDefinition> definitionMap = Maps.newHashMap();
for (FieldDefinition definition : selectClause.getFieldDefinitions()) {
@@ -444,10 +443,8 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
public Void visit (UpdateClause updateClause)
{
if (updateClause.getWhereClause() == null) {
throw new IllegalArgumentException(
checkArgument(updateClause.getWhereClause() != null,
"I dare not currently perform UPDATE without a WHERE clause.");
}
Class<? extends PersistentRecord> pClass = updateClause.getPersistentClass();
_innerClause = true;
@@ -785,10 +782,8 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
{
// TODO: nix type and use the class from the supplied ColumnExp
DepotMarshaller<?> dm = _types.getMarshaller(type);
if (dm == null) {
throw new IllegalArgumentException(
"Unknown field on persistent record [record=" + type + ", field=" + field + "]");
}
checkArgument(dm != null, "Unknown field on persistent record [record=%s, field=%s]",
type, field);
FieldMarshaller<?> fm = dm.getFieldMarshaller(field.name);
appendIdentifier(fm.getColumnName());
@@ -800,10 +795,8 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
{
Class<? extends PersistentRecord> type = field.getPersistentClass();
DepotMarshaller<?> dm = _types.getMarshaller(type);
if (dm == null) {
throw new IllegalArgumentException(
"Unknown field on persistent record [record=" + type + ", field=" + field + "]");
}
checkArgument(dm != null, "Unknown field on persistent record [record=%s, field=%s]",
type, field);
// first, see if there's a field definition
FieldMarshaller<?> fm = dm.getFieldMarshaller(field.name);
@@ -813,15 +806,12 @@ public abstract class BuildVisitor implements FragmentVisitor<Void>
if (fieldDef != null) {
boolean useOverride;
if (fieldDef instanceof FieldOverride) {
if (fm.getComputed() != null && dm.getComputed() != null) {
throw new IllegalArgumentException(
checkArgument(fm.getComputed() == null || dm.getComputed() == null,
"FieldOverride cannot be used on @Computed field: " + field);
}
useOverride = _enableOverrides;
} else if (fm.getComputed() == null && dm.getComputed() == null) {
throw new IllegalArgumentException(
"FieldDefinition must not be used on concrete field: " + field);
} else {
checkArgument(fm.getComputed() != null || dm.getComputed() != null,
"FieldDefinition must not be used on concrete field: " + field);
useOverride = true;
}
@@ -70,6 +70,7 @@ import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.expression.SQLExpression;
import com.samskivert.depot.impl.clause.CreateIndexClause;
import static com.google.common.base.Preconditions.checkArgument;
import static com.samskivert.depot.Log.log;
/**
@@ -90,7 +91,7 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
{
_pClass = pClass;
Preconditions.checkArgument(!java.lang.reflect.Modifier.isAbstract(pClass.getModifiers()),
checkArgument(!java.lang.reflect.Modifier.isAbstract(pClass.getModifiers()),
"Can't handle reference to abstract record: " + pClass.getName());
Entity entity = pClass.getAnnotation(Entity.class);
@@ -173,17 +174,13 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
ftype.equals(Short.TYPE) || ftype.equals(Short.class) ||
ftype.equals(Integer.TYPE) || ftype.equals(Integer.class) ||
ftype.equals(Long.TYPE) || ftype.equals(Long.class));
if (!isNumeric) {
throw new IllegalArgumentException(
"Cannot use @GeneratedValue on non-numeric column: " + field.getName());
}
checkArgument(isNumeric, "Cannot use @GeneratedValue on non-numeric column: %s",
field.getName());
switch(gv.strategy()) {
case AUTO:
case IDENTITY:
if (seenIdentityGenerator) {
throw new IllegalArgumentException(
"Persistent records can have at most one AUTO/IDENTITY generator.");
}
checkArgument(!seenIdentityGenerator, "Persistent records can have at " +
"most one AUTO/IDENTITY generator.");
_valueGenerators.put(field.getName(), new IdentityValueGenerator(gv, this, fm));
seenIdentityGenerator = true;
break;
@@ -191,10 +188,7 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
case TABLE:
String name = gv.generator();
generator = context.tableGenerators.get(name);
if (generator == null) {
throw new IllegalArgumentException(
"Unknown generator [generator=" + name + "]");
}
checkArgument(generator != null, "Unknown generator [generator=" + name + "]");
_valueGenerators.put(
field.getName(), new TableValueGenerator(generator, gv, this, fm));
break;
@@ -212,12 +206,11 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
Tuple<SQLExpression, Order> entry =
new Tuple<SQLExpression, Order>(fieldColumn, Order.ASC);
if (index.unique()) {
Preconditions.checkArgument(!namedFieldIndices.containsKey(index.name()),
checkArgument(!namedFieldIndices.containsKey(index.name()),
"All @Index for a particular name must be unique or non-unique");
uniqueNamedFieldIndices.put(name, entry);
} else {
Preconditions.checkArgument(
!uniqueNamedFieldIndices.containsKey(index.name()),
checkArgument(!uniqueNamedFieldIndices.containsKey(index.name()),
"All @Index for a particular name must be unique or non-unique");
namedFieldIndices.put(name, entry);
}
@@ -258,10 +251,7 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
int ii = 0;
for (String field : constraint.fields()) {
FieldMarshaller<?> fm = _fields.get(field);
if (fm == null) {
throw new IllegalArgumentException(
"Unknown unique constraint field: " + field);
}
checkArgument(fm != null, "Unknown unique constraint field: " + field);
colExps[ii ++] = new ColumnExp<Object>(_pClass, field);
}
_indexes.add(buildIndex(constraint.name(), true, colExps));
@@ -338,10 +328,8 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
public FullTextIndex getFullTextIndex (String name)
{
FullTextIndex fti = _fullTextIndexes.get(name);
if (fti == null) {
throw new IllegalStateException("Persistent class missing full text index " +
checkArgument(fti != null, "Persistent class missing full text index " +
"[class=" + _pClass + ", index=" + name + "]");
}
return fti;
}
@@ -486,10 +474,9 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
Comparable<?>[] values = new Comparable<?>[_pkColumns.size()];
for (int ii = 0; ii < _pkColumns.size(); ii++) {
Object keyValue = _pkColumns.get(ii).getFromSet(rs);
if (!(keyValue instanceof Comparable<?>)) {
throw new IllegalArgumentException("Key field must be Comparable<?> [field=" +
_pkColumns.get(ii).getColumnName() + "]");
}
checkArgument((keyValue instanceof Comparable<?>),
"Key field must be Comparable<?> [field=%s]",
_pkColumns.get(ii).getColumnName());
values[ii] = (Comparable<?>) keyValue;
}
return new Key<T>(_pClass, values);
@@ -999,10 +986,8 @@ public class DepotMarshaller<T extends PersistentRecord> implements QueryMarshal
String[] columns = new String[fields.length];
for (int ii = 0; ii < columns.length; ii ++) {
FieldMarshaller<?> fm = _fields.get(fields[ii].name);
if (fm == null) {
throw new IllegalArgumentException(
"Unknown field on record [field=" + fields[ii] + ", class=" + _pClass + "]");
}
checkArgument(fm != null, "Unknown field on record [field=%s, class=%s]",
fields[ii], _pClass);
columns[ii] = fm.getColumnName();
}
return columns;
@@ -35,6 +35,8 @@ import com.samskivert.depot.PersistentRecord;
import com.samskivert.depot.clause.QueryClause;
import com.samskivert.depot.expression.SQLExpression;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Maintains a record of the persistent classes brought into the context of the associated SQL,
* i.e. any class associated with a concrete table that would appear in FROM or JOIN clauses or as
@@ -121,9 +123,7 @@ public class DepotTypes
{
if (_useTableAbbreviations) {
Integer ix = _classIx.get(cl);
if (ix == null) {
throw new IllegalArgumentException("Unknown persistence class: " + cl);
}
checkArgument(ix != null, "Unknown persistence class: " + cl);
return "T" + (ix+1);
}
return getTableName(cl);
@@ -140,10 +140,7 @@ public class DepotTypes
public String getColumnName (Class<? extends PersistentRecord> cl, String field)
{
FieldMarshaller<?> fm = getMarshaller(cl).getFieldMarshaller(field);
if (fm == null) {
throw new IllegalArgumentException(
"Field not known on class [field=" + field + ", class=" + cl + "]");
}
checkArgument(fm != null, "Field not known on class [field=%s, class=%s]", field, cl);
return fm.getColumnName();
}
@@ -156,9 +153,7 @@ public class DepotTypes
public DepotMarshaller<?> getMarshaller (Class<? extends PersistentRecord> cl)
{
DepotMarshaller<?> marsh = _classMap.get(cl);
if (marsh == null) {
throw new IllegalArgumentException("Persistent class not known: " + cl);
}
checkArgument(marsh != null, "Persistent class not known: " + cl);
return marsh;
}
@@ -47,6 +47,8 @@ import com.samskivert.util.ByteEnumUtil;
import com.samskivert.util.Logger;
import com.samskivert.util.StringUtil;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Handles the marshalling and unmarshalling of a particular field of a persistent object.
*
@@ -91,9 +93,7 @@ public abstract class FieldMarshaller<T>
// finally look for an @Transform annotation on the field type (expensive)
xform = findTransformAnnotation(field.getType());
if (xform == null) {
throw new IllegalArgumentException("Cannot marshall " + field + ".");
}
checkArgument(xform != null, "Cannot marshall " + field + ".");
}
try {
@@ -116,12 +116,9 @@ public abstract class FieldMarshaller<T>
final Transformer<F,T> xformer, Field field, Transform annotation)
{
Class<?> pojoType = getTransformerType(xformer, "from");
if (!pojoType.isAssignableFrom(field.getType())) {
throw new IllegalArgumentException(
"@Transform error on " + field.getType().getName() + "." +
field.getName() + ": " + xformer.getClass().getName() + " cannot convert " +
field.getType().getName());
}
checkArgument(pojoType.isAssignableFrom(field.getType()),
"@Transform error on %s.%s: %s cannot convert %s", field.getType().getName(),
field.getName(), xformer.getClass().getName(), field.getType().getName());
xformer.init(field.getGenericType(), annotation);
@SuppressWarnings("unchecked") final FieldMarshaller<T> delegate =
@@ -455,11 +452,8 @@ public abstract class FieldMarshaller<T>
}
}
}
if (ttype == null) {
throw new IllegalArgumentException(
Logger.format("Transformer lacks " + which + "Persistent() method!?",
"xclass", xformer.getClass()));
}
checkArgument(ttype != null, "Transformer lacks %sPersistent() method!? [xclass=%s]",
which, xformer.getClass());
return ttype;
}
@@ -745,11 +739,9 @@ public abstract class FieldMarshaller<T>
@Override public void create (Field field) {
super.create(field);
// do some sanity checking so that the unsafe business we do below is safer
if (!Enum.class.isAssignableFrom(_eclass)) {
throw new IllegalArgumentException(
checkArgument(Enum.class.isAssignableFrom(_eclass),
"ByteEnum not implemented by real Enum: " + field);
}
}
@Override public String getColumnType (ColumnTyper typer, int length) {
return typer.getByteType(length);
@@ -51,6 +51,7 @@ import com.samskivert.depot.expression.ColumnExp;
import com.samskivert.depot.impl.operator.In;
import com.samskivert.jdbc.DatabaseLiaison;
import static com.google.common.base.Preconditions.checkArgument;
import static com.samskivert.depot.Log.log;
/**
@@ -86,16 +87,12 @@ public abstract class FindAllQuery<T extends PersistentRecord,R>
{
super(ctx, type);
if (_dmarsh.getComputed() != null) {
throw new IllegalArgumentException(
checkArgument(_dmarsh.getComputed() == null,
"This algorithm doesn't work on @Computed records.");
}
for (QueryClause clause : clauses) {
if (clause instanceof FieldOverride) {
throw new IllegalArgumentException(
checkArgument(!(clause instanceof FieldOverride),
"This algorithm doesn't work with FieldOverrides.");
}
}
_select = new SelectClause(_type, _dmarsh.getPrimaryKeyFields(), clauses);
switch(strategy) {
@@ -289,8 +289,7 @@ public class PostgreSQLBuilder
case English:
return "pg_catalog.english";
default:
throw new IllegalArgumentException(
"Unknown full text configuration: " + configuration);
throw new IllegalArgumentException("Unknown full text configuration: " + configuration);
}
}
@@ -36,6 +36,8 @@ import com.samskivert.depot.clause.QueryClause;
import com.samskivert.jdbc.ColumnDefinition;
import com.samskivert.util.ByteEnum;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
import static com.samskivert.depot.Log.log;
/**
@@ -74,9 +76,7 @@ public abstract class SQLBuilder
public PreparedStatement prepare (Connection conn)
throws SQLException
{
if (_buildVisitor == null) {
throw new IllegalArgumentException("Cannot prepare query until it's been built.");
}
checkState(_buildVisitor != null, "Cannot prepare query until it's been built.");
PreparedStatement stmt = conn.prepareStatement(_buildVisitor.getQuery());
@@ -136,10 +136,8 @@ public abstract class SQLBuilder
}
// sanity check nullability
if (coldef.nullable && field.getType().isPrimitive()) {
throw new IllegalArgumentException(
checkArgument(!coldef.nullable || !field.getType().isPrimitive(),
"Primitive Java type cannot be nullable [field=" + field.getName() + "]");
}
return coldef;
}
@@ -35,6 +35,8 @@ import com.google.common.collect.Maps;
import com.samskivert.depot.PersistentRecord;
import com.samskivert.util.Tuple;
import static com.google.common.base.Preconditions.checkArgument;
/**
* Creates functions that map persistent records to runtime counterparts and vice versa. A few bits
* of magic are provided to help in mapping from the persistent world to the runtime work. Namely:
@@ -176,12 +178,9 @@ public class RuntimeUtil
}
// if we have no persistent field for the runtime field, we're now out of luck
if (pfield == null) {
throw new IllegalArgumentException(
"Cannot create mapping for " + rfield + ". Neither " +
pclass.getSimpleName() + "." + rfield.getName() + " nor " +
rfield.getType() + " " + getter + "() exist.");
}
checkArgument(pfield != null,
"Cannot create mapping for %s. Neither %s.%s nor %s %s() exist.",
rfield, pclass.getSimpleName(), rfield.getName(), rfield.getType(), getter);
// if the fields match exactly, return a getter that just gets the field
if (rfield.getType().equals(pfield.getType())) {
@@ -222,10 +221,8 @@ public class RuntimeUtil
}
// if we have no persistent field for this runtime field, we're now out of luck
if (pfield == null) {
throw new IllegalArgumentException("Cannot create setter for " + rfield + ". " +
checkArgument(pfield != null, "Cannot create setter for " + rfield + ". " +
"No corresponding field exists in " + pclass + ".");
}
// if the fields match exactly, return a setter that just sets the field
if (rfield.getType().equals(pfield.getType())) {