annotate src/pathfinder/mergetest/channels/SelectorSimulator.java @ 164:5c458f1a7679

*** empty log message ***
author tkaito
date Thu, 28 Aug 2008 22:18:28 +0900
parents 66e9cebce3fa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
161
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
1 package pathfinder.mergetest.channels;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
2
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
3 import java.io.IOException;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
4 import java.util.ArrayList;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
5 //import java.util.Set; //書き直す?
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
6
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
7
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
8
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
9 public class SelectorSimulator<P> {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
10
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
11 private ArrayList<SelectionKeySimulator<P>> keyList;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
12 private ArrayList<SelectionKeySimulator<P>> selectedKeys;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
13
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
14 public SelectorSimulator() {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
15 // TODO Auto-generated constructor stub
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
16 keyList = new ArrayList<SelectionKeySimulator<P>>();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
17 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
18
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
19 public int select() throws IOException {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
20 selectedKeys = new ArrayList<SelectionKeySimulator<P>>();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
21
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
22 synchronized(this) {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
23
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
24 while(selectedKeys.isEmpty()){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
25 for(SelectionKeySimulator<P> key : keyList){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
26 if(key.isAble())
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
27 selectedKeys.add(key);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
28 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
29
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
30 if(selectedKeys.isEmpty())
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
31 try {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
32 this.wait();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
33 } catch (InterruptedException e) {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
34 throw new IOException("Error, Selector was interrupted!");
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
35 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
36 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
37 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
38 return selectedKeys.size();
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
39 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
40
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
41 public SelectionKeySimulator<P> register(SelectableChannelSimulator<P> cs, int opt){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
42 SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
43 keyList.add(key);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
44 return key;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
45 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
46
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
47 public SelectionKeySimulator<P> register(ChannelSimulator<P> cs, int opt, Object handler){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
48 SelectionKeySimulator<P> key = new SelectionKeySimulator<P>(cs, opt);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
49 key.attach(handler);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
50 keyList.add(key);
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
51 return key;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
52 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
53
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
54 public ArrayList<SelectionKeySimulator<P>> selectedKeys() {
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
55
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
56 return selectedKeys;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
57 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
58
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
59 public SelectionKeySimulator<P> getKey(ChannelSimulator<P> channel){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
60 for(SelectionKeySimulator<P> key : keyList){
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
61 if(key.channel() == channel)
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
62 return key;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
63 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
64 return null;
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
65 }
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
66
66e9cebce3fa move from pathfinder.simulator.channels.*
kent
parents:
diff changeset
67 }