KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > wrappers > LimiterWrapper


1 /*
2   Copyright (C) 2001 Renaud Pawlak <renaud@aopsys.com>
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.wrappers;
19
20 import org.aopalliance.intercept.ConstructorInvocation;
21 import org.aopalliance.intercept.MethodInvocation;
22 import org.objectweb.jac.core.AspectComponent;
23 import org.objectweb.jac.core.Interaction;
24 import org.objectweb.jac.core.Wrapper;
25
26 /**
27  * <code>LimiterWrapper</code> implements a wrapper that tests a
28  * counter value before calling the wrappee object. If the counter is
29  * not with the max and min limits, it raises the
30  * <code>LimiterException</code>
31  *
32  * <p>Use this wrapper as follows:
33  *
34  * <ul><pre>
35  * LimiterWrapper lw = new LimiterWrapper(0, 100);
36  * a_stack.wrap(lw, "inc", "push");
37  * a_stack.wrap(lw, "dec", "pop");
38  * a_stack.wrap(lw, "testMax", "push");
39  * a_stack.wrap(lw, "testMin", "pop");
40  * </pre></ul>
41  *
42  * <p>Where <code>a_stack</code> is an instance of a class
43  * <code>Stack</code>. When wrapping the stack, it will raise a
44  * <code>LimiterException</code> if a push is done and that the
45  * counter is greater or equal to 100 or if a pop is done and that the
46  * counter is lower or equal to 0.
47  *
48  * <p> NOTE: this class cannot be used alone. Its instances must
49  * wrap some instances of other objects (possibly other
50  * wrappers).
51  *
52  * @see org.objectweb.jac.core.Wrappee
53  * @see org.objectweb.jac.core.Wrapping#wrap(Wrappee,Wrapper,AbstractMethodItem)
54  * @see org.objectweb.jac.wrappers.LimiterException */

55
56 public class LimiterWrapper extends Wrapper {
57
58    /** Store the maximum bound of the limiter. */
59     protected int max;
60    /** Store the minimum bound of the limiter. */
61     protected int min;
62    /** Store the counter of the limiter. */
63     protected int counter = 0;
64
65    /**
66     * Construct a new limiter and initialize the bounds.
67     *
68     * @param min the minimum counter value.
69     * @param max the maximum counter value.
70     */

71    public LimiterWrapper(AspectComponent ac, int min, int max) {
72       super(ac);
73       this.min = min;
74       this.max = max;
75    }
76
77    /**
78     * Return the max bound of the limiter.
79     *
80     * @return the max bound
81     */

82    public int getMax() {
83       return max;
84    }
85    
86    /**
87     * Set the max bound of the limiter.
88     *
89     * @param max the new max bound
90     */

91    public void setMax(int max) {
92       this.max = max;
93    }
94
95    /**
96      * Return the current counter of the limiter.
97      *
98      * @return the counter
99      */

100    public int getCounter() {
101       return counter;
102    }
103
104     /**
105      * This wrapping method increments the limiter counter and calls
106      * the wrappee method.
107      *
108      * <p>For instance, <code>inc</code> could wrap the
109      * <code>push</code> method of a stack so that the counter is
110      * incremented when a new element is placed on the top of the
111      * stack.
112      *
113      * <p>NOTE: this method do not test the bounds. Use
114      * <code>testMax</code> to do this.
115      *
116      * @see LimiterWrapper#testMax(Interaction)
117      *
118      * @return the original method return value
119      */

120     
121     public Object JavaDoc inc(Interaction interaction) {
122        counter++;
123        return proceed(interaction);
124     }
125
126     /**
127      * This wrapping method decrements the limiter counter and calls
128      * the wrappee method.
129      *
130      * <p>For instance, <code>dec</code> could wrap the
131      * <code>pop</code> method of a stack so that the counter is
132      * incremented when an element is removed from it.
133      *
134      * <p>NOTE: this method do not test the bounds. Use
135      * <code>testMin</code> to do this.
136      *
137      * @see LimiterWrapper#testMin(Interaction)
138      *
139      * @return the original method return value
140      */

141      
142     public Object JavaDoc dec(Interaction interaction) {
143        counter--;
144        return proceed(interaction);
145     }
146     
147     /**
148      * This wrapping method tests the counter of the limiter and
149      * raises the <code>LimiterExeption</code> when when it is over
150      * the maximum value.
151      *
152      * <p>NOTE: this method must be used with the <code>inc</code> and
153      * <code>dec</code> wrapping methods that mofify the counter
154      * value.
155      *
156      * @see LimiterWrapper#inc(Interaction)
157      * @see LimiterWrapper#dec(Interaction)
158      *
159      * @return the original method return value or raise an exception
160      */

161      
162     public Object JavaDoc testMax(Interaction interaction)
163        throws LimiterException {
164        if(counter >= max) {
165           throw new LimiterException("counter reached its maximum count");
166        }
167        return proceed(interaction);
168     }
169
170     /**
171      * This wrapping method tests the counter of the limiter and
172      * raises the <code>LimiterExeption</code> when when it is below
173      * the minimum value.
174      *
175      * <p>NOTE: this method must be used with the <code>inc</code> and
176      * <code>dec</code> wrapping methods that mofify the counter
177      * value.
178      *
179      * @see LimiterWrapper#inc(Interaction)
180      * @see LimiterWrapper#dec(Interaction)
181      *
182      * @return the original method return value or raise an exception
183      */

184      
185     public Object JavaDoc testMin(Interaction interaction)
186        throws LimiterException {
187        if(counter <= min) {
188           throw new LimiterException("counter reached its minimum count");
189        }
190        return proceed(interaction);
191     }
192
193     /* (non-Javadoc)
194      * @see org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance.intercept.MethodInvocation)
195      */

196     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
197         // TODO Auto-generated method stub
198
return null;
199     }
200
201     /* (non-Javadoc)
202      * @see org.aopalliance.intercept.ConstructorInterceptor#construct(org.aopalliance.intercept.ConstructorInvocation)
203      */

204     public Object JavaDoc construct(ConstructorInvocation invocation) throws Throwable JavaDoc {
205         // TODO Auto-generated method stub
206
return null;
207     }
208
209 }
210
Popular Tags