Cleanup RuntimeException throws
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / main / java / org / opendaylight / mdsal / dom / store / inmemory / AbstractDOMShardTreeChangePublisher.java
index 250494180c62bdcae2807ebc97956a11581714ef..3fc183ee34b35da2453c72702ebfe4423a341d10 100644 (file)
@@ -5,11 +5,11 @@
  * 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.mdsal.dom.store.inmemory;
 
-import com.google.common.base.Optional;
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.Objects.requireNonNull;
+
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -17,13 +17,13 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.stream.Collectors;
-import javax.annotation.Nonnull;
-import javax.annotation.Nullable;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeListener;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
 import org.opendaylight.mdsal.dom.spi.AbstractDOMDataTreeChangeListenerRegistration;
 import org.opendaylight.mdsal.dom.spi.RegistrationTreeNode;
+import org.opendaylight.mdsal.dom.spi.shard.ChildShardContext;
 import org.opendaylight.mdsal.dom.spi.store.AbstractDOMStoreTreeChangePublisher;
 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTreeChangePublisher;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
@@ -36,18 +36,17 @@ import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTree;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNode;
+import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidateNodes;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidates;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataValidationFailedException;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.ModificationType;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.api.NormalizedNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableMapEntryNodeBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-abstract class AbstractDOMShardTreeChangePublisher extends AbstractDOMStoreTreeChangePublisher
-        implements DOMStoreTreeChangePublisher {
+abstract class AbstractDOMShardTreeChangePublisher extends AbstractDOMStoreTreeChangePublisher {
 
     private static final Logger LOG = LoggerFactory.getLogger(AbstractDOMShardTreeChangePublisher.class);
 
@@ -56,11 +55,10 @@ abstract class AbstractDOMShardTreeChangePublisher extends AbstractDOMStoreTreeC
     private final DataTree dataTree;
 
     protected AbstractDOMShardTreeChangePublisher(final DataTree dataTree,
-                                                  final YangInstanceIdentifier shardPath,
-                                                  final Map<DOMDataTreeIdentifier, ChildShardContext> childShards) {
-        this.dataTree = Preconditions.checkNotNull(dataTree);
-        this.shardPath = Preconditions.checkNotNull(shardPath);
-        this.childShards = Preconditions.checkNotNull(childShards);
+            final YangInstanceIdentifier shardPath, final Map<DOMDataTreeIdentifier, ChildShardContext> childShards) {
+        this.dataTree = requireNonNull(dataTree);
+        this.shardPath = requireNonNull(shardPath);
+        this.childShards = requireNonNull(childShards);
     }
 
     @Override
@@ -117,20 +115,19 @@ abstract class AbstractDOMShardTreeChangePublisher extends AbstractDOMStoreTreeC
 
         if (preExistingData.isPresent()) {
             final NormalizedNode<?, ?> data = preExistingData.get();
-            Preconditions.checkState(data instanceof DataContainerNode,
-                    "Expected DataContainer node, but was {}", data.getClass());
+            checkState(data instanceof DataContainerNode, "Expected DataContainer node, but was {}", data.getClass());
             // if we are listening on root of some shard we still get
             // empty normalized node, root is always present
             if (((DataContainerNode<?>) data).getValue().isEmpty()) {
                 initialCandidate = DataTreeCandidates.newDataTreeCandidate(listenerPath,
-                        new EmptyDataTreeCandidateNode(data.getIdentifier()));
+                    DataTreeCandidateNodes.empty(data.getIdentifier()));
             } else {
                 initialCandidate = DataTreeCandidates.fromNormalizedNode(listenerPath,
-                        translateRootShardIdentifierToListenerPath(listenerPath, preExistingData.get()));
+                    translateRootShardIdentifierToListenerPath(listenerPath, preExistingData.get()));
             }
         } else {
             initialCandidate = DataTreeCandidates.newDataTreeCandidate(listenerPath,
-                    new EmptyDataTreeCandidateNode(listenerPath.getLastPathArgument()));
+                DataTreeCandidateNodes.empty(listenerPath.getLastPathArgument()));
         }
 
         listener.onDataTreeChanged(Collections.singleton(initialCandidate));
