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/*<S, T>*/ { | 13 abstract class Converter<S, T> implements StreamTransformer<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/*=TT*/> fuse/*<TT>*/( | 27 Converter<S, TT> fuse<TT>(Converter<T, TT> other) { |
28 Converter<T, dynamic/*=TT*/> other) { | 28 return new _FusedConverter<S, T, TT>(this, other); |
29 return new _FusedConverter<S, T, dynamic/*=TT*/>(this, other); | |
30 } | 29 } |
31 | 30 |
32 /** | 31 /** |
33 * Starts a chunked conversion. | 32 * Starts a chunked conversion. |
34 * | 33 * |
35 * The returned sink serves as input for the long-running conversion. The | 34 * The returned sink serves as input for the long-running conversion. The |
36 * given [sink] serves as output. | 35 * given [sink] serves as output. |
37 */ | 36 */ |
38 Sink/*<S>*/ startChunkedConversion(Sink/*<T>*/ sink) { | 37 Sink/*<S>*/ startChunkedConversion(Sink/*<T>*/ sink) { |
39 throw new UnsupportedError( | 38 throw new UnsupportedError( |
40 "This converter does not support chunked conversions: $this"); | 39 "This converter does not support chunked conversions: $this"); |
41 } | 40 } |
42 | 41 |
43 Stream/*<T>*/ bind(Stream/*<S>*/ stream) { | 42 Stream<T> bind(Stream<S> stream) { |
44 return new Stream/*<T>*/.eventTransformed( | 43 return new Stream<T>.eventTransformed( |
45 stream, | 44 stream, |
46 (EventSink sink) => new _ConverterStreamEventSink(this, sink)); | 45 (EventSink sink) => new _ConverterStreamEventSink(this, sink)); |
47 } | 46 } |
48 } | 47 } |
49 | 48 |
50 /** | 49 /** |
51 * Fuses two converters. | 50 * Fuses two converters. |
52 * | 51 * |
53 * For a non-chunked conversion converts the input in sequence. | 52 * For a non-chunked conversion converts the input in sequence. |
54 */ | 53 */ |
55 class _FusedConverter<S, M, T> extends Converter<S, T> | 54 class _FusedConverter<S, M, T> extends Converter<S, T> |
56 implements ChunkedConverter<S, T, S, T> { | 55 implements ChunkedConverter<S, T, S, T> { |
57 final Converter<S, M> _first; | 56 final Converter<S, M> _first; |
58 final Converter<M, T> _second; | 57 final Converter<M, T> _second; |
59 | 58 |
60 _FusedConverter(this._first, this._second); | 59 _FusedConverter(this._first, this._second); |
61 | 60 |
62 T convert(S input) => _second.convert(_first.convert(input)); | 61 T convert(S input) => _second.convert(_first.convert(input)); |
63 | 62 |
64 Sink/*<S>*/ startChunkedConversion(Sink/*<T>*/ sink) { | 63 Sink/*<S>*/ startChunkedConversion(Sink/*<T>*/ sink) { |
65 return _first.startChunkedConversion(_second.startChunkedConversion(sink)); | 64 return _first.startChunkedConversion(_second.startChunkedConversion(sink)); |
66 } | 65 } |
67 } | 66 } |
OLD | NEW |