1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 | package agents.anac.y2019.harddealer.math3.util;
|
---|
18 |
|
---|
19 | import agents.anac.y2019.harddealer.math3.exception.MaxCountExceededException;
|
---|
20 | import agents.anac.y2019.harddealer.math3.exception.NullArgumentException;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Utility that increments a counter until a maximum is reached, at
|
---|
24 | * which point, the instance will by default throw a
|
---|
25 | * {@link MaxCountExceededException}.
|
---|
26 | * However, the user is able to override this behaviour by defining a
|
---|
27 | * custom {@link MaxCountExceededCallback callback}, in order to e.g.
|
---|
28 | * select which exception must be thrown.
|
---|
29 | *
|
---|
30 | * @since 3.0
|
---|
31 | * @deprecated Use {@link IntegerSequence.Incrementor} instead.
|
---|
32 | */
|
---|
33 | @Deprecated
|
---|
34 | public class Incrementor {
|
---|
35 | /**
|
---|
36 | * Upper limit for the counter.
|
---|
37 | */
|
---|
38 | private int maximalCount;
|
---|
39 | /**
|
---|
40 | * Current count.
|
---|
41 | */
|
---|
42 | private int count = 0;
|
---|
43 | /**
|
---|
44 | * Function called at counter exhaustion.
|
---|
45 | */
|
---|
46 | private final MaxCountExceededCallback maxCountCallback;
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Default constructor.
|
---|
50 | * For the new instance to be useful, the maximal count must be set
|
---|
51 | * by calling {@link #setMaximalCount(int) setMaximalCount}.
|
---|
52 | */
|
---|
53 | public Incrementor() {
|
---|
54 | this(0);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Defines a maximal count.
|
---|
59 | *
|
---|
60 | * @param max Maximal count.
|
---|
61 | */
|
---|
62 | public Incrementor(int max) {
|
---|
63 | this(max,
|
---|
64 | new MaxCountExceededCallback() {
|
---|
65 | /** {@inheritDoc} */
|
---|
66 | public void trigger(int max) throws MaxCountExceededException {
|
---|
67 | throw new MaxCountExceededException(max);
|
---|
68 | }
|
---|
69 | });
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Defines a maximal count and a callback method to be triggered at
|
---|
74 | * counter exhaustion.
|
---|
75 | *
|
---|
76 | * @param max Maximal count.
|
---|
77 | * @param cb Function to be called when the maximal count has been reached.
|
---|
78 | * @throws NullArgumentException if {@code cb} is {@code null}
|
---|
79 | */
|
---|
80 | public Incrementor(int max, MaxCountExceededCallback cb)
|
---|
81 | throws NullArgumentException {
|
---|
82 | if (cb == null){
|
---|
83 | throw new NullArgumentException();
|
---|
84 | }
|
---|
85 | maximalCount = max;
|
---|
86 | maxCountCallback = cb;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Sets the upper limit for the counter.
|
---|
91 | * This does not automatically reset the current count to zero (see
|
---|
92 | * {@link #resetCount()}).
|
---|
93 | *
|
---|
94 | * @param max Upper limit of the counter.
|
---|
95 | */
|
---|
96 | public void setMaximalCount(int max) {
|
---|
97 | maximalCount = max;
|
---|
98 | }
|
---|
99 |
|
---|
100 | /**
|
---|
101 | * Gets the upper limit of the counter.
|
---|
102 | *
|
---|
103 | * @return the counter upper limit.
|
---|
104 | */
|
---|
105 | public int getMaximalCount() {
|
---|
106 | return maximalCount;
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Gets the current count.
|
---|
111 | *
|
---|
112 | * @return the current count.
|
---|
113 | */
|
---|
114 | public int getCount() {
|
---|
115 | return count;
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Checks whether a single increment is allowed.
|
---|
120 | *
|
---|
121 | * @return {@code false} if the next call to {@link #incrementCount(int)
|
---|
122 | * incrementCount} will trigger a {@code MaxCountExceededException},
|
---|
123 | * {@code true} otherwise.
|
---|
124 | */
|
---|
125 | public boolean canIncrement() {
|
---|
126 | return count < maximalCount;
|
---|
127 | }
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Performs multiple increments.
|
---|
131 | * See the other {@link #incrementCount() incrementCount} method).
|
---|
132 | *
|
---|
133 | * @param value Number of increments.
|
---|
134 | * @throws MaxCountExceededException at counter exhaustion.
|
---|
135 | */
|
---|
136 | public void incrementCount(int value) throws MaxCountExceededException {
|
---|
137 | for (int i = 0; i < value; i++) {
|
---|
138 | incrementCount();
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Adds one to the current iteration count.
|
---|
144 | * At counter exhaustion, this method will call the
|
---|
145 | * {@link MaxCountExceededCallback#trigger(int) trigger} method of the
|
---|
146 | * callback object passed to the
|
---|
147 | * {@link #Incrementor(int,MaxCountExceededCallback) constructor}.
|
---|
148 | * If not explictly set, a default callback is used that will throw
|
---|
149 | * a {@code MaxCountExceededException}.
|
---|
150 | *
|
---|
151 | * @throws MaxCountExceededException at counter exhaustion, unless a
|
---|
152 | * custom {@link MaxCountExceededCallback callback} has been set at
|
---|
153 | * construction.
|
---|
154 | */
|
---|
155 | public void incrementCount() throws MaxCountExceededException {
|
---|
156 | if (++count > maximalCount) {
|
---|
157 | maxCountCallback.trigger(maximalCount);
|
---|
158 | }
|
---|
159 | }
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Resets the counter to 0.
|
---|
163 | */
|
---|
164 | public void resetCount() {
|
---|
165 | count = 0;
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Defines a method to be called at counter exhaustion.
|
---|
170 | * The {@link #trigger(int) trigger} method should usually throw an exception.
|
---|
171 | */
|
---|
172 | public interface MaxCountExceededCallback {
|
---|
173 | /**
|
---|
174 | * Function called when the maximal count has been reached.
|
---|
175 | *
|
---|
176 | * @param maximalCount Maximal count.
|
---|
177 | * @throws MaxCountExceededException at counter exhaustion
|
---|
178 | */
|
---|
179 | void trigger(int maximalCount) throws MaxCountExceededException;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /** Create an instance that delegates everything to a {@link IntegerSequence.Incrementor}.
|
---|
183 | * <p>
|
---|
184 | * This factory method is intended only as a temporary hack for internal use in
|
---|
185 | * Apache Commons Math 3.X series, when {@code Incrementor} is required in
|
---|
186 | * interface (as a return value or in protected fields). It should <em>not</em>
|
---|
187 | * be used in other cases. The {@link IntegerSequence.Incrementor} class should
|
---|
188 | * be used instead of {@code Incrementor}.
|
---|
189 | * </p>
|
---|
190 | * <p>
|
---|
191 | * All methods are mirrored to the underlying {@link IntegerSequence.Incrementor},
|
---|
192 | * as long as neither {@link #setMaximalCount(int)} nor {@link #resetCount()} are called.
|
---|
193 | * If one of these two methods is called, the created instance becomes independent
|
---|
194 | * of the {@link IntegerSequence.Incrementor} used at creation. The rationale is that
|
---|
195 | * {@link IntegerSequence.Incrementor} cannot change their maximal count and cannot be reset.
|
---|
196 | * </p>
|
---|
197 | * @param incrementor wrapped {@link IntegerSequence.Incrementor}
|
---|
198 | * @return an incrementor wrapping an {@link IntegerSequence.Incrementor}
|
---|
199 | * @since 3.6
|
---|
200 | */
|
---|
201 | public static Incrementor wrap(final IntegerSequence.Incrementor incrementor) {
|
---|
202 | return new Incrementor() {
|
---|
203 |
|
---|
204 | /** Underlying incrementor. */
|
---|
205 | private IntegerSequence.Incrementor delegate;
|
---|
206 |
|
---|
207 | {
|
---|
208 | // set up matching values at initialization
|
---|
209 | delegate = incrementor;
|
---|
210 | super.setMaximalCount(delegate.getMaximalCount());
|
---|
211 | super.incrementCount(delegate.getCount());
|
---|
212 | }
|
---|
213 |
|
---|
214 | /** {@inheritDoc} */
|
---|
215 | @Override
|
---|
216 | public void setMaximalCount(int max) {
|
---|
217 | super.setMaximalCount(max);
|
---|
218 | delegate = delegate.withMaximalCount(max);
|
---|
219 | }
|
---|
220 |
|
---|
221 | /** {@inheritDoc} */
|
---|
222 | @Override
|
---|
223 | public void resetCount() {
|
---|
224 | super.resetCount();
|
---|
225 | delegate = delegate.withStart(0);
|
---|
226 | }
|
---|
227 |
|
---|
228 | /** {@inheritDoc} */
|
---|
229 | @Override
|
---|
230 | public void incrementCount() {
|
---|
231 | super.incrementCount();
|
---|
232 | delegate.increment();
|
---|
233 | }
|
---|
234 |
|
---|
235 | };
|
---|
236 | }
|
---|
237 |
|
---|
238 | }
|
---|