1
2
3
4
5 package org.epics.pvmanager;
6
7 import java.util.Collection;
8 import java.util.HashMap;
9 import java.util.HashSet;
10 import java.util.Map;
11 import java.util.Set;
12
13
14
15
16
17 class WritePlanner {
18
19 private Map<String, ChannelHandler> channels = new HashMap<String, ChannelHandler>();
20 private Map<String, Object> values = new HashMap<String, Object>();
21 private Map<String, Set<String>> preceding = new HashMap<String, Set<String>>();
22 private Map<String, Set<String>> succeeding = new HashMap<String, Set<String>>();
23 private Set<String> leafs = new HashSet<String>();
24
25 void addChannel(ChannelHandler channel, Object value, Collection<String> precedingChannels) {
26 channels.put(channel.getChannelName(), channel);
27 values.put(channel.getChannelName(), value);
28 preceding.put(channel.getChannelName(), new HashSet<String>(precedingChannels));
29 for (String precedingChannel : precedingChannels) {
30 Set<String> succeedingChannels = succeeding.get(precedingChannel);
31 if (succeedingChannels == null) {
32 succeedingChannels = new HashSet<String>();
33 succeeding.put(precedingChannel, succeedingChannels);
34 }
35 succeedingChannels.add(channel.getChannelName());
36 }
37 if (precedingChannels.isEmpty()) {
38 leafs.add(channel.getChannelName());
39 }
40 }
41
42 void removeChannel(String channelName) {
43 channels.remove(channelName);
44 values.remove(channelName);
45 preceding.remove(channelName);
46 Set<String> succeedingChannels = succeeding.remove(channelName);
47 if (succeedingChannels != null) {
48 for (String succeedingChannel : succeedingChannels) {
49 Set<String> precedingChannels = preceding.get(succeedingChannel);
50 precedingChannels.remove(channelName);
51 if (precedingChannels.isEmpty())
52 leafs.add(succeedingChannel);
53 }
54 }
55 }
56
57 Map<ChannelHandler, Object> nextChannels() {
58 Map<ChannelHandler, Object> nextChannels = new HashMap<ChannelHandler, Object>();
59 for (String channelName : leafs) {
60 nextChannels.put(channels.get(channelName), values.get(channelName));
61 }
62 leafs.clear();
63 return nextChannels;
64 }
65
66 boolean isDone() {
67 return channels.isEmpty();
68 }
69
70 }