diff options
author | Benoit Lamarche <benoitlamarche@google.com> | 2015-02-25 18:11:56 +0100 |
---|---|---|
committer | Benoit Lamarche <benoitlamarche@google.com> | 2015-02-25 18:11:56 +0100 |
commit | d528744790b3552a42cfc61d11bbd7dbc8e6a2f2 (patch) | |
tree | 9e922069f11c02f5f51f21278d79b34ef5756217 /sched | |
parent | bd4686c4ee1d09d44e41d2cf699e3109dc974d87 (diff) | |
download | toolchain_jack-d528744790b3552a42cfc61d11bbd7dbc8e6a2f2.tar.gz toolchain_jack-d528744790b3552a42cfc61d11bbd7dbc8e6a2f2.tar.bz2 toolchain_jack-d528744790b3552a42cfc61d11bbd7dbc8e6a2f2.zip |
Add OrCodec
Change-Id: I624f82f169564017d5dbbaf3b47d96715917449c
Diffstat (limited to 'sched')
-rw-r--r-- | sched/src/com/android/sched/util/codec/OrCodec.java | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/sched/src/com/android/sched/util/codec/OrCodec.java b/sched/src/com/android/sched/util/codec/OrCodec.java new file mode 100644 index 00000000..c1789a1b --- /dev/null +++ b/sched/src/com/android/sched/util/codec/OrCodec.java @@ -0,0 +1,104 @@ +/* + * Copyright (C) 2015 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.sched.util.codec; + +import com.android.sched.util.config.ConfigurationError; + +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +import javax.annotation.CheckForNull; +import javax.annotation.Nonnull; + +/** + * This {@link StringCodec} tries successively several {@link StringCodec}s until one is successful. + * + * @param <T> Element type + */ +public class OrCodec<T> implements StringCodec<T> { + @Nonnull + private final List<StringCodec<? extends T>> codecList; + + public OrCodec(@Nonnull List<StringCodec<? extends T>> codecList) { + assert codecList.size() >= 1; + this.codecList = codecList; + } + + @Override + @Nonnull + public T parseString(@Nonnull CodecContext context, @Nonnull String string) { + try { + return checkString(context, string); + } catch (ParsingException e) { + throw new ConfigurationError(e); + } + } + + @Override + @CheckForNull + public T checkString(@Nonnull CodecContext context, @Nonnull String string) + throws ParsingException { + for (StringCodec<? extends T> codec : codecList) { + try { + return codec.checkString(context, string); + } catch (ParsingException e) { + // try next codec + } + } + return codecList.get(0).checkString(context, string); + } + + @Override + public void checkValue(@Nonnull CodecContext context, @Nonnull T data) { + } + + @Override + @Nonnull + public String getUsage() { + StringBuffer usage = new StringBuffer(); + Iterator<StringCodec<? extends T>> codecListIterator = codecList.iterator(); + while (codecListIterator.hasNext()) { + usage.append(codecListIterator.next().getUsage()); + if (codecListIterator.hasNext()) { + usage.append(" or "); + } + } + return usage.toString(); + } + + @Override + @Nonnull + public List<ValueDescription> getValueDescriptions() { + return Collections.<ValueDescription> emptyList(); + } + + @SuppressWarnings("unchecked") + @Override + @Nonnull + public String formatValue(@Nonnull T data) { + //STOPSHIP: rework? + for (StringCodec<? extends T> codec : codecList) { + try { + return ((StringCodec<T>) codec).formatValue(data); + } catch (ClassCastException e) { + // try next codec + } + } + throw new AssertionError(); + } +}
\ No newline at end of file |