@@ -203,16 +200,15 @@ abstract class AbstractDOMShardTreeChangePublisher extends AbstractDOMStoreTreeC
         private final Map<YangInstanceIdentifier, ListenerRegistration<DOMDataTreeChangeListener>> registrations =
                 new HashMap<>();
 
-        DOMDataTreeListenerWithSubshards(final DataTree dataTree,
-                                         final YangInstanceIdentifier listenerPath,
-                                         final DOMDataTreeChangeListener delegate) {
-            this.dataTree = Preconditions.checkNotNull(dataTree);
-            this.listenerPath = Preconditions.checkNotNull(listenerPath);
-            this.delegate = Preconditions.checkNotNull(delegate);
+        DOMDataTreeListenerWithSubshards(final DataTree dataTree, final YangInstanceIdentifier listenerPath,
+                final DOMDataTreeChangeListener delegate) {
+            this.dataTree = requireNonNull(dataTree);
+            this.listenerPath = requireNonNull(listenerPath);
+            this.delegate = requireNonNull(delegate);
         }
 
         @Override
-        public void onDataTreeChanged(@Nonnull final Collection<DataTreeCandidate> changes) {
+        public void onDataTreeChanged(final Collection<DataTreeCandidate> changes) {
             LOG.debug("Received data changed {}", changes);
             delegate.onDataTreeChanged(changes);
         }
@@ -225,7 +221,7 @@ abstract class AbstractDOMShardTreeChangePublisher extends AbstractDOMStoreTreeC
         }
 
         void addSubshard(final ChildShardContext context) {
-            Preconditions.checkState(context.getShard() instanceof DOMStoreTreeChangePublisher,
+            checkState(context.getShard() instanceof DOMStoreTreeChangePublisher,
                     "All subshards that are initialDataChangeEvent part of ListenerContext need to be listenable");
 
             final DOMStoreTreeChangePublisher listenableShard = (DOMStoreTreeChangePublisher) context.getShard();
@@ -254,66 +250,20 @@ abstract class AbstractDOMShardTreeChangePublisher extends AbstractDOMStoreTreeC
                 dataTree.validate(modification);
             } catch (final DataValidationFailedException e) {
                 LOG.error("Validation failed for built modification", e);
-                throw new RuntimeException("Notification validation failed", e);
+                throw new IllegalStateException("Notification validation failed", e);
             }
 
-            // strip nodes we dont need since this listener doesn't have to be registered at the root of the DataTree
+            // strip nodes we do not need since this listener doesn't have to be registered at the root of the DataTree
             DataTreeCandidateNode modifiedChild = dataTree.prepare(modification).getRootNode();
             for (final PathArgument pathArgument : listenerPath.getPathArguments()) {
                 modifiedChild = modifiedChild.getModifiedChild(pathArgument);
             }
 
             if (modifiedChild == null) {
-                modifiedChild = new EmptyDataTreeCandidateNode(listenerPath.getLastPathArgument());
+                modifiedChild = DataTreeCandidateNodes.empty(listenerPath.getLastPathArgument());
             }
 
             return DataTreeCandidates.newDataTreeCandidate(listenerPath, modifiedChild);
         }
     }
-
-    private static final class EmptyDataTreeCandidateNode implements DataTreeCandidateNode {
-
-        private final PathArgument identifier;
-
-        EmptyDataTreeCandidateNode(final PathArgument identifier) {
-            this.identifier = Preconditions.checkNotNull(identifier, "Identifier should not be null");
-        }
-
-        @Nonnull
-        @Override
-        public PathArgument getIdentifier() {
-            return identifier;
-        }
-
-        @Nonnull
-        @Override
-        public Collection<DataTreeCandidateNode> getChildNodes() {
-            return Collections.emptySet();
-        }
-
-        @Nullable
-        @Override
-        public DataTreeCandidateNode getModifiedChild(final PathArgument identifier) {
-            return null;
-        }
-
-        @Nonnull
-        @Override
-        public ModificationType getModificationType() {
-            return ModificationType.UNMODIFIED;
-        }
-
-        @Nonnull
-        @Override
-        public Optional<NormalizedNode<?, ?>> getDataAfter() {
-            return Optional.absent();
-        }
-
-        @Nonnull
-        @Override
-        public Optional<NormalizedNode<?, ?>> getDataBefore() {
-            return Optional.absent();
-        }
-    }
-
 }
\ No newline at end of file