import static com.google.common.base.Preconditions.checkState;
+import java.util.LinkedHashMap;
import java.util.Map;
import org.opendaylight.yangtools.concepts.Identifiable;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodeContainer;
import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableMap;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Iterables;
import com.google.common.primitives.UnsignedLong;
public class StoreMetadataNode implements Immutable, Identifiable<PathArgument>, StoreTreeNode<StoreMetadataNode> {
private final Map<PathArgument, StoreMetadataNode> children;
+ /**
+ *
+ * @param data
+ * @param nodeVersion
+ * @param subtreeVersion
+ * @param children Map of children, must not be modified externally
+ */
protected StoreMetadataNode(final NormalizedNode<?, ?> data, final UnsignedLong nodeVersion,
final UnsignedLong subtreeVersion, final Map<PathArgument, StoreMetadataNode> children) {
this.nodeVersion = nodeVersion;
this.subtreeVersion = subtreeVersion;
this.data = data;
- this.children = ImmutableMap.copyOf(children);
-
+ this.children = Preconditions.checkNotNull(children);
}
public static Builder builder() {
}
public Iterable<StoreMetadataNode> getChildren() {
- return children.values();
+ return Iterables.unmodifiableIterable(children.values());
}
@Override
return Optional.absent();
}
- public static final StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node,
+ public static final StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node,
final UnsignedLong nodeVersion, final UnsignedLong subtreeVersion) {
Builder builder = builder() //
.setNodeVersion(nodeVersion) //
@SuppressWarnings("unchecked")
NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>> nodeContainer = (NormalizedNodeContainer<?, ?, NormalizedNode<?, ?>>) node;
for (NormalizedNode<?, ?> subNode : nodeContainer.getValue()) {
- builder.add(createRecursivelly(subNode, nodeVersion, subtreeVersion));
+ builder.add(createRecursively(subNode, nodeVersion, subtreeVersion));
}
}
return builder.build();
private UnsignedLong nodeVersion;
private UnsignedLong subtreeVersion;
private NormalizedNode<?, ?> data;
- private final ImmutableMap.Builder<PathArgument, StoreMetadataNode> children = ImmutableMap.builder();
+ private Map<PathArgument, StoreMetadataNode> children = new LinkedHashMap<>();
+ private boolean dirty = false;
private Builder() {}
}
public Builder add(final StoreMetadataNode node) {
+ if (dirty) {
+ children = new LinkedHashMap<>(children);
+ dirty = false;
+ }
children.put(node.getIdentifier(), node);
return this;
}
checkState(data != null, "Data node should not be null.");
checkState(subtreeVersion.compareTo(nodeVersion) >= 0,
"Subtree version must be equals or greater than node version.");
- return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children.build());
+ dirty = true;
+ return new StoreMetadataNode(data, nodeVersion, subtreeVersion, children);
}
}
- public static StoreMetadataNode createRecursivelly(final NormalizedNode<?, ?> node, final UnsignedLong version) {
- return createRecursivelly(node, version, version);
+ public static StoreMetadataNode createRecursively(final NormalizedNode<?, ?> node, final UnsignedLong version) {
+ return createRecursively(node, version, version);
}
}