/* * Copyright (c) 2015 Cisco Systems, Inc. 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.parser.stmt.reactor; import static java.util.Objects.requireNonNull; import com.google.common.base.MoreObjects.ToStringHelper; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Map; import org.eclipse.jdt.annotation.NonNull; import org.opendaylight.yangtools.yang.parser.spi.meta.NamespaceBehaviour; import org.opendaylight.yangtools.yang.parser.spi.meta.ParserNamespace; abstract class NamespaceBehaviourWithListeners> extends NamespaceBehaviour { abstract static class ValueAddedListener { private final NamespaceStorageNode ctxNode; ValueAddedListener(final NamespaceStorageNode contextNode) { this.ctxNode = requireNonNull(contextNode); } final NamespaceStorageNode getCtxNode() { return ctxNode; } } abstract static class KeyedValueAddedListener extends ValueAddedListener { private final K key; KeyedValueAddedListener(final NamespaceStorageNode contextNode, final K key) { super(contextNode); this.key = requireNonNull(key); } final K getKey() { return key; } final boolean isRequestedValue(final NamespaceBehaviour behavior, final NamespaceStorageNode storage, final V value) { return value == behavior.getFrom(getCtxNode(), key); } abstract void onValueAdded(Object value); } abstract static class PredicateValueAddedListener extends ValueAddedListener { PredicateValueAddedListener(final NamespaceStorageNode contextNode) { super(contextNode); } abstract boolean onValueAdded(@NonNull K key, @NonNull V value); } protected final NamespaceBehaviour delegate; private List> derivedNamespaces; protected NamespaceBehaviourWithListeners(final NamespaceBehaviour delegate) { super(delegate.getIdentifier()); this.delegate = delegate; } abstract void addListener(KeyedValueAddedListener listener); abstract void addListener(PredicateValueAddedListener listener); @Override public abstract void addTo(NamespaceStorageNode storage, K key, V value); protected void notifyListeners(final NamespaceStorageNode storage, final Iterator> keyListeners, final V value) { List> toNotify = new ArrayList<>(); while (keyListeners.hasNext()) { final KeyedValueAddedListener listener = keyListeners.next(); if (listener.isRequestedValue(this, storage, value)) { keyListeners.remove(); toNotify.add(listener); } } for (KeyedValueAddedListener listener : toNotify) { listener.onValueAdded(value); } } protected void notifyDerivedNamespaces(final NamespaceStorageNode storage, final K key, final V value) { if (derivedNamespaces != null) { for (VirtualNamespaceContext derived : derivedNamespaces) { derived.addedToSourceNamespace(storage, key, value); } } } final void addDerivedNamespace(final VirtualNamespaceContext namespace) { if (derivedNamespaces == null) { derivedNamespaces = new ArrayList<>(); } derivedNamespaces.add(namespace); } @Override public V getFrom(final NamespaceStorageNode storage, final K key) { return delegate.getFrom(storage, key); } @Override public Map getAllFrom(final NamespaceStorageNode storage) { return delegate.getAllFrom(storage); } @Override protected ToStringHelper addToStringAttributes(final ToStringHelper helper) { return super.addToStringAttributes(helper).add("delegate", delegate); } }