001    /*
002     *  Licensed to the Apache Software Foundation (ASF) under one
003     *  or more contributor license agreements.  See the NOTICE file
004     *  distributed with this work for additional information
005     *  regarding copyright ownership.  The ASF licenses this file
006     *  to you under the Apache License, Version 2.0 (the
007     *  "License"); you may not use this file except in compliance
008     *  with the License.  You may obtain a copy of the License at
009     *  
010     *    http://www.apache.org/licenses/LICENSE-2.0
011     *  
012     *  Unless required by applicable law or agreed to in writing,
013     *  software distributed under the License is distributed on an
014     *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *  KIND, either express or implied.  See the License for the
016     *  specific language governing permissions and limitations
017     *  under the License. 
018     *  
019     */
020    package org.apache.directory.shared.asn1.codec.stateful;
021    
022    
023    /**
024     * Convenience class to not have to reimplement the two setter methods everytime
025     * one starts a new decoder.
026     * 
027     * @author <a href="mailto:dev@directory.apache.org"> Apache Directory Project</a>
028     * @version $Rev: 437004 $
029     */
030    public abstract class AbstractStatefulDecoder implements StatefulDecoder
031    {
032        /** this decoder's callback */
033        private DecoderCallback cb = null;
034    
035        /** this decoder's monitor */
036        private DecoderMonitor monitor = null;
037    
038    
039        // ------------------------------------------------------------------------
040        // constructors
041        // ------------------------------------------------------------------------
042    
043        /**
044         * Creates a stateful decoder where the callback and monitor must be set.
045         */
046        public AbstractStatefulDecoder()
047        {
048        }
049    
050    
051        /**
052         * Creates a stateful decoder with a callback.
053         * 
054         * @param cb
055         *            the callback to use for this decoder
056         */
057        public AbstractStatefulDecoder(DecoderCallback cb)
058        {
059            setCallback( cb );
060        }
061    
062    
063        /**
064         * Creates a stateful decoder with a monitor but no callback.
065         * 
066         * @param monitor
067         *            the monitor to use for this decoder
068         */
069        public AbstractStatefulDecoder(DecoderMonitor monitor)
070        {
071            this.monitor = monitor;
072        }
073    
074    
075        /**
076         * Creates a stateful decoder.
077         * 
078         * @param cb
079         *            the callback to use for this decoder
080         * @param monitor
081         *            the monitor to use for this decoder
082         */
083        public AbstractStatefulDecoder(DecoderCallback cb, DecoderMonitor monitor)
084        {
085            this.monitor = monitor;
086            setCallback( cb );
087        }
088    
089    
090        // ------------------------------------------------------------------------
091        // StatefulDecoder methods
092        // ------------------------------------------------------------------------
093    
094        /*
095         * (non-Javadoc)
096         * 
097         * @see org.apache.asn1.codec.stateful.StatefulDecoder#setCallback(
098         *      org.apache.asn1.codec.stateful.DecoderCallback)
099         */
100        public void setCallback( DecoderCallback cb )
101        {
102            DecoderCallback old = this.cb;
103            this.cb = cb;
104    
105            if ( this.monitor != null )
106            {
107                this.monitor.callbackSet( this, old, cb );
108            }
109        }
110    
111    
112        /*
113         * (non-Javadoc)
114         * 
115         * @see org.apache.asn1.codec.stateful.StatefulDecoder#setDecoderMonitor(
116         *      org.apache.asn1.codec.stateful.DecoderMonitor)
117         */
118        public void setDecoderMonitor( DecoderMonitor monitor )
119        {
120            this.monitor = monitor;
121        }
122    
123    
124        // ------------------------------------------------------------------------
125        // protected methods
126        // ------------------------------------------------------------------------
127    
128        /**
129         * Notifies via the callback if one has been set that this decoder has
130         * decoded a unit of encoded data.
131         * 
132         * @param decoded
133         *            the decoded byproduct.
134         */
135        protected void decodeOccurred( Object decoded )
136        {
137            if ( cb != null )
138            {
139                cb.decodeOccurred( this, decoded );
140            }
141        }
142    
143    
144        /**
145         * Gets the decoder's monitor.
146         * 
147         * @return the monitor for this StatefulDecoder
148         */
149        protected DecoderMonitor getDecoderMonitor()
150        {
151            return monitor;
152        }
153    }