diff options
author | Jean-Philippe Lesot <jplesot@google.com> | 2015-03-13 11:48:07 +0100 |
---|---|---|
committer | Jean-Philippe Lesot <jplesot@google.com> | 2015-03-13 12:05:32 +0100 |
commit | d06c510190b05c95dfeff1e0483e103eaff7a1eb (patch) | |
tree | 3eb07237ebe89ae1f922a900d7c0043ee221c37c /sched/src/com/android/sched | |
parent | be49dc7d7d939391a5646a174df8eebe77b31fef (diff) | |
download | toolchain_jack-d06c510190b05c95dfeff1e0483e103eaff7a1eb.tar.gz toolchain_jack-d06c510190b05c95dfeff1e0483e103eaff7a1eb.tar.bz2 toolchain_jack-d06c510190b05c95dfeff1e0483e103eaff7a1eb.zip |
Fix PairCodec and ConvertCodec
If a StringCodec return an object from checkString, we can not ignore it. It
was not the way PairCodec and ConvertCodec work.
Change-Id: Ic61f5d8fbd30fc7a0e22949f3a324dae3fd710ed
Diffstat (limited to 'sched/src/com/android/sched')
4 files changed, 60 insertions, 38 deletions
diff --git a/sched/src/com/android/sched/util/codec/ConvertCodec.java b/sched/src/com/android/sched/util/codec/ConvertCodec.java index 5139d906..d97ce50a 100644 --- a/sched/src/com/android/sched/util/codec/ConvertCodec.java +++ b/sched/src/com/android/sched/util/codec/ConvertCodec.java @@ -33,13 +33,27 @@ public abstract class ConvertCodec<SRC, DST> implements StringCodec<DST> { this.codec = codec; } + @Nonnull + protected abstract DST convert(@Nonnull SRC src); + + + @Override + @Nonnull + public DST parseString(@Nonnull CodecContext context, @Nonnull String string) { + return convert(codec.parseString(context, string)); + } + @Override @CheckForNull public DST checkString(@Nonnull CodecContext context, @Nonnull String string) throws ParsingException { - codec.checkString(context, string); + SRC src = codec.checkString(context, string); - return null; + if (src == null) { + return null; + } else { + return convert(src); + } } @Override diff --git a/sched/src/com/android/sched/util/codec/PairCodec.java b/sched/src/com/android/sched/util/codec/PairCodec.java index 4032bf89..14a793ea 100644 --- a/sched/src/com/android/sched/util/codec/PairCodec.java +++ b/sched/src/com/android/sched/util/codec/PairCodec.java @@ -59,17 +59,15 @@ public class PairCodec<T, U> implements StringCodec<Entry<T, U>>{ @Override @Nonnull - public Entry<T, U> parseString(@Nonnull CodecContext context, - @Nonnull String string) { - int endKey = string.indexOf(separator); - if (endKey == -1) { - throw new ConfigurationError("Missing '" - + separator + "' in '" + string + "'"); - } - String key = string.substring(0, endKey); - String valueString = string.substring(endKey + separator.length()); - return new AbstractMap.SimpleEntry<T, U>(keyParser.parseString(context, key), - valueParser.parseString(context, valueString)); + public Entry<T, U> parseString(@Nonnull CodecContext context, @Nonnull String string) { + int endKey = string.indexOf(separator); + if (endKey == -1) { + throw new ConfigurationError("Missing '" + separator + "' in '" + string + "'"); + } + String key = string.substring(0, endKey); + String valueString = string.substring(endKey + separator.length()); + return new AbstractMap.SimpleEntry<T, U>(keyParser.parseString(context, key), + valueParser.parseString(context, valueString)); } @Override @@ -77,19 +75,20 @@ public class PairCodec<T, U> implements StringCodec<Entry<T, U>>{ public Entry<T, U> checkString(@Nonnull CodecContext context, @Nonnull String string) throws ParsingException { - ChainedExceptionBuilder<ParsingException> exceptions = new ChainedExceptionBuilder<ParsingException>(); T keyElement = null; U valueElement = null; + String key = null; + String valueString = null; int endKey = string.indexOf(separator); if (endKey == -1) { exceptions.appendException(new ParsingException("Missing '" + separator + "' in '" + string + "'")); } else { - String key = string.substring(0, endKey); - String valueString = string.substring(endKey + separator.length()); + key = string.substring(0, endKey); + valueString = string.substring(endKey + separator.length()); try { keyElement = keyParser.checkString(context, key); } catch (ParsingException e) { @@ -103,10 +102,21 @@ public class PairCodec<T, U> implements StringCodec<Entry<T, U>>{ } exceptions.throwIfNecessary(); - // If one element is null, do not compute the pair - if (keyElement == null || valueElement == null) { + // If the two elements are null, do not compute the pair + if (keyElement == null && valueElement == null) { return null; } else { + // Else, complete the pair, and return it + if (keyElement == null) { + assert key != null; + keyElement = keyParser.parseString(context, key); + } + + if (valueElement == null) { + assert valueString != null; + valueElement = valueParser.parseString(context, valueString); + } + return new AbstractMap.SimpleEntry<T, U>(keyElement, valueElement); } } diff --git a/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java b/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java index b9c9e868..daa3a4ae 100644 --- a/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java +++ b/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java @@ -35,19 +35,6 @@ public class PairListToMapCodecConverter<T, U> extends @Override @Nonnull - public Map<T, U> parseString(@Nonnull CodecContext context, - @Nonnull String string) { - List<Entry<T, U>> list = codec.parseString(context, string); - Map<T, U> map = new HashMap<T, U>(Math.round(list.size() / 0.75f), - 0.75f); - for (Entry<T, U> entry : list) { - map.put(entry.getKey(), entry.getValue()); - } - return map; - } - - @Override - @Nonnull public String formatValue(@Nonnull Map<T, U> data) { return codec.formatValue(new ArrayList<Map.Entry<T, U>>(data.entrySet())); } @@ -57,4 +44,16 @@ public class PairListToMapCodecConverter<T, U> extends throws CheckingException { codec.checkValue(context, new ArrayList<Map.Entry<T, U>>(data.entrySet())); } + + @Override + @Nonnull + protected Map<T, U> convert(@Nonnull List<Entry<T, U>> list) { + Map<T, U> map = new HashMap<T, U>(Math.round(list.size() / 0.75f), 0.75f); + + for (Entry<T, U> entry : list) { + map.put(entry.getKey(), entry.getValue()); + } + + return map; + } }
\ No newline at end of file diff --git a/sched/src/com/android/sched/util/config/id/MessageDigestPropertyId.java b/sched/src/com/android/sched/util/config/id/MessageDigestPropertyId.java index f91fc91e..f6e28757 100644 --- a/sched/src/com/android/sched/util/config/id/MessageDigestPropertyId.java +++ b/sched/src/com/android/sched/util/config/id/MessageDigestPropertyId.java @@ -48,13 +48,6 @@ public class MessageDigestPropertyId extends PropertyId<MessageDigestFactory> { new MessageDigestCodec()) { @Override @Nonnull - public MessageDigestFactory parseString(@Nonnull CodecContext context, - @Nonnull String string) { - return new MessageDigestFactory(codec.parseString(context, string)); - } - - @Override - @Nonnull public String formatValue(@Nonnull MessageDigestFactory data) { return codec.formatValue(data.getService()); } @@ -64,6 +57,12 @@ public class MessageDigestPropertyId extends PropertyId<MessageDigestFactory> { throws CheckingException { codec.checkValue(context, data.getService()); } + + @Override + @Nonnull + protected MessageDigestFactory convert(@Nonnull Service service) { + return new MessageDigestFactory(service); + } }); } |