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 encoder. 026 * 027 * @author <a href="mailto:dev@directory.apache.org"> Apache Directory Project</a> 028 * @version $Rev: 437004 $ 029 */ 030 public abstract class AbstractStatefulEncoder implements StatefulEncoder 031 { 032 /** this encoder's callback */ 033 private EncoderCallback cb = null; 034 035 /** this encoder's monitor */ 036 private EncoderMonitor monitor = null; 037 038 039 // ------------------------------------------------------------------------ 040 // constructors 041 // ------------------------------------------------------------------------ 042 043 /** 044 * Creates a stateful encoder where the callback and monitor must be set. 045 */ 046 public AbstractStatefulEncoder() 047 { 048 } 049 050 051 /** 052 * Creates a stateful encoder with a callback. 053 * 054 * @param cb 055 * the callback to use for this encoder 056 */ 057 public AbstractStatefulEncoder(EncoderCallback cb) 058 { 059 setCallback( cb ); 060 } 061 062 063 /** 064 * Creates a stateful encoder with a monitor but no callback. 065 * 066 * @param monitor 067 * the monitor to use for this encoder 068 */ 069 public AbstractStatefulEncoder(EncoderMonitor monitor) 070 { 071 this.monitor = monitor; 072 } 073 074 075 /** 076 * Creates a stateful encoder. 077 * 078 * @param cb 079 * the callback to use for this encoder 080 * @param monitor 081 * the monitor to use for this encoder 082 */ 083 public AbstractStatefulEncoder(EncoderCallback cb, EncoderMonitor monitor) 084 { 085 this.monitor = monitor; 086 setCallback( cb ); 087 } 088 089 090 // ------------------------------------------------------------------------ 091 // StatefulEncoder methods 092 // ------------------------------------------------------------------------ 093 094 /* 095 * (non-Javadoc) 096 * 097 * @see org.apache.asn1.codec.stateful.StatefulEncoder#setCallback( 098 * org.apache.asn1.codec.stateful.EncoderCallback) 099 */ 100 public void setCallback( EncoderCallback cb ) 101 { 102 EncoderCallback 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.StatefulEncoder#setEncoderMonitor( 116 * org.apache.asn1.codec.stateful.EncoderMonitor) 117 */ 118 public void setEncoderMonitor( EncoderMonitor 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 encoder has 130 * encoded a unit of encoded data. 131 * 132 * @param encoded 133 * the encoded byproduct. 134 */ 135 protected void encodeOccurred( Object encoded ) 136 { 137 if ( cb != null ) 138 { 139 cb.encodeOccurred( this, encoded ); 140 } 141 } 142 143 144 /** 145 * Gets the encoder's monitor. 146 * 147 * @return the monitor for this StatefulEncoder 148 */ 149 protected EncoderMonitor getEncoderMonitor() 150 { 151 return monitor; 152 } 153 }