1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.octo.captcha.engine.sound;
20
21 import java.util.Arrays;
22
23 import com.octo.captcha.CaptchaException;
24 import com.octo.captcha.sound.SoundCaptchaFactory;
25
26 /***
27 * <p/>
28 * This engine is based on a java.util.List of factories. It has a default constructor. Sub class must implements the
29 * buildInitialFactories() method that should build an initial set of factories. </p>
30 *
31 * @author Benoit Doumas
32 * @version 1.0
33 */
34 public abstract class ListSoundCaptchaEngine extends SoundCaptchaEngine {
35
36 public ListSoundCaptchaEngine() {
37 buildInitialFactories();
38 checkFactoriesSize();
39 }
40
41 /***
42 * this method should be implemented as folow : <ul> <li>First construct all the factories you want to initialize
43 * the gimpy with</li> <li>then call the this.addFactoriy method for each factory</li> </ul>
44 */
45 protected abstract void buildInitialFactories();
46
47 /***
48 * Add a factory to the gimpy list
49 *
50 * @return true if added false otherwise
51 */
52 public boolean addFactory(SoundCaptchaFactory factory) {
53 return factory != null && this.factories.add(factory);
54 }
55
56 /***
57 * Add an array of factories to the gimpy list
58 */
59 public void addFactories(SoundCaptchaFactory[] factories) {
60 checkNotNullOrEmpty(factories);
61 this.factories.addAll(Arrays.asList(factories));
62 }
63
64 private void checkFactoriesSize() {
65 if (factories.size() == 0)
66 throw new CaptchaException("This soundEngine has no factories. Please initialize it "
67 + "properly with the buildInitialFactory() called by "
68 + "the constructor or the addFactory() mehtod later!");
69 }
70
71 }