1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17 package org.apache.asn1.codec.stateful ;
18
19
20 /**
21 * Convenience class to not have to reimplement the two setter methods everytime
22 * one starts a new encoder.
23 *
24 * @author <a href="mailto:dev@directory.apache.org">
25 * Apache Directory Project</a>
26 * @version $Rev: 157644 $
27 */
28 public abstract class AbstractStatefulEncoder implements StatefulEncoder
29 {
30 /** this encoder's callback */
31 private EncoderCallback cb = null ;
32 /** this encoder's monitor */
33 private EncoderMonitor monitor = null ;
34
35
36 // ------------------------------------------------------------------------
37 // constructors
38 // ------------------------------------------------------------------------
39
40
41 /**
42 * Creates a stateful encoder where the callback and monitor must be set.
43 */
44 public AbstractStatefulEncoder()
45 {
46 }
47
48
49 /**
50 * Creates a stateful encoder with a callback.
51 *
52 * @param cb the callback to use for this encoder
53 */
54 public AbstractStatefulEncoder( EncoderCallback cb )
55 {
56 setCallback( cb ) ;
57 }
58
59
60 /**
61 * Creates a stateful encoder with a monitor but no callback.
62 *
63 * @param monitor the monitor to use for this encoder
64 */
65 public AbstractStatefulEncoder( EncoderMonitor monitor )
66 {
67 this.monitor = monitor ;
68 }
69
70
71 /**
72 * Creates a stateful encoder.
73 *
74 * @param cb the callback to use for this encoder
75 * @param monitor the monitor to use for this encoder
76 */
77 public AbstractStatefulEncoder( EncoderCallback cb, EncoderMonitor monitor )
78 {
79 this.monitor = monitor ;
80 setCallback( cb ) ;
81 }
82
83
84 // ------------------------------------------------------------------------
85 // StatefulEncoder methods
86 // ------------------------------------------------------------------------
87
88
89 /* (non-Javadoc)
90 * @see org.apache.asn1.codec.stateful.StatefulEncoder#setCallback(
91 * org.apache.asn1.codec.stateful.EncoderCallback)
92 */
93 public void setCallback( EncoderCallback cb )
94 {
95 EncoderCallback old = this.cb ;
96 this.cb = cb ;
97
98 if ( this.monitor != null )
99 {
100 this.monitor.callbackSet( this, old, cb );
101 }
102 }
103
104
105 /* (non-Javadoc)
106 * @see org.apache.asn1.codec.stateful.StatefulEncoder#setEncoderMonitor(
107 * org.apache.asn1.codec.stateful.EncoderMonitor)
108 */
109 public void setEncoderMonitor( EncoderMonitor monitor )
110 {
111 this.monitor = monitor ;
112 }
113
114
115 // ------------------------------------------------------------------------
116 // protected methods
117 // ------------------------------------------------------------------------
118
119
120 /**
121 * Notifies via the callback if one has been set that this encoder has
122 * encoded a unit of encoded data.
123 *
124 * @param encoded the encoded byproduct.
125 */
126 protected void encodeOccurred( Object encoded )
127 {
128 if ( cb != null )
129 {
130 cb.encodeOccurred( this, encoded ) ;
131 }
132 }
133
134
135 /**
136 * Gets the encoder's monitor.
137 *
138 * @return the monitor for this StatefulEncoder
139 */
140 protected EncoderMonitor getEncoderMonitor()
141 {
142 return monitor ;
143 }
144 }