summaryrefslogtreecommitdiffstats
path: root/sched
diff options
context:
space:
mode:
authorYohann Roussel <yroussel@google.com>2015-03-17 16:12:00 +0100
committerYohann Roussel <yroussel@google.com>2015-03-18 18:54:32 +0100
commit373888481b82bbebd24a2ffaeec41613d8256b24 (patch)
treeb3c70798cadbf909b4b674ca1af729f4a7c92546 /sched
parent406fe10656443b6b08684153c51b99024007e581 (diff)
downloadtoolchain_jack-373888481b82bbebd24a2ffaeec41613d8256b24.zip
toolchain_jack-373888481b82bbebd24a2ffaeec41613d8256b24.tar.gz
toolchain_jack-373888481b82bbebd24a2ffaeec41613d8256b24.tar.bz2
Allow ConvertCodec to check convertion
And use this capability to check double defintions of keys in PairListToMapCodecConverter. Change-Id: Ia7052c8d33710e5abc43e753c7f1c91e869a7185
Diffstat (limited to 'sched')
-rw-r--r--sched/src/com/android/sched/util/codec/ConvertCodec.java14
-rw-r--r--sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java16
-rw-r--r--sched/src/com/android/sched/util/codec/ParsingException.java13
3 files changed, 37 insertions, 6 deletions
diff --git a/sched/src/com/android/sched/util/codec/ConvertCodec.java b/sched/src/com/android/sched/util/codec/ConvertCodec.java
index e32a293..60cbd83 100644
--- a/sched/src/com/android/sched/util/codec/ConvertCodec.java
+++ b/sched/src/com/android/sched/util/codec/ConvertCodec.java
@@ -16,14 +16,16 @@
package com.android.sched.util.codec;
+import com.android.sched.util.config.ConfigurationError;
+
import java.util.List;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
- * This {@link StringCodec} is used to convert a {@link StringCodec<SRC>} to a {@link StringCodec
- * <DST>}.
+ * This {@link StringCodec} is used to convert a {@code StringCodec<SRC>} to a
+ * {@code StringCodec<DST>}.
*/
public abstract class ConvertCodec<SRC, DST> implements StringCodec<DST> {
@Nonnull
@@ -34,13 +36,17 @@ public abstract class ConvertCodec<SRC, DST> implements StringCodec<DST> {
}
@Nonnull
- protected abstract DST convert(@Nonnull SRC src);
+ protected abstract DST convert(@Nonnull SRC src) throws ParsingException;
@Override
@Nonnull
public DST parseString(@Nonnull CodecContext context, @Nonnull String string) {
- return convert(codec.parseString(context, string));
+ try {
+ return convert(codec.parseString(context, string));
+ } catch (ParsingException e) {
+ throw new ConfigurationError(e);
+ }
}
@Override
diff --git a/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java b/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java
index daa3a4a..b4867f0 100644
--- a/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java
+++ b/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java
@@ -16,6 +16,8 @@
package com.android.sched.util.codec;
+import com.android.sched.util.config.ChainedException.ChainedExceptionBuilder;
+
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -47,12 +49,22 @@ public class PairListToMapCodecConverter<T, U> extends
@Override
@Nonnull
- protected Map<T, U> convert(@Nonnull List<Entry<T, U>> list) {
+ protected Map<T, U> convert(@Nonnull List<Entry<T, U>> list) throws ParsingException {
+ ChainedExceptionBuilder<ParsingException> exceptions =
+ new ChainedExceptionBuilder<ParsingException>();
Map<T, U> map = new HashMap<T, U>(Math.round(list.size() / 0.75f), 0.75f);
+ int index = 1;
for (Entry<T, U> entry : list) {
- map.put(entry.getKey(), entry.getValue());
+ if (!map.containsKey(entry.getKey())) {
+ map.put(entry.getKey(), entry.getValue());
+ } else {
+ exceptions.appendException(new ListParsingException(index, "Value for '"
+ + entry.getKey() + "' is already defined"));
+ }
+ index++;
}
+ exceptions.throwIfNecessary();
return map;
}
diff --git a/sched/src/com/android/sched/util/codec/ParsingException.java b/sched/src/com/android/sched/util/codec/ParsingException.java
index 759f978..78b1962 100644
--- a/sched/src/com/android/sched/util/codec/ParsingException.java
+++ b/sched/src/com/android/sched/util/codec/ParsingException.java
@@ -18,6 +18,8 @@ package com.android.sched.util.codec;
import com.android.sched.util.config.ChainedException;
+import java.util.Iterator;
+
import javax.annotation.Nonnull;
@@ -39,4 +41,15 @@ public class ParsingException extends ChainedException {
public ParsingException(@Nonnull String message, @Nonnull Throwable cause) {
super(message, cause);
}
+
+ public ParsingException(@Nonnull ChainedException causes) {
+ this((Throwable) causes);
+
+ Iterator<ChainedException> iter = causes.iterator();
+ iter.next();
+ while (iter.hasNext()) {
+ new ParsingException(iter.next()).putAsLastExceptionOf(this);
+ }
+ }
+
}