From 3dcbd870439b3ef8dc9482679ac95906cdc3d396 Mon Sep 17 00:00:00 2001 From: Emil Forslund Date: Mon, 23 Nov 2015 10:03:29 -0800 Subject: [PATCH] Change from map to set in PluginDataProperty --- .../internal/core/config/PluginDataImpl.java | 1 + .../gui/config/PluginDataProperty.java | 47 +++++++++---------- 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/speedment/internal/core/config/PluginDataImpl.java b/src/main/java/com/speedment/internal/core/config/PluginDataImpl.java index 62531d07f6..78a7318b4e 100644 --- a/src/main/java/com/speedment/internal/core/config/PluginDataImpl.java +++ b/src/main/java/com/speedment/internal/core/config/PluginDataImpl.java @@ -102,6 +102,7 @@ public > T find(Class childClass, String name) th } @Override + @SuppressWarnings("unchecked") public Optional> add(Child child) { final ChildHolder> holder = children.computeIfAbsent( child.getInterfaceMainClass(), diff --git a/src/main/java/com/speedment/internal/gui/config/PluginDataProperty.java b/src/main/java/com/speedment/internal/gui/config/PluginDataProperty.java index f5865d692f..851228a4de 100644 --- a/src/main/java/com/speedment/internal/gui/config/PluginDataProperty.java +++ b/src/main/java/com/speedment/internal/gui/config/PluginDataProperty.java @@ -17,22 +17,23 @@ package com.speedment.internal.gui.config; import com.speedment.Speedment; -import com.speedment.config.Node; import com.speedment.config.PluginData; import com.speedment.config.Project; import com.speedment.config.aspects.Child; import com.speedment.config.aspects.Parent; import com.speedment.exception.SpeedmentException; import com.speedment.internal.core.config.utils.ConfigUtil; -import com.speedment.stream.MapStream; import groovy.lang.Closure; +import static java.util.Collections.newSetFromMap; import java.util.Optional; import java.util.concurrent.ConcurrentSkipListMap; import java.util.stream.Stream; -import javafx.collections.ObservableMap; import static java.util.Objects.requireNonNull; -import static javafx.collections.FXCollections.observableMap; +import java.util.concurrent.ConcurrentHashMap; +import static java.util.stream.Collectors.toCollection; +import static javafx.collections.FXCollections.observableSet; +import javafx.collections.ObservableSet; /** * @@ -40,26 +41,26 @@ */ public final class PluginDataProperty extends AbstractParentProperty> implements PluginData, ChildHelper { - private final ObservableMap> children; + private final ObservableSet> children; private Project parent; public PluginDataProperty(Speedment speedment) { super(speedment); - children = observableMap(new ConcurrentSkipListMap<>()); + children = observableSet(newSetFromMap(new ConcurrentSkipListMap<>())); } public PluginDataProperty(Speedment speedment, PluginData prototype) { super(speedment, prototype); - children = observableMap(MapStream - .fromValues(prototype.stream() + children = observableSet( + prototype.stream() .map(child -> { @SuppressWarnings("unchecked") final Child newChild = (Child) child; newChild.setParent(this); return newChild; }) - , Node::getName).toConcurrentNavigableMap() + .collect(toCollection(() -> newSetFromMap(new ConcurrentHashMap<>()))) ); } @@ -92,21 +93,20 @@ public Child metadata(Closure c) { )) .newChildToPluginData(c, this); - children.put(child.getName(), child); + children.add(child); return child; }); } @Override public Optional> add(Child child) { - return Optional.ofNullable( - children.put(child.getName(), child) - ); + requireNonNull(child); + return children.add(child) ? Optional.empty() : Optional.of(child); } @Override public Stream> stream() { - return MapStream.of(children).values(); + return children.stream(); } @Override @@ -128,22 +128,19 @@ public int countOf(Class> childType) { } @Override - @SuppressWarnings("unchecked") public > T find(Class childType, String name) throws SpeedmentException { requireNonNull(childType); requireNonNull(name); - final Child child = children.get(name); - if (child != null) { - if (child.getInterfaceMainClass().isAssignableFrom(childType)) { - @SuppressWarnings("unchecked") - final T node = (T) child; - return node; - } else { - throw wrongChildTypeException(childType); - } + final Child child = children.stream().filter(c -> name.equals(c.getName())) + .findAny().orElseThrow(() -> noChildWithNameException(childType, name)); + + if (child.getInterfaceMainClass().isAssignableFrom(childType)) { + @SuppressWarnings("unchecked") + final T node = (T) child; + return node; } else { - throw noChildWithNameException(childType, name); + throw wrongChildTypeException(childType); } } } \ No newline at end of file