|
| 1 | +package org.codefx.libfx.control; |
| 2 | + |
| 3 | +import java.util.function.Consumer; |
| 4 | + |
| 5 | +import javafx.collections.FXCollections; |
| 6 | +import javafx.collections.ObservableMap; |
| 7 | + |
| 8 | +import org.codefx.libfx.control.properties.ControlProperties; |
| 9 | +import org.codefx.libfx.control.properties.ControlPropertyListenerHandle; |
| 10 | + |
| 11 | +/** |
| 12 | + * Demonstrates how to use the {@link ControlPropertyListenerHandle} and its builder. |
| 13 | + */ |
| 14 | +@SuppressWarnings("static-method") |
| 15 | +public class ControlPropertyListenerDemo { |
| 16 | + |
| 17 | + // #region CONSTRUCTION & MAIN |
| 18 | + |
| 19 | + /** |
| 20 | + * Creates a new demo. |
| 21 | + */ |
| 22 | + private ControlPropertyListenerDemo() { |
| 23 | + // nothing to do |
| 24 | + } |
| 25 | + |
| 26 | + /** |
| 27 | + * Runs this demo. |
| 28 | + * |
| 29 | + * @param args |
| 30 | + * command line arguments (will not be used) |
| 31 | + */ |
| 32 | + public static void main(String[] args) { |
| 33 | + ControlPropertyListenerDemo demo = new ControlPropertyListenerDemo(); |
| 34 | + |
| 35 | + demo.simpleCase(); |
| 36 | + demo.attachAndDetach(); |
| 37 | + |
| 38 | + demo.timeNoTypeCheck(); |
| 39 | + demo.timeWithTypeCheck(); |
| 40 | + |
| 41 | + demo.castVsTypeChecking(); |
| 42 | + } |
| 43 | + |
| 44 | + // #end CONSTRUCTION & MAIN |
| 45 | + |
| 46 | + // #region DEMOS |
| 47 | + |
| 48 | + /** |
| 49 | + * Demonstrates the simple case, in which a value processor is added for some key. |
| 50 | + */ |
| 51 | + private void simpleCase() { |
| 52 | + ObservableMap<Object, Object> properties = FXCollections.observableHashMap(); |
| 53 | + |
| 54 | + // build and attach the listener |
| 55 | + ControlProperties.<String> on(properties) |
| 56 | + .forKey("Key") |
| 57 | + .processValue(value -> System.out.println(" -> " + value)) |
| 58 | + .buildAttached(); |
| 59 | + |
| 60 | + // set values of the correct type for the correct key |
| 61 | + System.out.print("Set \"Value\" for the correct key for the first time: "); |
| 62 | + properties.put("Key", "Value"); |
| 63 | + System.out.print("Set \"Value\" for the correct key for the second time: "); |
| 64 | + properties.put("Key", "Value"); |
| 65 | + |
| 66 | + // set values of the wrong type: |
| 67 | + System.out.println("Set an Integer for the correct key: ... (nothing will happen)"); |
| 68 | + properties.put("Key", 5); |
| 69 | + |
| 70 | + // set values for the wrong key |
| 71 | + System.out.println("Set \"Value\" for another key: ... (nothing will happen)"); |
| 72 | + properties.put("OtherKey", "Value"); |
| 73 | + |
| 74 | + System.out.println(); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Demonstrates how a listener can be attached and detached. |
| 79 | + */ |
| 80 | + private void attachAndDetach() { |
| 81 | + ObservableMap<Object, Object> properties = FXCollections.observableHashMap(); |
| 82 | + |
| 83 | + // build the listener (but don't attach it yet) and assign it to a variable |
| 84 | + ControlPropertyListenerHandle listener = ControlProperties.<String> on(properties) |
| 85 | + .forKey("Key") |
| 86 | + .processValue(value -> System.out.println(" -> " + value)) |
| 87 | + .buildDetached(); |
| 88 | + |
| 89 | + // set a value when the listener is not yet attached |
| 90 | + System.out.println( |
| 91 | + "Set \"ExistingValue\" before attaching the listener: ... (nothing will happen)"); |
| 92 | + properties.put("Key", "ExistingValue"); |
| 93 | + |
| 94 | + // now attach the listener |
| 95 | + System.out.print("When the listener is set, \"ExistingValue\" is processed and removed: "); |
| 96 | + listener.attach(); |
| 97 | + |
| 98 | + System.out.print("Set \"Value\": "); |
| 99 | + properties.put("Key", "Value"); |
| 100 | + |
| 101 | + // detach the listener |
| 102 | + listener.detach(); |
| 103 | + System.out.println("Set \"UnnoticedValue\" when the listener is detached: ... (nothing will happen)"); |
| 104 | + |
| 105 | + System.out.println(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Measures the time it takes to get a lot of {@link ClassCastException}. |
| 110 | + */ |
| 111 | + private void timeNoTypeCheck() { |
| 112 | + ObservableMap<Object, Object> properties = FXCollections.observableHashMap(); |
| 113 | + |
| 114 | + Consumer<String> unreached = value -> { |
| 115 | + throw new RuntimeException("Should not be executed!"); |
| 116 | + }; |
| 117 | + |
| 118 | + // build and a attach a listener which does no type check before cast |
| 119 | + ControlProperties.<String> on(properties) |
| 120 | + .forKey("Key") |
| 121 | + .processValue(unreached) |
| 122 | + .buildAttached(); |
| 123 | + |
| 124 | + // add a couple of values of the wrong type to average the time that takes |
| 125 | + Integer valueOfWrongType = 5; |
| 126 | + int runs = (int) 1e5; |
| 127 | + long startTimeInNS = System.nanoTime(); |
| 128 | + |
| 129 | + for (int i = 0; i < runs; i++) |
| 130 | + properties.put("Key", valueOfWrongType); |
| 131 | + |
| 132 | + long endTimeInNS = System.nanoTime(); |
| 133 | + long timePerRunInNS = (endTimeInNS - startTimeInNS) / runs; |
| 134 | + System.out.println("For unchecked casts, adding a value of the wrong type takes ~" + timePerRunInNS + " ns."); |
| 135 | + |
| 136 | + System.out.println(); |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Demonstrates how type checking increases performance if values of an incorrect type are added frequently. |
| 141 | + */ |
| 142 | + private void timeWithTypeCheck() { |
| 143 | + ObservableMap<Object, Object> properties = FXCollections.observableHashMap(); |
| 144 | + |
| 145 | + Consumer<String> unreached = value -> { |
| 146 | + throw new RuntimeException("Should not be executed!"); |
| 147 | + }; |
| 148 | + |
| 149 | + // build and a attach a listener which does a type check before cast |
| 150 | + ControlProperties.<String> on(properties) |
| 151 | + .forKey("Key") |
| 152 | + .forValueType(String.class) |
| 153 | + .processValue(unreached) |
| 154 | + .buildAttached(); |
| 155 | + |
| 156 | + // add a couple of values of the wrong type to average the time that takes |
| 157 | + Integer valueOfWrongType = 5; |
| 158 | + int runs = (int) 1e5; |
| 159 | + long startTimeInNS = System.nanoTime(); |
| 160 | + |
| 161 | + for (int i = 0; i < runs; i++) |
| 162 | + properties.put("Key", valueOfWrongType); |
| 163 | + |
| 164 | + long endTimeInNS = System.nanoTime(); |
| 165 | + long timePerRunInNS = (endTimeInNS - startTimeInNS) / runs; |
| 166 | + System.out.println("For checked casts, adding a value of the wrong type takes ~" + timePerRunInNS + " ns."); |
| 167 | + |
| 168 | + System.out.println(); |
| 169 | + } |
| 170 | + |
| 171 | + // #end DEMOS |
| 172 | + |
| 173 | + /** |
| 174 | + * TODO (nipa): I don't get it. The simple test below clearly shows that raising an exception takes about 6.000 ns. |
| 175 | + * So why the hell does {@link #timeNoTypeCheck()} run way faster than that?! |
| 176 | + * <p> |
| 177 | + * Some days later: I ran this again and discovered that the time difference is now very measurable and looks |
| 178 | + * correct. Perhaps some JVM optimization because I ran it so often? |
| 179 | + */ |
| 180 | + private void castVsTypeChecking() { |
| 181 | + int runs = (int) 1e5; |
| 182 | + Object integer = 3; |
| 183 | + |
| 184 | + // CAST |
| 185 | + long start = System.nanoTime(); |
| 186 | + for (int i = 0; i < runs; i++) |
| 187 | + try { |
| 188 | + String string = (String) integer; |
| 189 | + System.out.println(string); |
| 190 | + } catch (ClassCastException e) { |
| 191 | + // do nothing |
| 192 | + } |
| 193 | + long end = System.nanoTime(); |
| 194 | + System.out.println("Each unchecked cast took ~" + (end - start) / runs + " ns."); |
| 195 | + |
| 196 | + // TYPE CHECK |
| 197 | + start = System.nanoTime(); |
| 198 | + for (int i = 0; i < runs; i++) |
| 199 | + if (String.class.isInstance(integer)) { |
| 200 | + String bar = (String) integer; |
| 201 | + System.out.println(bar); |
| 202 | + } |
| 203 | + end = System.nanoTime(); |
| 204 | + System.out.println("Each type check took ~" + (end - start) / runs + " ns."); |
| 205 | + } |
| 206 | +} |
0 commit comments