-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockFactoryManagerImpl.java
More file actions
48 lines (44 loc) · 1.4 KB
/
StockFactoryManagerImpl.java
File metadata and controls
48 lines (44 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import java.rmi.server.UnicastRemoteObject;
import java.rmi.RemoteException;
import java.util.Map;
import java.util.HashMap;
import java.util.Collection;
import java.util.Iterator;
public class StockFactoryManagerImpl extends UnicastRemoteObject implements StockFactoryManager
{
public Map<StockFactory, Integer> factories;
public StockFactoryManagerImpl ( ) throws RemoteException
{
this.factories = new HashMap<StockFactory, Integer>();
}
public synchronized void addFactory( StockFactory f ) throws RemoteException
{
this.factories.put( f, 0 );
}
public synchronized Stock create ( ) throws RemoteException
{
Stock res;
Iterator< StockFactory > it = this.factories.keySet().iterator();
StockFactory lower = (StockFactory)it.next();
if( lower != null )
{
int min = this.factories.get( lower );
StockFactory nextFact = null;
while(it.hasNext())
{
nextFact = it.next();
int value = this.factories.get( nextFact );
if( value <= min )
{
min = value;
lower = nextFact;
}
}
this.factories.put( nextFact, (min + 1) );
res = nextFact.create();
}
else
res = null;
return res;
}
}