Chromium Code Reviews| Index: pkg/analysis_server/lib/src/protocol.dart |
| diff --git a/pkg/analysis_server/lib/src/protocol.dart b/pkg/analysis_server/lib/src/protocol.dart |
| index 75b29c7a60be4825563fd3b48fe0d764e371049a..bd2029f9cec4f1a5f7df036919668c2dfd7d915e 100644 |
| --- a/pkg/analysis_server/lib/src/protocol.dart |
| +++ b/pkg/analysis_server/lib/src/protocol.dart |
| @@ -6,6 +6,35 @@ library protocol; |
| import 'dart:convert' show JsonDecoder; |
| +/// An abstract enumeration. |
| +abstract class Enum2<E extends Enum2> implements Comparable<E> { |
| + /// The name of this enum constant, as declared in the enum declaration. |
| + final String name; |
| + |
| + /// The position in the enum declaration. |
| + final int ordinal; |
| + |
| + const Enum2(this.name, this.ordinal); |
| + |
| + int get hashCode => ordinal; |
|
Brian Wilkerson
2014/05/24 15:49:11
I think we want to use @override annotations where
scheglov
2014/05/24 16:07:26
Done here and for toString().
|
| + |
| + String toString() => name; |
| + |
| + int compareTo(E other) => ordinal - other.ordinal; |
| + |
| + /// Returns the enum constant with the given [name], `null` if not found. |
| + static Enum2 valueOf(List<Enum2> values, String name) { |
| + for (int i = 0; i < values.length; i++) { |
| + var value = values[i]; |
|
Brian Wilkerson
2014/05/24 15:49:11
And I should have mentioned that there are lots of
scheglov
2014/05/24 16:07:26
Got it.
Done.
|
| + if (value.name == name) { |
| + return value; |
| + } |
| + } |
| + return null; |
| + } |
| +} |
| + |
| + |
| /** |
| * Instances of the class [Request] represent a request that was received. |
| */ |
| @@ -283,6 +312,22 @@ class RequestDatum { |
| } |
| /** |
| + * Validate that the datum is a list of strings, and convert it into [Enum]s. |
| + */ |
| + Set<Enum2> asEnumSet(List<Enum2> allValues) { |
| + var values = new Set(); |
| + for (var name in asStringList()) { |
| + var value = Enum2.valueOf(allValues, name); |
| + if (value == null) { |
| + throw new RequestFailure(new Response.invalidParameter(request, path, |
| + "be a list of names from the list $allValues")); |
| + } |
| + values.add(value); |
| + } |
| + return values; |
| + } |
| + |
| + /** |
| * Determine if the datum is a map whose values are all strings. |
| * |
| * Note: we can safely assume that the keys are all strings, since JSON maps |