OPWNJPA-1191

Adding messages exceptions thrown by TupleImpl

git-svn-id: https://svn.apache.org/repos/asf/openjpa/trunk@796854 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Dick 2009-07-22 19:36:59 +00:00
parent c6e394d936
commit fc5d50bf18
3 changed files with 36 additions and 18 deletions

View File

@ -25,7 +25,11 @@ import java.util.List;
import javax.persistence.Tuple; import javax.persistence.Tuple;
import javax.persistence.TupleElement; import javax.persistence.TupleElement;
import org.apache.openjpa.kernel.ExpressionStoreQuery;
import org.apache.openjpa.lib.util.Localizer;
public class TupleImpl implements Tuple { public class TupleImpl implements Tuple {
private static final Localizer _loc = Localizer.forPackage(TupleImpl.class);
List<TupleElement<?>> elements = new ArrayList<TupleElement<?>>(); List<TupleElement<?>> elements = new ArrayList<TupleElement<?>>();
/** /**
@ -39,9 +43,10 @@ public class TupleImpl implements Tuple {
*/ */
public <X> X get(TupleElement<X> tupleElement) { public <X> X get(TupleElement<X> tupleElement) {
if (!elements.contains(tupleElement)) { if (!elements.contains(tupleElement)) {
throw new IllegalArgumentException("tupleElement was not found in this tuple"); // TODO MDD improve throw new IllegalArgumentException(_loc.get(
"tuple-element-not-found",
new Object[] { tupleElement, elements }).getMessage());
} }
TupleElementImpl<X> impl = (TupleElementImpl<X>) tupleElement; TupleElementImpl<X> impl = (TupleElementImpl<X>) tupleElement;
return impl.getValue(); return impl.getValue();
} }
@ -61,13 +66,13 @@ public class TupleImpl implements Tuple {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <X> X get(String alias, Class<X> type) { public <X> X get(String alias, Class<X> type) {
if (type == null) { if (type == null) {
throw new IllegalArgumentException("Type was null"); throw new IllegalArgumentException(_loc.get("tuple-was-null", "type").getMessage());
} }
Object rval = get(alias); Object rval = get(alias);
if (!type.isAssignableFrom(rval.getClass())) { if (!type.isAssignableFrom(rval.getClass())) {
throw new IllegalArgumentException(String.format( throw new IllegalArgumentException(_loc.get(
"TupleElement type did not match for alias: %s. Provided type: %s actual type: %s", alias, type, rval "tuple-element-wrong-type",
.getClass().toString())); new Object[] { alias, type, rval.getClass() }).getMessage());
} }
return (X) rval; return (X) rval;
} }
@ -83,14 +88,21 @@ public class TupleImpl implements Tuple {
*/ */
public Object get(String alias) { public Object get(String alias) {
if (alias == null) { if (alias == null) {
throw new IllegalArgumentException(String.format("Alias was null.")); // TODO MDD determine if we can support this.
throw new IllegalArgumentException(_loc.get("typle-was-null", "alias").getMessage());
} }
for (TupleElement<?> te : elements) { for (TupleElement<?> te : elements) {
if (alias.equals(te.getAlias())) { if (alias.equals(te.getAlias())) {
return ((TupleElementImpl<?>) te).getValue(); return ((TupleElementImpl<?>) te).getValue();
} }
} }
throw new IllegalArgumentException(String.format("Alias %s was not found.", alias));
List<String> knownAliases = new ArrayList<String>();
for(TupleElement<?> te : elements) {
knownAliases.add(te.getAlias());
}
throw new IllegalArgumentException(_loc.get("tuple-alias-not-found",
new Object[] { alias, knownAliases }).getMessage());
} }
/** /**
@ -107,13 +119,13 @@ public class TupleImpl implements Tuple {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <X> X get(int i, Class<X> type) { public <X> X get(int i, Class<X> type) {
if (type == null) { if (type == null) {
throw new IllegalArgumentException("Type was null"); throw new IllegalArgumentException(_loc.get("tuple-was-null", "type").getMessage());
} }
Object rval = get(i); Object rval = get(i);
if(! type.isAssignableFrom(rval.getClass())) { if(! type.isAssignableFrom(rval.getClass())) {
throw new IllegalArgumentException(String.format( throw new IllegalArgumentException(_loc.get(
"Type did not match for position: %d. Provided type: %s actual type: %s", i, type.getClass(), "tuple-element-wrong-type",
rval.getClass())); new Object[] { "position", i, type, type.getClass() }).getMessage());
} }
return (X) rval; return (X) rval;
} }
@ -129,11 +141,11 @@ public class TupleImpl implements Tuple {
*/ */
public Object get(int i) { public Object get(int i) {
if (i > elements.size()) { if (i > elements.size()) {
throw new IllegalArgumentException(String.format( throw new IllegalArgumentException(_loc.get("tuple-exceeded-size",
"Attempt to read TupleElement %d when there are only %d elements available", i, elements.size())); new Object[] { i, elements.size() }).getMessage());
} }
if (i == -1) { if (i <= -1) {
throw new IllegalArgumentException("Cannot obtain the -1th element in this tuple. Thank you for playing"); throw new IllegalArgumentException(_loc.get("tuple-stop-thinking-in-python").getMessage());
} }
return toArray()[i]; return toArray()[i];
} }

View File

@ -198,4 +198,9 @@ dynamic-agent: OpenJPA dynamically loaded the class enhancer. Any classes \
loaded by the JVM. loaded by the JVM.
vlem-creation-info: OpenJPA dynamically loaded a validation provider. vlem-creation-info: OpenJPA dynamically loaded a validation provider.
no-embeddable-metadata: Unable to load metadata for embeddable class "{0}". no-embeddable-metadata: Unable to load metadata for embeddable class "{0}".
tuple-element-not-found: TupleElement "{0}" "{1}" was not found in this Tuple. Contents of the Tuple: {2}.
tuple-was-null: Input argument {0} was null. Unable to proceed.
tuple-element-wrong-type: TupleElement type did not match for {0} "{1}". Provided type "{2}" , actual type "{3}"
tuple-alias-not-found: Alias "{0}" was not found in this tuple. Aliases found : "{1}"
tuple-exceeded-size : Attempt to read TupleElement {0} when there are only {1} elements available
tuple-stop-thinking-in-python: Currently we do not support negative indexes into a Tuple.

View File

@ -21,10 +21,11 @@ package org.apache.openjpa.persistence;
import java.util.List; import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Tuple; import javax.persistence.Tuple;
import javax.persistence.TupleElement; import javax.persistence.TupleElement;
import org.apache.openjpa.lib.util.Localizer;
import junit.framework.TestCase; import junit.framework.TestCase;
/** /**