import java.util.Map;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
static final class ImmutableLeafNode<T> extends AbstractImmutableNormalizedAttrNode<InstanceIdentifier.NodeIdentifier, T> implements LeafNode<T> {
- ImmutableLeafNode(InstanceIdentifier.NodeIdentifier nodeIdentifier, T value, Map<QName, String> attributes) {
+ ImmutableLeafNode(final InstanceIdentifier.NodeIdentifier nodeIdentifier, final T value, final Map<QName, String> attributes) {
super(nodeIdentifier, value, attributes);
}
-
}
}
import java.util.Map;
import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeAttrBuilder;
static final class ImmutableLeafSetEntryNode<T> extends AbstractImmutableNormalizedAttrNode<InstanceIdentifier.NodeWithValue, T> implements LeafSetEntryNode<T> {
- ImmutableLeafSetEntryNode(InstanceIdentifier.NodeWithValue nodeIdentifier, T value, Map<QName, String> attributes) {
+ ImmutableLeafSetEntryNode(final InstanceIdentifier.NodeWithValue nodeIdentifier, final T value, final Map<QName, String> attributes) {
super(nodeIdentifier, value, attributes);
Preconditions.checkArgument(nodeIdentifier.getValue().equals(value),
"Node identifier contains different value: %s than value itself: %s", nodeIdentifier, value);
}
-
}
}
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
public class ImmutableLeafSetNodeBuilder<T> implements ListNodeBuilder<T, LeafSetEntryNode<T>> {
@Override
public ListNodeBuilder<T, LeafSetEntryNode<T>> withValue(final List<LeafSetEntryNode<T>> value) {
- for (LeafSetEntryNode<T> leafSetEntry : value) {
+ for (final LeafSetEntryNode<T> leafSetEntry : value) {
withChild(leafSetEntry);
}
@Override
- public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, Map<QName, String> attributes) {
+ public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value, final Map<QName, String> attributes) {
return withChild(new ImmutableLeafSetEntryNodeBuilder.ImmutableLeafSetEntryNode<>(
new InstanceIdentifier.NodeWithValue(nodeIdentifier.getNodeType(), value), value, attributes));
}
@Override
- public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(T value) {
+ public ListNodeBuilder<T, LeafSetEntryNode<T>> withChildValue(final T value) {
return withChildValue(value, Collections.<QName,String>emptyMap());
}
return Optional.fromNullable(mappedChildren.get(child));
}
+ @Override
+ protected int valueHashCode() {
+ return mappedChildren.hashCode();
+ }
+
+ @Override
+ protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
+ return mappedChildren.equals(((ImmutableLeafSetNode<?>) other).mappedChildren);
+ }
}
@Override
// FIXME, find better solution than 2 maps (map from QName to Child ?)
@Override
- public DataContainerNodeAttrBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withValue(List<DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> value) {
- for (DataContainerChild<? extends InstanceIdentifier.PathArgument, ?> childId : value) {
- InstanceIdentifier.PathArgument identifier = childId.getIdentifier();
+ public DataContainerNodeAttrBuilder<InstanceIdentifier.NodeIdentifierWithPredicates, MapEntryNode> withValue(final List<DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> value) {
+ for (final DataContainerChild<? extends InstanceIdentifier.PathArgument, ?> childId : value) {
+ final InstanceIdentifier.PathArgument identifier = childId.getIdentifier();
// Augmentation nodes cannot be keys, and do not have to be present in childrenQNamesToPaths map
if(identifier instanceof InstanceIdentifier.AugmentationIdentifier) {
}
private void checkKeys() {
- for (QName keyQName : nodeIdentifier.getKeyValues().keySet()) {
+ for (final QName keyQName : nodeIdentifier.getKeyValues().keySet()) {
- InstanceIdentifier.PathArgument childNodePath = childrenQNamesToPaths.get(keyQName);
- DataContainerChild<?, ?> childNode = value.get(childNodePath);
+ final InstanceIdentifier.PathArgument childNodePath = childrenQNamesToPaths.get(keyQName);
+ final DataContainerChild<?, ?> childNode = value.get(childNodePath);
Preconditions.checkNotNull(childNode, "Key child node: %s, not present", keyQName);
- Object actualValue = nodeIdentifier.getKeyValues().get(keyQName);
- Object expectedValue = childNode.getValue();
+ final Object actualValue = nodeIdentifier.getKeyValues().get(keyQName);
+ final Object expectedValue = childNode.getValue();
Preconditions.checkArgument(expectedValue.equals(actualValue),
"Key child node with unexpected value, is: %s, should be: %s", actualValue, expectedValue);
}
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
public class ImmutableMapNodeBuilder
@Override
public CollectionNodeBuilder<MapEntryNode, MapNode> withValue(final List<MapEntryNode> value) {
// TODO replace or putAll ?
- for (MapEntryNode mapEntryNode : value) {
+ for (final MapEntryNode mapEntryNode : value) {
withChild(mapEntryNode);
}
return Optional.fromNullable(mappedChildren.get(child));
}
+ @Override
+ protected int valueHashCode() {
+ return mappedChildren.hashCode();
+ }
+
+ @Override
+ protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
+ return mappedChildren.equals(((ImmutableMapNode) other).mappedChildren);
+ }
}
}
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
+import com.google.common.base.Objects.ToStringHelper;
+
public abstract class AbstractImmutableDataContainerAttrNode<K extends InstanceIdentifier.PathArgument>
extends AbstractImmutableDataContainerNode<K>
implements AttributesContainer {
private final Map<QName, String> attributes;
public AbstractImmutableDataContainerAttrNode(
- Map<InstanceIdentifier.PathArgument, DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> children,
- K nodeIdentifier, Map<QName, String> attributes) {
+ final Map<InstanceIdentifier.PathArgument, DataContainerChild<? extends InstanceIdentifier.PathArgument, ?>> children,
+ final K nodeIdentifier, final Map<QName, String> attributes) {
super(children, nodeIdentifier);
this.attributes = attributes;
}
}
@Override
- public final Object getAttributeValue(QName value) {
+ public final Object getAttributeValue(final QName value) {
return attributes.get(value);
}
+
+ @Override
+ protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+ return super.addToStringAttributes(toStringHelper).add("attributes", attributes);
+ }
+
+// FIXME: are attributes part of hashCode/equals?
+// @Override
+// protected int valueHashCode() {
+// int result = super.valueHashCode();
+// for (final Entry<?, ?> a : attributes.entrySet()) {
+// result = 31 * result + a.hashCode();
+// }
+// return result;
+// }
+
+ // FIXME: are attributes part of hashCode/equals?
+// @Override
+// protected boolean valueEquals(final NormalizedNode<?, ?> other) {
+// if (!super.valueEquals(other)) {
+// return false;
+// }
+// final Set<Entry<QName, String>> tas = getAttributes().entrySet();
+// final Set<Entry<QName, String>> oas = container.getAttributes().entrySet();
+//
+// return tas.containsAll(oas) && oas.containsAll(tas);
+// return true;
+// }
}
import java.util.Map;
-import com.google.common.collect.ImmutableSet;
import org.opendaylight.yangtools.concepts.Immutable;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier.PathArgument;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
import org.opendaylight.yangtools.yang.data.api.schema.DataContainerNode;
import com.google.common.base.Optional;
-import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
public abstract class AbstractImmutableDataContainerNode<K extends PathArgument> //
extends AbstractImmutableNormalizedNode<K, Iterable<DataContainerChild<? extends PathArgument, ?>>> //
return Optional.<DataContainerChild<? extends PathArgument, ?>> fromNullable(children.get(child));
}
+ @Override
+ protected int valueHashCode() {
+ return children.hashCode();
+ }
+
+ @Override
+ protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
+ if (!(other instanceof AbstractImmutableDataContainerNode<?>)) {
+ return false;
+ }
+
+ return children.equals(((AbstractImmutableDataContainerNode<?>)other).children);
+ }
}
import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
+import com.google.common.base.Objects.ToStringHelper;
import com.google.common.collect.ImmutableMap;
public abstract class AbstractImmutableNormalizedAttrNode<K extends InstanceIdentifier.PathArgument,V>
private final Map<QName, String> attributes;
- protected AbstractImmutableNormalizedAttrNode(K nodeIdentifier, V value, Map<QName, String> attributes) {
+ protected AbstractImmutableNormalizedAttrNode(final K nodeIdentifier, final V value, final Map<QName, String> attributes) {
super(nodeIdentifier, value);
this.attributes = ImmutableMap.copyOf(attributes);
}
}
@Override
- public final Object getAttributeValue(QName value) {
+ public final Object getAttributeValue(final QName value) {
return attributes.get(value);
}
+ @Override
+ protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+ return super.addToStringAttributes(toStringHelper).add("attributes", attributes);
+ }
+
+ @Override
+ protected int valueHashCode() {
+ final int result = getValue().hashCode();
+// FIXME: are attributes part of hashCode/equals?
+// for (final Entry<?, ?> a : attributes.entrySet()) {
+// result = 31 * result + a.hashCode();
+// }
+ return result;
+ }
+
+ @Override
+ protected boolean valueEquals(final AbstractImmutableNormalizedNode<?, ?> other) {
+ if (!getValue().equals(other.getValue())) {
+ return false;
+ }
+
+// FIXME: are attributes part of hashCode/equals?
+// final Set<Entry<QName, String>> tas = getAttributes().entrySet();
+// final Set<Entry<QName, String>> oas = container.getAttributes().entrySet();
+//
+// return tas.containsAll(oas) && oas.containsAll(tas);
+ return true;
+ }
}
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
import com.google.common.base.Objects;
+import com.google.common.base.Objects.ToStringHelper;
import com.google.common.base.Preconditions;
public abstract class AbstractImmutableNormalizedNode<K extends InstanceIdentifier.PathArgument,V>
implements NormalizedNode<K, V>, Immutable {
- protected final K nodeIdentifier;
- protected final V value;
+ private final K nodeIdentifier;
+ private final V value;
- protected AbstractImmutableNormalizedNode(K nodeIdentifier, V value) {
+ protected AbstractImmutableNormalizedNode(final K nodeIdentifier, final V value) {
this.nodeIdentifier = Preconditions.checkNotNull(nodeIdentifier, "nodeIdentifier");
this.value = Preconditions.checkNotNull(value, "value");
}
}
@Override
- public final V setValue(V value) {
+ public final V setValue(final V value) {
throw new UnsupportedOperationException("Immutable");
}
@Override
public final String toString() {
- return Objects.toStringHelper(this)
- .add("nodeIdentifier", nodeIdentifier)
- .add("value", value)
- .toString();
+ return addToStringAttributes(Objects.toStringHelper(this)).toString();
}
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (!(o instanceof AbstractImmutableNormalizedNode)) return false;
-
- AbstractImmutableNormalizedNode<?, ?> that = (AbstractImmutableNormalizedNode<?, ?>) o;
+ protected ToStringHelper addToStringAttributes(final ToStringHelper toStringHelper) {
+ return toStringHelper.add("nodeIdentifier", nodeIdentifier).add("value", getValue());
+ }
- if (!nodeIdentifier.equals(that.nodeIdentifier)) return false;
- if (!value.equals(that.value)) return false;
+ protected abstract boolean valueEquals(AbstractImmutableNormalizedNode<?, ?> other);
+ protected abstract int valueHashCode();
- return true;
+ @Override
+ public final boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (this.getClass() != obj.getClass()) {
+ return false;
+ }
+
+ final AbstractImmutableNormalizedNode<?, ?> other = (AbstractImmutableNormalizedNode<?, ?>)obj;
+ if (!nodeIdentifier.equals(other.nodeIdentifier)) {
+ return false;
+ }
+
+ return valueEquals(other);
}
@Override
- public int hashCode() {
+ public final int hashCode() {
int result = nodeIdentifier.hashCode();
- result = 31 * result + value.hashCode();
+ result = 31 * result + valueHashCode();
return result;
}
}