Mercurial > hg > blitz_stable
comparison src/org/dancres/blitz/stats/SwitchSettings.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 package org.dancres.blitz.stats; | |
2 | |
3 import java.util.LinkedList; | |
4 import java.util.ArrayList; | |
5 import java.util.Iterator; | |
6 | |
7 import java.util.logging.Level; | |
8 | |
9 import net.jini.config.ConfigurationException; | |
10 | |
11 import org.dancres.blitz.config.ConfigurationFactory; | |
12 | |
13 /** | |
14 This class tracks currently set profiling switches and keeps listener's | |
15 up-to-date on changes in switch state. | |
16 | |
17 @todo Load default switch settings from configuration | |
18 */ | |
19 public class SwitchSettings { | |
20 private static final String STATS_SWITCHES = "stats"; | |
21 | |
22 private static SwitchSettings theSettings = new SwitchSettings(); | |
23 | |
24 private ArrayList theSwitches = new ArrayList(); | |
25 private LinkedList theListeners = new LinkedList(); | |
26 | |
27 SwitchSettings() { | |
28 try { | |
29 Switch[] mySwitches = | |
30 (Switch[]) ConfigurationFactory.getEntry(STATS_SWITCHES, | |
31 Switch[].class, | |
32 new Switch[0]); | |
33 | |
34 if (mySwitches.length != 0) { | |
35 for (int i = 0; i < mySwitches.length; i++) { | |
36 theSwitches.add(mySwitches[i]); | |
37 } | |
38 } | |
39 } catch (ConfigurationException aCE) { | |
40 StatsBoard.theLogger.log(Level.SEVERE, "Failed to load switch settings for statistics tracking", aCE); | |
41 } | |
42 } | |
43 | |
44 public static SwitchSettings get() { | |
45 return theSettings; | |
46 } | |
47 | |
48 public void add(SwitchListener aListener) { | |
49 boolean wasAdded = false; | |
50 | |
51 synchronized(theListeners) { | |
52 if (! theListeners.contains(aListener)) { | |
53 theListeners.add(aListener); | |
54 wasAdded = true; | |
55 } | |
56 } | |
57 | |
58 // If we added a listener - make it aware of current switch settings | |
59 if (wasAdded) | |
60 refresh(aListener); | |
61 } | |
62 | |
63 public void remove(SwitchListener aListener) { | |
64 synchronized(theListeners) { | |
65 if (theListeners.contains(aListener)) | |
66 theListeners.remove(aListener); | |
67 } | |
68 } | |
69 | |
70 /** | |
71 Update the settings of a particular switch | |
72 */ | |
73 public void update(Switch aSwitch) { | |
74 synchronized (theSwitches) { | |
75 int myPos = theSwitches.indexOf(aSwitch); | |
76 if (myPos != -1) { | |
77 theSwitches.remove(myPos); | |
78 } | |
79 | |
80 theSwitches.add(aSwitch); | |
81 } | |
82 | |
83 synchronized(theListeners) { | |
84 Iterator myListeners = theListeners.iterator(); | |
85 | |
86 while (myListeners.hasNext()) { | |
87 SwitchListener myListener = | |
88 (SwitchListener) myListeners.next(); | |
89 | |
90 myListener.switchFlipped(aSwitch); | |
91 } | |
92 } | |
93 } | |
94 | |
95 /** | |
96 Update settings based on the passed array of switches | |
97 */ | |
98 public void update(Switch[] aListOfSwitches) { | |
99 for (int i = 0; i < aListOfSwitches.length; i++) { | |
100 update(aListOfSwitches[i]); | |
101 } | |
102 } | |
103 | |
104 /** | |
105 Notify a switch listener of all currently set switches | |
106 */ | |
107 private synchronized void refresh(SwitchListener aListener) { | |
108 synchronized(theSwitches) { | |
109 Iterator mySwitches = theSwitches.iterator(); | |
110 | |
111 while (mySwitches.hasNext()) { | |
112 Switch mySwitch = (Switch) mySwitches.next(); | |
113 | |
114 aListener.switchFlipped(mySwitch); | |
115 } | |
116 } | |
117 } | |
118 } |