| 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 /** | 9 /** |
| 10 * An abstract enumeration. |
| 11 */ |
| 12 abstract class Enum2<E extends Enum2> implements Comparable<E> { |
| 13 /** |
| 14 * The name of this enum constant, as declared in the enum declaration. |
| 15 */ |
| 16 final String name; |
| 17 |
| 18 /** |
| 19 * The position in the enum declaration. |
| 20 */ |
| 21 final int ordinal; |
| 22 |
| 23 const Enum2(this.name, this.ordinal); |
| 24 |
| 25 @override |
| 26 int get hashCode => ordinal; |
| 27 |
| 28 @override |
| 29 String toString() => name; |
| 30 |
| 31 int compareTo(E other) => ordinal - other.ordinal; |
| 32 |
| 33 /** |
| 34 * Returns the enum constant with the given [name], `null` if not found. |
| 35 */ |
| 36 static Enum2 valueOf(List<Enum2> values, String name) { |
| 37 for (int i = 0; i < values.length; i++) { |
| 38 Enum2 value = values[i]; |
| 39 if (value.name == name) { |
| 40 return value; |
| 41 } |
| 42 } |
| 43 return null; |
| 44 } |
| 45 } |
| 46 |
| 47 |
| 48 /** |
| 10 * Instances of the class [Request] represent a request that was received. | 49 * Instances of the class [Request] represent a request that was received. |
| 11 */ | 50 */ |
| 12 class Request { | 51 class Request { |
| 13 /** | 52 /** |
| 14 * The name of the JSON attribute containing the id of the request. | 53 * The name of the JSON attribute containing the id of the request. |
| 15 */ | 54 */ |
| 16 static const String ID = 'id'; | 55 static const String ID = 'id'; |
| 17 | 56 |
| 18 /** | 57 /** |
| 19 * The name of the JSON attribute containing the name of the request. | 58 * The name of the JSON attribute containing the name of the request. |
| (...skipping 256 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 */ | 315 */ |
| 277 List<String> asStringList() { | 316 List<String> asStringList() { |
| 278 if (!isStringList()) { | 317 if (!isStringList()) { |
| 279 throw new RequestFailure(new Response.invalidParameter(request, path, | 318 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 280 "be a list of strings")); | 319 "be a list of strings")); |
| 281 } | 320 } |
| 282 return datum; | 321 return datum; |
| 283 } | 322 } |
| 284 | 323 |
| 285 /** | 324 /** |
| 325 * Validate that the datum is a list of strings, and convert it into [Enum]s. |
| 326 */ |
| 327 Set<Enum2> asEnumSet(List<Enum2> allValues) { |
| 328 Set values = new Set(); |
| 329 for (String name in asStringList()) { |
| 330 Enum2 value = Enum2.valueOf(allValues, name); |
| 331 if (value == null) { |
| 332 throw new RequestFailure(new Response.invalidParameter(request, path, |
| 333 "be a list of names from the list $allValues")); |
| 334 } |
| 335 values.add(value); |
| 336 } |
| 337 return values; |
| 338 } |
| 339 |
| 340 /** |
| 286 * Determine if the datum is a map whose values are all strings. | 341 * Determine if the datum is a map whose values are all strings. |
| 287 * | 342 * |
| 288 * Note: we can safely assume that the keys are all strings, since JSON maps | 343 * Note: we can safely assume that the keys are all strings, since JSON maps |
| 289 * cannot have any other key type. | 344 * cannot have any other key type. |
| 290 */ | 345 */ |
| 291 bool isStringMap() { | 346 bool isStringMap() { |
| 292 if (datum is! Map) { | 347 if (datum is! Map) { |
| 293 return false; | 348 return false; |
| 294 } | 349 } |
| 295 for (var value in datum.values) { | 350 for (var value in datum.values) { |
| (...skipping 441 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 737 /** | 792 /** |
| 738 * The response to be returned as a result of the failure. | 793 * The response to be returned as a result of the failure. |
| 739 */ | 794 */ |
| 740 final Response response; | 795 final Response response; |
| 741 | 796 |
| 742 /** | 797 /** |
| 743 * Initialize a newly created exception to return the given reponse. | 798 * Initialize a newly created exception to return the given reponse. |
| 744 */ | 799 */ |
| 745 RequestFailure(this.response); | 800 RequestFailure(this.response); |
| 746 } | 801 } |
| OLD | NEW |