Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library protocol; | 5 library protocol; |
| 6 | 6 |
| 7 import 'dart:convert' show JsonDecoder; | 7 import 'dart:convert' show JsonDecoder; |
| 8 | 8 |
| 9 /// An abstract enumeration. | |
| 10 abstract class Enum2<E extends Enum2> implements Comparable<E> { | |
| 11 /// The name of this enum constant, as declared in the enum declaration. | |
| 12 final String name; | |
| 13 | |
| 14 /// The position in the enum declaration. | |
| 15 final int ordinal; | |
| 16 | |
| 17 const Enum2(this.name, this.ordinal); | |
| 18 | |
| 19 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().
| |
| 20 | |
| 21 String toString() => name; | |
| 22 | |
| 23 int compareTo(E other) => ordinal - other.ordinal; | |
| 24 | |
| 25 /// Returns the enum constant with the given [name], `null` if not found. | |
| 26 static Enum2 valueOf(List<Enum2> values, String name) { | |
| 27 for (int i = 0; i < values.length; i++) { | |
| 28 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.
| |
| 29 if (value.name == name) { | |
| 30 return value; | |
| 31 } | |
| 32 } | |
| 33 return null; | |
| 34 } | |
| 35 } | |
| 36 | |
| 37 | |
| 9 /** | 38 /** |
| 10 * Instances of the class [Request] represent a request that was received. | 39 * Instances of the class [Request] represent a request that was received. |
| 11 */ | 40 */ |
| 12 class Request { | 41 class Request { |
| 13 /** | 42 /** |
| 14 * The name of the JSON attribute containing the id of the request. | 43 * The name of the JSON attribute containing the id of the request. |
| 15 */ | 44 */ |
| 16 static const String ID = 'id'; | 45 static const String ID = 'id'; |
| 17 | 46 |
| 18 /** | 47 /** |
| (...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 276 */ | 305 */ |
| 277 List<String> asStringList() { | 306 List<String> asStringList() { |
| 278 if (!isStringList()) { | 307 if (!isStringList()) { |
| 279 throw new RequestFailure(new Response.invalidParameter(request, path, | 308 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 280 "be a list of strings")); | 309 "be a list of strings")); |
| 281 } | 310 } |
| 282 return datum; | 311 return datum; |
| 283 } | 312 } |
| 284 | 313 |
| 285 /** | 314 /** |
| 315 * Validate that the datum is a list of strings, and convert it into [Enum]s. | |
| 316 */ | |
| 317 Set<Enum2> asEnumSet(List<Enum2> allValues) { | |
| 318 var values = new Set(); | |
| 319 for (var name in asStringList()) { | |
| 320 var value = Enum2.valueOf(allValues, name); | |
| 321 if (value == null) { | |
| 322 throw new RequestFailure(new Response.invalidParameter(request, path, | |
| 323 "be a list of names from the list $allValues")); | |
| 324 } | |
| 325 values.add(value); | |
| 326 } | |
| 327 return values; | |
| 328 } | |
| 329 | |
| 330 /** | |
| 286 * Determine if the datum is a map whose values are all strings. | 331 * Determine if the datum is a map whose values are all strings. |
| 287 * | 332 * |
| 288 * Note: we can safely assume that the keys are all strings, since JSON maps | 333 * Note: we can safely assume that the keys are all strings, since JSON maps |
| 289 * cannot have any other key type. | 334 * cannot have any other key type. |
| 290 */ | 335 */ |
| 291 bool isStringMap() { | 336 bool isStringMap() { |
| 292 if (datum is! Map) { | 337 if (datum is! Map) { |
| 293 return false; | 338 return false; |
| 294 } | 339 } |
| 295 for (var value in datum.values) { | 340 for (var value in datum.values) { |
| (...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 737 /** | 782 /** |
| 738 * The response to be returned as a result of the failure. | 783 * The response to be returned as a result of the failure. |
| 739 */ | 784 */ |
| 740 final Response response; | 785 final Response response; |
| 741 | 786 |
| 742 /** | 787 /** |
| 743 * Initialize a newly created exception to return the given reponse. | 788 * Initialize a newly created exception to return the given reponse. |
| 744 */ | 789 */ |
| 745 RequestFailure(this.response); | 790 RequestFailure(this.response); |
| 746 } | 791 } |
| OLD | NEW |