Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(162)

Side by Side Diff: sdk/lib/convert/converter.dart

Issue 20374003: Add transformer support to Converters. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Provide better types for bind. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 * *Converters are still experimental and are subject to change without notice.* 10 * *Converters are still experimental and are subject to change without notice.*
11 * 11 *
12 */ 12 */
13 abstract class Converter<S, T> { 13 abstract class Converter<S, T> implements StreamTransformer {
14 /** 14 /**
15 * Converts [input] and returns the result of the conversion. 15 * Converts [input] and returns the result of the conversion.
16 */ 16 */
17 T convert(S input); 17 T convert(S input);
18 18
19 /** 19 /**
20 * Fuses `this` with [other]. 20 * Fuses `this` with [other].
21 * 21 *
22 * Encoding with the resulting converter is equivalent to converting with 22 * Encoding with the resulting converter is equivalent to converting with
23 * `this` before converting with `other`. 23 * `this` before converting with `other`.
24 */ 24 */
25 Converter<S, dynamic> fuse(Converter<T, dynamic> other) { 25 Converter<S, dynamic> fuse(Converter<T, dynamic> other) {
26 return new _FusedConverter<S, T, dynamic>(this, other); 26 return new _FusedConverter<S, T, dynamic>(this, other);
27 } 27 }
28 28
29 /** 29 /**
30 * Starts a chunked conversion. 30 * Starts a chunked conversion.
31 */ 31 */
32 ChunkedConversionSink startChunkedConversion(ChunkedConversionSink sink) { 32 ChunkedConversionSink startChunkedConversion(ChunkedConversionSink sink) {
33 throw new UnsupportedError( 33 throw new UnsupportedError(
34 "This converter does not support chunked conversions: $this"); 34 "This converter does not support chunked conversions: $this");
35 } 35 }
36
37 // Subclasses are encouraged to provide better types.
38 Stream bind(Stream source) {
39 return new _ConverterTransformStream(source, this);
40 }
36 } 41 }
37 42
38 /** 43 /**
39 * Fuses two converters. 44 * Fuses two converters.
40 * 45 *
41 * For a non-chunked conversion converts the input in sequence. 46 * For a non-chunked conversion converts the input in sequence.
42 */ 47 */
43 class _FusedConverter<S, M, T> extends Converter<S, T> { 48 class _FusedConverter<S, M, T> extends Converter<S, T> {
44 final Converter _first; 49 final Converter _first;
45 final Converter _second; 50 final Converter _second;
46 51
47 _FusedConverter(this._first, this._second); 52 _FusedConverter(this._first, this._second);
48 53
49 T convert(S input) => _second.convert(_first.convert(input)); 54 T convert(S input) => _second.convert(_first.convert(input));
50 55
51 ChunkedConversionSink startChunkedConversion(ChunkedConversionSink sink) { 56 ChunkedConversionSink startChunkedConversion(ChunkedConversionSink sink) {
52 return _first.startChunkedConversion(_second.startChunkedConversion(sink)); 57 return _first.startChunkedConversion(_second.startChunkedConversion(sink));
53 } 58 }
54 } 59 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698