0c4ed99fb1d18ecc456158079bcc58da97ed207b
[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: www.phylosoft.org/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     final static private ArrayList<ProcessRunning> _processes = new ArrayList<ProcessRunning>();\r
34 \r
35     public synchronized ProcessRunning getProcessByIndex( final int i ) {\r
36         return getProcesses().get( i );\r
37     }\r
38 \r
39     public synchronized int size() {\r
40         return getProcesses().size();\r
41     }\r
42 \r
43     public synchronized ProcessRunning getProcessById( final int id ) {\r
44         for( final ProcessRunning p : getProcesses() ) {\r
45             if ( p.getId() == id ) {\r
46                 return p;\r
47             }\r
48         }\r
49         return null;\r
50     }\r
51 \r
52     public synchronized int addProcess( final String name ) {\r
53         final ProcessRunning p = ProcessRunning.createInstance( name );\r
54         final int id = p.getId();\r
55         if ( getProcessById( id ) != null ) {\r
56             throw new IllegalStateException( "process with id " + id + "already exists" );\r
57         }\r
58         getProcesses().add( p );\r
59         return id;\r
60     }\r
61 \r
62     public synchronized boolean removeProcess( final int id ) {\r
63         final int i = getProcessIndexById( id );\r
64         if ( i >= 0 ) {\r
65             getProcesses().remove( i );\r
66             return true;\r
67         }\r
68         return false;\r
69     }\r
70 \r
71     private synchronized int getProcessIndexById( final int id ) {\r
72         final ProcessRunning p = getProcessById( id );\r
73         if ( p != null ) {\r
74             return p.getId();\r
75         }\r
76         return -1;\r
77     }\r
78 \r
79     private synchronized List<ProcessRunning> getProcesses() {\r
80         return _processes;\r
81     }\r
82 }\r