1 /*
2 * Copyright 2004-2005 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 import java.util.LinkedList;
21
22
23 /**
24 * A convenience callback which collects decoded or encoded objects to audit a
25 * codecs's activity. The callback also comes in handy when data is to be
26 * pushed through a codec and grabed immediately afterwords to serialize codec
27 * operation.
28 *
29 * @author <a href="mailto:dev@directory.apache.org">
30 * Apache Directory Project</a>
31 * @version $Rev: 161723 $
32 */
33 public class CallbackHistory implements DecoderCallback, EncoderCallback
34 {
35 /** history of decoded objects in cronological order */
36 private final LinkedList history ;
37 /** the length of callback history stored */
38 private final int length ;
39
40
41 /**
42 * Creates an auditing callback that manages a history of indefinite length.
43 */
44 public CallbackHistory()
45 {
46 this( -1 ) ;
47 }
48
49
50 /**
51 * Creates an auditing callback that manages a history of fixed or
52 * indefinite length. If the length is fixed the history effectively
53 * becomes a FIFO structure.
54 *
55 * @param length the maximum length of callback history to store before
56 * dropping decoded items, a length of zero or 1 corresponds to indefinite
57 * history
58 */
59 public CallbackHistory( int length )
60 {
61 this.length = length ;
62 history = new LinkedList() ;
63 }
64
65
66 /* (non-Javadoc)
67 * @see org.apache.asn1.codec.stateful.DecoderCallback#decodeOccurred(
68 * org.apache.asn1.codec.stateful.StatefulDecoder, java.lang.Object)
69 */
70 public void decodeOccurred( StatefulDecoder decoder, Object decoded )
71 {
72 if ( length > 0 )
73 {
74 while ( history.size() >= length )
75 {
76 history.removeLast() ;
77 }
78 }
79
80 history.addFirst( decoded ) ;
81 }
82
83
84 /**
85 * Callback to deliver a fully encoded object.
86 *
87 * @param encoder the stateful encoder driving the callback
88 * @param encoded the object that was encoded
89 */
90 public void encodeOccurred( StatefulEncoder encoder, Object encoded )
91 {
92 if ( length > 0 )
93 {
94 while ( history.size() >= length )
95 {
96 history.removeLast() ;
97 }
98 }
99
100 history.addFirst( encoded ) ;
101 }
102
103
104 /**
105 * Gets the most recent decoded object if one exists.
106 *
107 * @return the most recent decoded object
108 * @throws java.util.NoSuchElementException if the history is empty
109 */
110 public Object getMostRecent()
111 {
112 return history.getFirst() ;
113 }
114
115
116 /**
117 * Gets the oldest decoded object if one exists.
118 *
119 * @return the oldest decoded object
120 * @throws java.util.NoSuchElementException if the history is empty
121 */
122 public Object getOldest()
123 {
124 return history.getLast() ;
125 }
126
127
128 /**
129 * Tests to see if the history is empty.
130 *
131 * @return true if the history is empty, false otherwise
132 */
133 public boolean isEmpty()
134 {
135 return history.isEmpty() ;
136 }
137
138
139 /**
140 * Clears the history of decoded items.
141 */
142 public void clear()
143 {
144 history.clear() ;
145 }
146
147
148 /**
149 * Gets the number of decoded items in the callback history.
150 *
151 * @return the number of decoded items in the callback history
152 */
153 public int size()
154 {
155 return history.size() ;
156 }
157 }