|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
RunnerPool.java | 100% | 94.1% | 87.5% | 92.6% |
|
1 | /** | |
2 | * JTRunner is free software; you can redistribute it and/or modify it under the | |
3 | * terms of the GNU General Public License as published by the Free Software | |
4 | * Foundation; either version 2, or (at your option) any later version. | |
5 | */ | |
6 | ||
7 | package jtr.pool; | |
8 | ||
9 | import java.util.*; | |
10 | import jtr.runners.*; | |
11 | ||
12 | /** | |
13 | * This class represents a pool of <code>IRunner</code> instances.<br> | |
14 | * Only <code>IRunnerParameterized</code> instances are allowed to be added to | |
15 | * the pool, and once added they all become <code>IRunnerPooled</code>. Only | |
16 | * <code>IRunnerPooled</code> instances are elegible for execution. | |
17 | * | |
18 | * @author Francesco Russo (frusso@dev.java.net) | |
19 | * @version 4.0 | |
20 | * @since 1.0 | |
21 | */ | |
22 | public class RunnerPool { | |
23 | /** | |
24 | * Costructor. Requires the reference to the pool manager that will manage | |
25 | * the pool. | |
26 | * | |
27 | * @param poolManager | |
28 | * IPoolManager | |
29 | */ | |
30 | 2 | public RunnerPool(IPoolManager poolManager) { |
31 | 2 | pool = new Vector<IRunnerParameterized>(); |
32 | 2 | this.setPoolManager(poolManager); |
33 | } | |
34 | ||
35 | /** | |
36 | * Adds an <code>IRunnerParameterized</code> to the current pool. | |
37 | * | |
38 | * @param runner | |
39 | * IRunnerParameterized | |
40 | * @return IRunnerPooled | |
41 | */ | |
42 | 4998 | public synchronized IRunnerPooled add(IRunnerParameterized runner) { |
43 | 4998 | IRunnerPooled pooledRunner = runner.setPool(this); |
44 | 4998 | pool.add(pooledRunner); |
45 | 4998 | return pooledRunner; |
46 | } | |
47 | ||
48 | /** | |
49 | * Adds a set of <code>IRunnerParameterized</code> to the current pool. | |
50 | * | |
51 | * @param runners | |
52 | * IRunnerParameterized[] | |
53 | * @return IRunnerPooled[] | |
54 | */ | |
55 | 12 | public synchronized IRunnerPooled[] addAll(IRunnerParameterized[] runners) { |
56 | 12 | for (int i = 0; i < runners.length; i++) { |
57 | 98 | runners[i] = add(runners[i]); |
58 | } | |
59 | 12 | return (IRunnerPooled[]) runners; |
60 | } | |
61 | ||
62 | /** | |
63 | * Removes the <code>IRunnerPooled</code> allocated at the provided index. | |
64 | * | |
65 | * @param index | |
66 | * int | |
67 | * @return IRunnerPooled | |
68 | */ | |
69 | 0 | public synchronized IRunnerPooled remove(int index) { |
70 | 0 | return (IRunnerPooled) pool.remove(index); |
71 | } | |
72 | ||
73 | /** | |
74 | * Make the pool empty and returns all the removed entries. | |
75 | * | |
76 | * @return IRunnerPooled[] | |
77 | */ | |
78 | 102 | public synchronized IRunnerPooled[] removeAll() { |
79 | 102 | IRunnerPooled[] pRunners = new IRunnerPooled[pool.size()]; |
80 | 102 | pRunners = (IRunnerPooled[]) pool.toArray(pRunners); |
81 | 102 | pool.removeAllElements(); |
82 | 102 | return pRunners; |
83 | } | |
84 | ||
85 | /** | |
86 | * Gets the <code>IPoolManager</code> in charge of managing this pool. | |
87 | * | |
88 | * @return IPoolManager | |
89 | */ | |
90 | 4900 | public IPoolManager getPoolManager() { |
91 | 4900 | return poolManager; |
92 | } | |
93 | ||
94 | /** | |
95 | * Sets the <code>IPoolManager</code> in charge of managing this pool. | |
96 | * | |
97 | * @param poolManager | |
98 | * IPoolManager | |
99 | */ | |
100 | 2 | public void setPoolManager(IPoolManager poolManager) { |
101 | 2 | this.poolManager = poolManager; |
102 | } | |
103 | ||
104 | /** | |
105 | * Returns the actual size of the pool. | |
106 | * | |
107 | * @return int | |
108 | */ | |
109 | 4804 | public int size() { |
110 | 4804 | pool.trimToSize(); |
111 | 4804 | return pool.size(); |
112 | } | |
113 | ||
114 | private Vector<IRunnerParameterized> pool; | |
115 | private IPoolManager poolManager; | |
116 | } |
|