moved to: https://sites.google.com/site/cmzmasek/home/software/forester
[jalview.git] / forester / java / src / org / forester / archaeopteryx / tools / ProcessPool.java
1 // $Id:\r
2 // FORESTER -- software libraries and applications\r
3 // for evolutionary biology research and applications.\r
4 //\r
5 // Copyright (C) 2008-2012 Christian M. Zmasek\r
6 // Copyright (C) 2008-2012 Sanford Burnham Medical Research Institute\r
7 // All rights reserved\r
8 //\r
9 // This library is free software; you can redistribute it and/or\r
10 // modify it under the terms of the GNU Lesser General Public\r
11 // License as published by the Free Software Foundation; either\r
12 // version 2.1 of the License, or (at your option) any later version.\r
13 //\r
14 // This library is distributed in the hope that it will be useful,\r
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
17 // Lesser General Public License for more details.\r
18 //\r
19 // You should have received a copy of the GNU Lesser General Public\r
20 // License along with this library; if not, write to the Free Software\r
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA\r
22 //\r
23 // Contact: phylosoft @ gmail . com\r
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester\r
25 \r
26 package org.forester.archaeopteryx.tools;\r
27 \r
28 import java.util.ArrayList;\r
29 import java.util.List;\r
30 \r
31 public class ProcessPool {\r
32 \r
33     private final static boolean            DEBUG = true;\r
34     private final ArrayList<ProcessRunning> _processes;\r
35 \r
36     private ProcessPool() {\r
37         _processes = new ArrayList<ProcessRunning>();\r
38     }\r
39 \r
40     public static ProcessPool createInstance() {\r
41         return new ProcessPool();\r
42     }\r
43 \r
44     public synchronized ProcessRunning getProcessByIndex( final int i ) {\r
45         return getProcesses().get( i );\r
46     }\r
47 \r
48     public synchronized int size() {\r
49         return getProcesses().size();\r
50     }\r
51 \r
52     public synchronized ProcessRunning getProcessById( final long id ) {\r
53         for( final ProcessRunning p : getProcesses() ) {\r
54             if ( p.getId() == id ) {\r
55                 return p;\r
56             }\r
57         }\r
58         return null;\r
59     }\r
60 \r
61     public synchronized long addProcess( final String name ) {\r
62         final ProcessRunning p = ProcessRunning.createInstance( name );\r
63         final long id = p.getId();\r
64         if ( getProcessById( id ) != null ) {\r
65             throw new IllegalStateException( " process with id " + id + "already exists" );\r
66         }\r
67         getProcesses().add( p );\r
68         if ( DEBUG ) {\r
69             System.out.println( " pp: added: " + p );\r
70         }\r
71         return id;\r
72     }\r
73 \r
74     public synchronized boolean removeProcess( final long id ) {\r
75         final int i = getProcessIndexById( id );\r
76         if ( i >= 0 ) {\r
77             if ( DEBUG ) {\r
78                 final ProcessRunning p = getProcessById( id );\r
79                 System.out.println( " pp: removing: " + p );\r
80             }\r
81             getProcesses().remove( i );\r
82             return true;\r
83         }\r
84         return false;\r
85     }\r
86 \r
87     private synchronized int getProcessIndexById( final long id ) {\r
88         for( int i = 0; i < size(); ++i ) {\r
89             if ( getProcesses().get( i ).getId() == id ) {\r
90                 return i;\r
91             }\r
92         }\r
93         return -1;\r
94     }\r
95 \r
96     private synchronized List<ProcessRunning> getProcesses() {\r
97         return _processes;\r
98     }\r
99 }\r