PreferencesUtils { public static <T> void read(Property<? super T> property, Preferences preferences, Function<String, ? extends T> parser) { String name = property.getName(); String saved = preferences.get(name, null); if (saved != null) { try { property.setValue(parser.apply(saved)); } catch (Exception ignore) { } } } private  PreferencesUtils(); static void save(Property<? extends T> property,
                              Preferences preferences,
                              Function<? super T, String> serializer); static void save(IntegerProperty property, Preferences preferences); static void save(LongProperty property, Preferences preferences); static void save(DoubleProperty property, Preferences preferences); static void save(BooleanProperty property, Preferences preferences); static void save(StringProperty property, Preferences preferences); static void read(Property<? super T> property,
                              Preferences preferences,
                              Function<String, ? extends T> parser); static void read(IntegerProperty property, Preferences preferences); static void read(LongProperty property, Preferences preferences); static void read(DoubleProperty property, Preferences preferences); static void read(BooleanProperty property, Preferences preferences); static void read(StringProperty property, Preferences preferences);  }
@Test public void testReadInt() { String name = "int"; int value = Integer.MIN_VALUE; IntegerProperty property = new SimpleIntegerProperty(null, name, -value); preferences.putInt(name, value); PreferencesUtils.read(property, preferences); assertEquals(property.getValue().intValue(), value); }
@Test public void testReadLong() { String name = "long"; long value = Long.MIN_VALUE; LongProperty property = new SimpleLongProperty(null, name, -value); preferences.putLong(name, value); PreferencesUtils.read(property, preferences); assertEquals(property.getValue().longValue(), value); }
@Test public void testReadDouble() { String name = "double"; double value = Double.MIN_VALUE; DoubleProperty property = new SimpleDoubleProperty(null, name, -value); preferences.putDouble(name, value); PreferencesUtils.read(property, preferences); assertEquals(property.getValue().doubleValue(), value); }
@Test public void testReadBoolean() { String name = "boolean"; boolean value = true; BooleanProperty property = new SimpleBooleanProperty(null, name, !value); preferences.putBoolean(name, value); PreferencesUtils.read(property, preferences); assertEquals(property.getValue(), value); }
@Test public void testReadString() { String name = "string"; String value = "foo"; StringProperty property = new SimpleStringProperty(null, name, "bar"); preferences.put(name, value); PreferencesUtils.read(property, preferences); assertEquals(property.getValue(), value); }
@Test public void testReadWhenSavedValueIsWrongType() { int initialValue = 10; IntegerProperty property = new SimpleIntegerProperty(null, "value", initialValue); preferences.putBoolean("value", false); PreferencesUtils.read(property, preferences); assertEquals(initialValue, property.getValue().intValue()); }
@Test public void testReadWhenNoValuePresent() { double initialValue = 123.456; DoubleProperty property = new SimpleDoubleProperty(null, "foo", initialValue); PreferencesUtils.read(property, preferences); assertEquals(initialValue, property.getValue().doubleValue()); }