OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 part of dart.convert; | |
6 | |
7 /** | |
8 * A [ChunkedConversionInterface] defines an interface through which a | |
9 * converter can be accessed when doing chunked conversions. | |
10 * | |
11 * A [ChunkedConversionInterface]s is associated with a [ChunkedConversionSink]. | |
12 * It represents this sink and can adapt sinks to this interface. | |
13 */ | |
14 abstract class ChunkedConversionInterface { | |
15 const ChunkedConversionInterface(); | |
16 | |
17 /** | |
18 * Adapts the given [sink] to the [ChunkedConversionSink] associated with | |
19 * `this`. | |
20 * | |
21 * If `this` does not know the interface of [sink] it adapts to the | |
22 * non-chunked interface [ChunkedConversionSink.INTERFACE] which is supported | |
23 * by every [ChunkedConversionSink]. | |
24 * | |
25 * This method must not invoke the [sink]'s `adaptTo` method. | |
26 */ | |
27 ChunkedConversionSink adapt(ChunkedConversionSink sink); | |
28 } | |
29 | |
30 /** | |
31 * A [ChunkedConversionSink] is used to transmit data more efficiently between | |
32 * two converters during chunked conversions. | |
33 * | |
34 * This base class, [ChunkedConversionSink], only supports non-chunked | |
35 * conversions (using [addNonChunked]), but sub-classes are encouraged to | |
36 * provide more efficient means. | |
37 */ | |
38 abstract class ChunkedConversionSink<T, ChunkedT> { | |
Søren Gjesse
2013/07/24 09:26:41
Consider moving the documentation of T and chunked
floitsch
2013/07/24 18:31:15
ChunkedT was removed.
I hope that the single gener
| |
39 /** The default interface falls back to a non-chunked conversion. */ | |
40 static const ChunkedConversionInterface INTERFACE = | |
41 const _NonChunkedInterface(); | |
42 | |
43 ChunkedConversionSink(); | |
44 /** | |
45 * Creates a [ChunkedConversionSink] with a callback. | |
46 * | |
47 * The [callback] is invoked when `this` sink receives data through | |
48 * [addNonChunked]. | |
49 */ | |
50 factory ChunkedConversionSink.withCallback(void callback(T input)) | |
51 = _CallbackSink; | |
52 | |
53 /** | |
54 * Adds data to `this` sink. | |
55 * | |
56 * The given [input] is received in one step, in a non-chunked way. | |
57 * | |
58 * This method is generally used as means of adapting two incompatible sinks | |
59 * or as the entry or exit point of a sequence of converters. In that case the | |
60 * conversion starts (or ends) non-chunked, but converters can use more | |
61 * efficient interfaces to transmit their data from one converter to the | |
62 * next. | |
63 */ | |
64 void addNonChunked(T input); | |
65 | |
66 /** | |
67 * The interface this sink implements. | |
68 */ | |
69 ChunkedConversionInterface get interface => INTERFACE; | |
70 | |
71 /** | |
72 * Adds chunked data to this sink. | |
73 * | |
74 * This method is used when converters are used as [StreamTransformer]s. | |
75 * | |
76 * The type [ChunkedT] may be different than [T]. For example a converter may | |
77 * convert from list of strings, but the chunked interface could expects | |
78 * strings directly. | |
79 */ | |
80 void add(ChunkedT o) { | |
81 throw new UnsupportedError("Chunked conversion - add"); | |
82 } | |
83 | |
84 /** | |
85 * Adapts `this` to support the given [interface]. | |
86 * | |
87 * This method may use the [interface]'s `adapt` method. | |
88 */ | |
89 ChunkedConversionSink adaptTo(ChunkedConversionInterface interface) { | |
90 if (this.interface == interface) return this; | |
91 return interface.adapt(this); | |
92 } | |
93 | |
94 /** | |
95 * Closes the sink. | |
96 * | |
97 * This signals the end of the chunked conversion. This method is called | |
98 * when converters are used as [StreamTransformer]'s. | |
99 */ | |
100 void close() { throw new UnsupportedError("Chunked conversion - close"); } | |
101 } | |
102 | |
103 typedef void _ChunkedConversionCallback<T>(T data); | |
104 | |
105 class _CallbackSink<T, ChunkedT> extends ChunkedConversionSink<T, dynamic> { | |
106 final _ChunkedConversionCallback<T> _callback; | |
107 _CallbackSink(this._callback); | |
108 | |
109 void addNonChunked(T input) { _callback(input); } | |
110 } | |
111 | |
112 class _NonChunkedInterface extends ChunkedConversionInterface { | |
113 const _NonChunkedInterface(); | |
114 | |
115 ChunkedConversionSink adapt(ChunkedConversionSink sink) { | |
116 // Every sink is suitable as a non-chunked sink. | |
117 return sink; | |
118 } | |
119 } | |
120 | |
121 class _NonChunkedSink<S, T> | |
122 extends ChunkedConversionSink<S, dynamic> { | |
123 final Converter<S, T> _converter; | |
124 final ChunkedConversionSink<T, dynamic> _output; | |
125 | |
126 _NonChunkedSink(this._converter, this._output); | |
127 | |
128 void addNonChunked(S input) { | |
129 _output.addNonChunked(_converter.convert(input)); | |
130 } | |
131 } | |
OLD | NEW |