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 * *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> { |
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 |
| 29 /** |
| 30 * Starts a chunked conversion. |
| 31 * |
| 32 * The returned [ChunkedConversionSink] supports the interface described by |
| 33 * [inputInterface]. |
| 34 * |
| 35 * If the given [sink] supports the methods described by |
| 36 * [outputInterface] then the chunked conversion can take advantage of |
| 37 * the additional methods and provide a more efficient conversion. |
| 38 * |
| 39 * If [sink] does not support the [outputInterface] it is adapted. If |
| 40 * necessary, the adapter buffers all chunks and invokes |
| 41 * `addNonChunked` on [sink]. |
| 42 */ |
| 43 ChunkedConversionSink startChunkedConversion(ChunkedConversionSink sink) { |
| 44 throw new UnsupportedError( |
| 45 "This converter does not support chunked conversions: $this"); |
| 46 } |
| 47 |
| 48 /** |
| 49 * Defines the accepted input interface for chunked conversions. |
| 50 * |
| 51 * Is `null` if `this` does not support chunked conversions. |
| 52 */ |
| 53 ChunkedConversionInterface get inputInterface => null; |
| 54 |
| 55 /** |
| 56 * Defines the accepted output interface for chunked conversions. |
| 57 * |
| 58 * Is `null` if `this` does not support chunked conversions. |
| 59 */ |
| 60 ChunkedConversionInterface get outputInterface => null; |
28 } | 61 } |
29 | 62 |
30 /** | 63 /** |
31 * Fuses two converters. | 64 * Fuses two converters. |
32 * | 65 * |
33 * For a non-chunked conversion converts the input in sequence. | 66 * For a non-chunked conversion converts the input in sequence. |
34 */ | 67 */ |
35 class _FusedConverter<S, M, T> extends Converter<S, T> { | 68 class _FusedConverter<S, M, T> extends Converter<S, T> { |
36 final Converter _first; | 69 final Converter _first; |
37 final Converter _second; | 70 final Converter _second; |
38 | 71 |
39 _FusedConverter(this._first, this._second); | 72 _FusedConverter(this._first, this._second); |
40 | 73 |
41 T convert(S input) => _second.convert(_first.convert(input)); | 74 T convert(S input) => _second.convert(_first.convert(input)); |
| 75 |
| 76 ChunkedConversionSink startChunkedConversion(ChunkedConversionSink sink) { |
| 77 return _first.startChunkedConversion(_second.startChunkedConversion(sink)); |
| 78 } |
| 79 |
| 80 ChunkedConversionInterface get inputInterface { |
| 81 // Don't advertise a chunked conversion if the _second converter doesn't |
| 82 // support it. |
| 83 if (_second.inputInterface != null) return _first.inputInterface; |
| 84 } |
| 85 ChunkedConversionInterface get outputInterface { |
| 86 // Don't advertise a chunked conversion if the _first converter doesn't |
| 87 // support it. |
| 88 if (_first.outputInterface != null) return _second.outputInterface; |
| 89 } |
42 } | 90 } |
OLD | NEW |