OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart.convert; | 5 part of dart.convert; |
6 | 6 |
7 /** | 7 /** |
8 * A [Converter] converts data from one representation into another. | 8 * A [Converter] converts data from one representation into another. |
9 * | 9 * |
10 * It is recommended that implementations of `Converter` extend this class, | 10 * It is recommended that implementations of `Converter` extend this class, |
11 * to inherit any further methods that may be added to the class. | 11 * to inherit any further methods that may be added to the class. |
12 */ | 12 */ |
13 abstract class Converter<S, T> implements StreamTransformer { | 13 abstract class Converter<S, T> { |
14 const Converter(); | 14 const Converter(); |
15 | 15 |
16 /** | 16 /** |
17 * Converts [input] and returns the result of the conversion. | 17 * Converts [input] and returns the result of the conversion. |
18 */ | 18 */ |
19 T convert(S input); | 19 T convert(S input); |
20 | 20 |
21 /** | 21 /** |
22 * Fuses `this` with [other]. | 22 * Fuses `this` with [other]. |
23 * | 23 * |
24 * Encoding with the resulting converter is equivalent to converting with | 24 * Encoding with the resulting converter is equivalent to converting with |
25 * `this` before converting with `other`. | 25 * `this` before converting with `other`. |
26 */ | 26 */ |
27 Converter<S, dynamic> fuse(Converter<T, dynamic> other) { | 27 Converter<S, dynamic> fuse(Converter<T, dynamic> other) { |
28 return new _FusedConverter<S, T, dynamic>(this, other); | 28 return new _FusedConverter<S, T, dynamic>(this, other); |
29 } | 29 } |
30 | |
31 /** | |
32 * Starts a chunked conversion. | |
33 */ | |
34 ChunkedConversionSink startChunkedConversion(Sink sink) { | |
35 throw new UnsupportedError( | |
36 "This converter does not support chunked conversions: $this"); | |
37 } | |
38 | |
39 // Subclasses are encouraged to provide better types. | |
40 Stream bind(Stream stream) { | |
41 return new Stream.eventTransformed( | |
42 stream, | |
43 (EventSink sink) => new _ConverterStreamEventSink(this, sink)); | |
44 } | |
45 } | 30 } |
46 | 31 |
47 /** | 32 /** |
48 * Fuses two converters. | 33 * Fuses two converters. |
49 * | 34 * |
50 * For a non-chunked conversion converts the input in sequence. | 35 * For a non-chunked conversion converts the input in sequence. |
51 */ | 36 */ |
52 class _FusedConverter<S, M, T> extends Converter<S, T> { | 37 class _FusedConverter<S, M, T> extends Converter<S, T> { |
53 final Converter _first; | 38 final Converter<S, M> _first; |
54 final Converter _second; | 39 final Converter<M, T> _second; |
55 | 40 |
56 _FusedConverter(this._first, this._second); | 41 _FusedConverter(this._first, this._second); |
57 | 42 |
58 T convert(S input) => _second.convert(_first.convert(input)); | 43 T convert(S input) => _second.convert(_first.convert(input)); |
59 | |
60 ChunkedConversionSink startChunkedConversion(Sink sink) { | |
61 return _first.startChunkedConversion(_second.startChunkedConversion(sink)); | |
62 } | |
63 } | 44 } |
OLD | NEW |