import org.opendaylight.yangtools.yang.model.api.stmt.DataTreeEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeAwareEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.SchemaTreeEffectiveStatement;
-import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
import org.opendaylight.yangtools.yang.model.api.stmt.TypedefNamespace;
/**
E extends DataTreeAwareEffectiveStatement<A, D>> extends WithDataTree<A, D, E> {
public abstract static class WithTypedefNamespace<A, D extends DeclaredStatement<A>,
E extends DataTreeAwareEffectiveStatement<A, D>> extends DefaultWithDataTree<A, D, E> {
- private final @NonNull ImmutableMap<QName, TypedefEffectiveStatement> typedefNamespace;
-
protected WithTypedefNamespace(final D declared,
final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
super(declared, substatements);
- this.typedefNamespace = createTypedefNamespace(substatements);
+ // Consistency check only
+ createTypedefNamespace(substatements);
}
protected WithTypedefNamespace(final WithTypedefNamespace<A, D, E> original) {
super(original);
- this.typedefNamespace = original.typedefNamespace;
}
@Override
protected <K, V, N extends IdentifierNamespace<K, V>> Optional<? extends Map<K, V>> getNamespaceContents(
final Class<N> namespace) {
if (TypedefNamespace.class.equals(namespace)) {
- return Optional.of((Map<K, V>) typedefNamespace);
+ return Optional.of((Map<K, V>) new LinearTypedefNamespace(effectiveSubstatements()));
}
return super.getNamespaceContents(namespace);
}
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.util.Collection;
+import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
return sameAsSchema ? (ImmutableMap) schemaTreeNamespace : ImmutableMap.copyOf(dataChildren);
}
- protected static @NonNull ImmutableMap<QName, TypedefEffectiveStatement> createTypedefNamespace(
+ protected static @NonNull HashMap<QName, TypedefEffectiveStatement> createTypedefNamespace(
final Collection<? extends EffectiveStatement<?, ?>> substatements) {
- final Map<QName, TypedefEffectiveStatement> typedefs = new LinkedHashMap<>();
+ final HashMap<QName, TypedefEffectiveStatement> typedefs = new HashMap<>();
substatements.stream().filter(TypedefEffectiveStatement.class::isInstance)
.forEach(child -> putChild(typedefs, (TypedefEffectiveStatement) child, "typedef"));
- return ImmutableMap.copyOf(typedefs);
+ return typedefs;
}
private static boolean indexDataTree(final Map<QName, DataTreeEffectiveStatement<?>> map,
--- /dev/null
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.spi.meta;
+
+import static java.util.Objects.requireNonNull;
+
+import com.google.common.collect.Collections2;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableSet;
+import java.util.AbstractMap;
+import java.util.Collection;
+import java.util.Map;
+import java.util.Set;
+import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.meta.EffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.TypedefEffectiveStatement;
+import org.opendaylight.yangtools.yang.model.api.stmt.TypedefNamespace;
+
+/**
+ * A filter-based implementation of a Map to serve with {@link TypedefNamespace}.
+ */
+final class LinearTypedefNamespace extends AbstractMap<QName, TypedefEffectiveStatement> implements Immutable {
+ private final Collection<TypedefEffectiveStatement> values;
+
+ @SuppressWarnings("unchecked")
+ LinearTypedefNamespace(final ImmutableList<? extends EffectiveStatement<?, ?>> substatements) {
+ values = (Collection<TypedefEffectiveStatement>)
+ Collections2.filter(substatements, TypedefEffectiveStatement.class::isInstance);
+ }
+
+ @Override
+ public int size() {
+ return values.size();
+ }
+
+ @Override
+ public boolean isEmpty() {
+ return values.isEmpty();
+ }
+
+ @Override
+ public boolean containsKey(final Object key) {
+ return get(key) != null;
+ }
+
+ @Override
+ public boolean containsValue(final Object value) {
+ return values.contains(requireNonNull(value));
+ }
+
+ @Override
+ public TypedefEffectiveStatement get(final Object key) {
+ final var nonnull = requireNonNull(key);
+ return values().stream().filter(stmt -> nonnull.equals(stmt.argument())).findFirst().orElse(null);
+ }
+
+ @Override
+ public TypedefEffectiveStatement remove(final Object key) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ @SuppressWarnings("checkstyle:parameterName")
+ public void putAll(final Map<? extends QName, ? extends TypedefEffectiveStatement> m) {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void clear() {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public Set<QName> keySet() {
+ return values.stream().map(TypedefEffectiveStatement::argument).collect(ImmutableSet.toImmutableSet());
+ }
+
+ @Override
+ public Collection<TypedefEffectiveStatement> values() {
+ return values;
+ }
+
+ @Override
+ public Set<Entry<QName, TypedefEffectiveStatement>> entrySet() {
+ return values.stream().map(stmt -> Map.entry(stmt.argument(), stmt)).collect(ImmutableSet.toImmutableSet());
+ }
+}