summaryrefslogtreecommitdiffstats
path: root/sched
diff options
context:
space:
mode:
authorYohann Roussel <yroussel@google.com>2015-03-19 10:34:09 +0100
committerYohann Roussel <yroussel@google.com>2015-03-19 11:25:38 +0000
commit1b82947155738507cdebb03827a535d554f29cbd (patch)
tree1ae0ea51b022b8f8185fd16a97088187f782248d /sched
parent44fcd8ac9cc721bc8f07300d7f2e8d741644d451 (diff)
downloadtoolchain_jack-1b82947155738507cdebb03827a535d554f29cbd.zip
toolchain_jack-1b82947155738507cdebb03827a535d554f29cbd.tar.gz
toolchain_jack-1b82947155738507cdebb03827a535d554f29cbd.tar.bz2
Make PairCodec use a Pair instead of a Map.Entry
Change-Id: Iaf77aa13a0f62e6dbf607e80f6a451d20aa94391
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/PairCodec.java64
-rw-r--r--sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java29
-rw-r--r--sched/src/com/android/sched/util/config/id/MessageDigestPropertyId.java12
4 files changed, 81 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 60cbd83..a95780b 100644
--- a/sched/src/com/android/sched/util/codec/ConvertCodec.java
+++ b/sched/src/com/android/sched/util/codec/ConvertCodec.java
@@ -38,6 +38,8 @@ public abstract class ConvertCodec<SRC, DST> implements StringCodec<DST> {
@Nonnull
protected abstract DST convert(@Nonnull SRC src) throws ParsingException;
+ @Nonnull
+ protected abstract SRC revert(@Nonnull DST dst);
@Override
@Nonnull
@@ -64,6 +66,18 @@ public abstract class ConvertCodec<SRC, DST> implements StringCodec<DST> {
@Override
@Nonnull
+ public String formatValue(@Nonnull DST data) {
+ return codec.formatValue(revert(data));
+ }
+
+ @Override
+ public void checkValue(@Nonnull CodecContext context, @Nonnull DST data)
+ throws CheckingException {
+ codec.checkValue(context, revert(data));
+ }
+
+ @Override
+ @Nonnull
public String getUsage() {
return codec.getUsage();
}
diff --git a/sched/src/com/android/sched/util/codec/PairCodec.java b/sched/src/com/android/sched/util/codec/PairCodec.java
index 11e3f6a..063ca14 100644
--- a/sched/src/com/android/sched/util/codec/PairCodec.java
+++ b/sched/src/com/android/sched/util/codec/PairCodec.java
@@ -16,10 +16,10 @@
package com.android.sched.util.codec;
+import com.android.sched.util.codec.PairCodec.Pair;
import com.android.sched.util.config.ChainedException.ChainedExceptionBuilder;
import com.android.sched.util.config.ConfigurationError;
-import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map.Entry;
@@ -30,8 +30,46 @@ import javax.annotation.Nonnull;
/**
* This {@link StringCodec} is used to create an instance of {@link Entry}
*/
-// STOPSHIP see https://android-review.googlesource.com/#/c/120553/17
-public class PairCodec<T, U> implements StringCodec<Entry<T, U>>{
+public class PairCodec<T, U> implements StringCodec<Pair<T, U>>{
+
+ /**
+ * A pair.
+ */
+ public static class Pair<T, U> {
+ @Nonnull
+ private final T first;
+ @Nonnull
+ private final U second;
+
+ public Pair(@Nonnull T first, @Nonnull U second) {
+ this.first = first;
+ this.second = second;
+ }
+
+ @Nonnull
+ public T getFirst() {
+ return first;
+ }
+
+ @Nonnull
+ public U getSecond() {
+ return second;
+ }
+
+ @Override
+ public final boolean equals(Object obj) {
+ if (obj instanceof Pair) {
+ return first.equals(((Pair<?, ?>) obj).first) && second.equals(((Pair<?, ?>) obj).second);
+ }
+ return super.equals(obj);
+ }
+
+ @Override
+ public final int hashCode() {
+ return first.hashCode() ^ second.hashCode();
+ }
+ }
+
@Nonnull
private final StringCodec<T> keyParser;
@Nonnull
@@ -59,20 +97,20 @@ public class PairCodec<T, U> implements StringCodec<Entry<T, U>>{
@Override
@Nonnull
- public Entry<T, U> parseString(@Nonnull CodecContext context, @Nonnull String string) {
+ public Pair<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),
+ return new Pair<T, U>(keyParser.parseString(context, key),
valueParser.parseString(context, valueString));
}
@Override
@CheckForNull
- public Entry<T, U> checkString(@Nonnull CodecContext context,
+ public Pair<T, U> checkString(@Nonnull CodecContext context,
@Nonnull String string)
throws ParsingException {
ChainedExceptionBuilder<ParsingException> exceptions =
@@ -117,7 +155,7 @@ public class PairCodec<T, U> implements StringCodec<Entry<T, U>>{
valueElement = valueParser.parseString(context, valueString);
}
- return new AbstractMap.SimpleEntry<T, U>(keyElement, valueElement);
+ return new Pair<T, U>(keyElement, valueElement);
}
}
@@ -161,27 +199,27 @@ public class PairCodec<T, U> implements StringCodec<Entry<T, U>>{
@Override
@Nonnull
- public String formatValue(@Nonnull Entry<T, U> data) {
+ public String formatValue(@Nonnull Pair<T, U> data) {
StringBuilder sb = new StringBuilder();
- sb.append(keyParser.formatValue(data.getKey()));
+ sb.append(keyParser.formatValue(data.getFirst()));
sb.append(separator);
- sb.append(valueParser.formatValue(data.getValue()));
+ sb.append(valueParser.formatValue(data.getSecond()));
return sb.toString();
}
@Override
- public void checkValue(@Nonnull CodecContext context, @Nonnull Entry<T, U> data)
+ public void checkValue(@Nonnull CodecContext context, @Nonnull Pair<T, U> data)
throws CheckingException {
ChainedExceptionBuilder<CheckingException> exceptions =
new ChainedExceptionBuilder<CheckingException>();
try {
- keyParser.checkValue(context, data.getKey());
+ keyParser.checkValue(context, data.getFirst());
} catch (CheckingException e) {
exceptions.appendException(e);
}
try {
- valueParser.checkValue(context, data.getValue());
+ valueParser.checkValue(context, data.getSecond());
} catch (CheckingException e) {
exceptions.appendException(e);
}
diff --git a/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java b/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java
index b4867f0..0570468 100644
--- a/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java
+++ b/sched/src/com/android/sched/util/codec/PairListToMapCodecConverter.java
@@ -16,6 +16,7 @@
package com.android.sched.util.codec;
+import com.android.sched.util.codec.PairCodec.Pair;
import com.android.sched.util.config.ChainedException.ChainedExceptionBuilder;
import java.util.ArrayList;
@@ -30,37 +31,35 @@ import javax.annotation.Nonnull;
* A {@link ConvertCodec} allowing to convert list of {@link Entry} into a map.
*/
public class PairListToMapCodecConverter<T, U> extends
- ConvertCodec<List<Entry<T, U>>, Map<T, U>> {
- public PairListToMapCodecConverter(StringCodec<List<Entry<T, U>>> codec) {
+ ConvertCodec<List<Pair<T, U>>, Map<T, U>> {
+ public PairListToMapCodecConverter(StringCodec<List<Pair<T, U>>> codec) {
super(codec);
}
@Override
@Nonnull
- public String formatValue(@Nonnull Map<T, U> data) {
- return codec.formatValue(new ArrayList<Map.Entry<T, U>>(data.entrySet()));
- }
-
- @Override
- public void checkValue(@Nonnull CodecContext context, @Nonnull Map<T, U> data)
- throws CheckingException {
- codec.checkValue(context, new ArrayList<Map.Entry<T, U>>(data.entrySet()));
+ protected List<Pair<T, U>> revert(@Nonnull Map<T, U> data) {
+ List<Pair<T, U>> list = new ArrayList<PairCodec.Pair<T, U>>(data.size());
+ for (Map.Entry<T, U> entry : data.entrySet()) {
+ list.add(new Pair<T, U>(entry.getKey(), entry.getValue()));
+ }
+ return list;
}
@Override
@Nonnull
- protected Map<T, U> convert(@Nonnull List<Entry<T, U>> list) throws ParsingException {
+ protected Map<T, U> convert(@Nonnull List<Pair<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) {
- if (!map.containsKey(entry.getKey())) {
- map.put(entry.getKey(), entry.getValue());
+ for (Pair<T, U> pair : list) {
+ if (!map.containsKey(pair.getFirst())) {
+ map.put(pair.getFirst(), pair.getSecond());
} else {
exceptions.appendException(new ListParsingException(index, "Value for '"
- + entry.getKey() + "' is already defined"));
+ + pair.getFirst() + "' is already defined"));
}
index++;
}
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 f6e2875..2d569f1 100644
--- a/sched/src/com/android/sched/util/config/id/MessageDigestPropertyId.java
+++ b/sched/src/com/android/sched/util/config/id/MessageDigestPropertyId.java
@@ -17,8 +17,6 @@
package com.android.sched.util.config.id;
-import com.android.sched.util.codec.CheckingException;
-import com.android.sched.util.codec.CodecContext;
import com.android.sched.util.codec.ConvertCodec;
import com.android.sched.util.codec.KeyValueCodec;
import com.android.sched.util.codec.MessageDigestCodec;
@@ -48,14 +46,8 @@ public class MessageDigestPropertyId extends PropertyId<MessageDigestFactory> {
new MessageDigestCodec()) {
@Override
@Nonnull
- public String formatValue(@Nonnull MessageDigestFactory data) {
- return codec.formatValue(data.getService());
- }
-
- @Override
- public void checkValue(@Nonnull CodecContext context, @Nonnull MessageDigestFactory data)
- throws CheckingException {
- codec.checkValue(context, data.getService());
+ protected Service revert(@Nonnull MessageDigestFactory dst) {
+ return dst.getService();
}
@Override