@Override public ConfigurationNode createEmptyNode(ConfigurationOptions options) { return SimpleConfigurationNode.root(options); } }
@Override public SimpleCommentedConfigurationNode getNode(Object... path) { return (SimpleCommentedConfigurationNode)super.getNode(path); }
@Override public <T> T getValue(@NonNull Function<Object, T> transformer, T def) { T ret = transformer.apply(getValue()); return ret == null ? storeDefault(def) : ret; }
SimpleConfigurationNode getParentAttached() { SimpleConfigurationNode parent = this.parent; if (parent.isVirtual()) { parent = parent.getParentAttached().attachChildIfAbsent(parent); } return this.parent = parent; }
protected void attachIfNecessary() { if (!attached) { getParentEnsureAttached().attachChild(this); } }
protected void attachIfNecessary() { if (!attached) { getParentAttached().attachChild(this); } }
@Test public void testGetSetValueSerialized() throws ObjectMappingException { SimpleConfigurationNode subject = SimpleConfigurationNode.root(); subject.setValue("48"); assertEquals((Object) 48, subject.getValue(TypeToken.of(Integer.class))); UUID testId = UUID.randomUUID(); subject.setValue(TypeToken.of(UUID.class), testId); assertEquals(testId.toString(), subject.getValue()); }
@Test public void testBooleanSerializer() throws ObjectMappingException { final TypeToken<Boolean> booleanType = TypeToken.of(Boolean.class); final TypeSerializer<Boolean> booleanSerializer = SERIALIZERS.get(booleanType); SimpleConfigurationNode node = SimpleConfigurationNode.root(); node.getNode("direct").setValue(true); node.getNode("fromstring").setValue("true"); assertEquals(true, booleanSerializer.deserialize(booleanType, node.getNode("direct"))); assertEquals(true, booleanSerializer.deserialize(booleanType, node.getNode("fromstring"))); }
@Test public void testMergeValues() { SimpleConfigurationNode first = SimpleConfigurationNode.root(); SimpleConfigurationNode second = SimpleConfigurationNode.root(); first.getNode("scalar").setValue("one"); first.getNode("absent").setValue("butmerged"); second.getNode("scalar").setValue("two"); ConfigurationNode firstAbsentMap = first.getNode("absent-map"); firstAbsentMap.getNode("a").setValue("one"); firstAbsentMap.getNode("b").setValue("two"); ConfigurationNode firstMergedMap = first.getNode("merged-map"); ConfigurationNode secondMergedMap = second.getNode("merged-map"); firstMergedMap.getNode("source").setValue("first"); secondMergedMap.getNode("source").setValue("second"); firstMergedMap.getNode("first-only").setValue("yeah"); secondMergedMap.getNode("second-only").setValue("yeah"); second.mergeValuesFrom(first); assertEquals("two", second.getNode("scalar").getString()); assertEquals("butmerged", second.getNode("absent").getString()); assertEquals("one", second.getNode("absent-map", "a").getString()); assertEquals("two", second.getNode("absent-map", "b").getString()); assertEquals("second", second.getNode("merged-map", "source").getString()); assertEquals("yeah", second.getNode("merged-map", "first-only").getString()); assertEquals("yeah", second.getNode("merged-map", "second-only").getString()); }
@Test public void testDefaultsCopied() { SimpleConfigurationNode subject = SimpleConfigurationNode.root(ConfigurationOptions.defaults().setShouldCopyDefaults(true)); assertNull(subject.getValue()); assertEquals("default value", subject.getValue("default value")); assertEquals("default value", subject.getValue()); }
@Test public void testTraversingNodeCreation() { SimpleConfigurationNode config = SimpleConfigurationNode.root(); SimpleConfigurationNode nodeOne = config.getNode("uncreated", "step", "node"); SimpleConfigurationNode nodeTwo = config.getNode("uncreated", "step", "color"); nodeOne.setValue("one"); nodeTwo.setValue("lilac"); ConfigurationNode attachedParent = config.getNode("uncreated", "step"); assertEquals(attachedParent, nodeOne.getParentEnsureAttached()); assertEquals(attachedParent, nodeTwo.getParentEnsureAttached()); }
SimpleConfigurationNode newChild = createNode(ent.getKey()); newChild.attached = true; newChild.setValue(ent.getValue()); SimpleConfigurationNode existing = newValue.putChildIfAbsent(ent.getKey(), newChild); if (existing != null) { existing.mergeValuesFrom(newChild); insertNewValue(other.getValue(), true);
ListConfigValue(SimpleConfigurationNode holder, Object startValue) { super(holder); SimpleConfigurationNode child = holder.createNode(0); child.attached = true; child.setValue(startValue); this.values.get().add(child); }
@Test public void testSettingMultipleTimesWorks() { SimpleConfigurationNode subject = SimpleConfigurationNode.root(); subject.setValue(ImmutableMap.of("a", "b", "b", "c", "c", "d")); assertTrue(subject.hasMapChildren()); subject.setValue(ImmutableMap.of("na", "na", "eh", "eh", "bleugh", "bleugh")); assertTrue(subject.hasMapChildren()); }
@Test public void testStringSerializer() throws ObjectMappingException { final TypeToken<String> stringType = TypeToken.of(String.class); final TypeSerializer<String> stringSerializer = SERIALIZERS.get(stringType); final ConfigurationNode node = SimpleConfigurationNode.root().setValue("foobar"); assertEquals("foobar", stringSerializer.deserialize(stringType, node)); stringSerializer.serialize(stringType, "foobarbaz", node); assertEquals("foobarbaz", node.getString()); }
protected SimpleConfigurationNode getChild(Object key, boolean attach) { SimpleConfigurationNode child = value.getChild(key); if (child == null) { // Does not currently exist! if (attach) { attachIfNecessary(); SimpleConfigurationNode existingChild = value.putChildIfAbsent(key, (child = createNode(key))); if (existingChild != null) { child = existingChild; } else { attachChild(child); } } else { child = createNode(key); } } return child; }
private <T> T calculateDef(T defValue) { if (defValue != null && getOptions().shouldCopyDefaults()) { setValue(defValue); } return defValue; }
/** * The same as {@link #getParent()} - but ensuring that 'parent' is attached via * {@link #attachChildIfAbsent(SimpleConfigurationNode)}. * * @return The parent */ SimpleConfigurationNode getParentEnsureAttached() { SimpleConfigurationNode parent = this.parent; if (parent.isVirtual()) { parent = parent.getParentEnsureAttached().attachChildIfAbsent(parent); } return this.parent = parent; }
@Override @SuppressWarnings("unchecked") public <T> T getValue(@NonNull TypeToken<T> type, T def) throws ObjectMappingException { Object value = getValue(); if (value == null) { return storeDefault(def); } TypeSerializer serial = getOptions().getSerializers().get(type); if (serial == null) { if (type.getRawType().isInstance(value)) { return (T) type.getRawType().cast(value); } else { return storeDefault(def); } } return (T) serial.deserialize(type, this); }
@Override public <T> T getValue(Function<Object, T> transformer, T def) { T ret = transformer.apply(getValue()); return ret == null ? calculateDef(def) : ret; }