AbstractImmutableNodeHandler.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.configuration2.tree;

  18. import java.util.Collections;
  19. import java.util.List;
  20. import java.util.Set;
  21. import java.util.stream.Collectors;

  22. /**
  23.  * <p>
  24.  * An abstract base class for a {@link NodeHandler} implementation for {@link ImmutableNode} objects.
  25.  * </p>
  26.  * <p>
  27.  * This class already implements all methods which need no other information than the passed in node object.
  28.  * Functionality which requires additional state (for example querying the root node or a parent node) has to be added by
  29.  * concrete sub classes.
  30.  * </p>
  31.  *
  32.  * @since 2.0
  33.  */
  34. abstract class AbstractImmutableNodeHandler implements NodeHandler<ImmutableNode> {
  35.     /**
  36.      * Checks if the passed in node is defined. Result is <strong>true</strong> if the node contains any data.
  37.      *
  38.      * @param node the node in question
  39.      * @return <strong>true</strong> if the node is defined, <strong>false</strong> otherwise
  40.      */
  41.     static boolean checkIfNodeDefined(final ImmutableNode node) {
  42.         return node.getValue() != null || !node.getChildren().isEmpty() || !node.getAttributes().isEmpty();
  43.     }

  44.     @Override
  45.     public Set<String> getAttributes(final ImmutableNode node) {
  46.         return node.getAttributes().keySet();
  47.     }

  48.     @Override
  49.     public Object getAttributeValue(final ImmutableNode node, final String name) {
  50.         return node.getAttributes().get(name);
  51.     }

  52.     @Override
  53.     public ImmutableNode getChild(final ImmutableNode node, final int index) {
  54.         return node.getChildren().get(index);
  55.     }

  56.     @Override
  57.     public List<ImmutableNode> getChildren(final ImmutableNode node) {
  58.         return node.getChildren();
  59.     }

  60.     /**
  61.      * {@inheritDoc} This implementation returns an immutable list with all child nodes that have the specified name.
  62.      */
  63.     @Override
  64.     public List<ImmutableNode> getChildren(final ImmutableNode node, final String name) {
  65.         return getMatchingChildren(node, NodeNameMatchers.EQUALS, name);
  66.     }

  67.     @Override
  68.     public int getChildrenCount(final ImmutableNode node, final String name) {
  69.         if (name == null) {
  70.             return node.getChildren().size();
  71.         }
  72.         return getMatchingChildrenCount(node, NodeNameMatchers.EQUALS, name);
  73.     }

  74.     /**
  75.      * {@inheritDoc} This implementation returns an immutable list with all child nodes accepted by the specified matcher.
  76.      */
  77.     @Override
  78.     public <C> List<ImmutableNode> getMatchingChildren(final ImmutableNode node, final NodeMatcher<C> matcher, final C criterion) {
  79.         return Collections.unmodifiableList(node.stream().filter(c -> matcher.matches(c, this, criterion)).collect(Collectors.toList()));
  80.     }

  81.     @Override
  82.     public <C> int getMatchingChildrenCount(final ImmutableNode node, final NodeMatcher<C> matcher, final C criterion) {
  83.         return getMatchingChildren(node, matcher, criterion).size();
  84.     }

  85.     @Override
  86.     public Object getValue(final ImmutableNode node) {
  87.         return node.getValue();
  88.     }

  89.     @Override
  90.     public boolean hasAttributes(final ImmutableNode node) {
  91.         return !node.getAttributes().isEmpty();
  92.     }

  93.     @Override
  94.     public int indexOfChild(final ImmutableNode parent, final ImmutableNode child) {
  95.         return parent.getChildren().indexOf(child);
  96.     }

  97.     /**
  98.      * {@inheritDoc} This implementation assumes that a node is defined if it has a value or has children or has attributes.
  99.      */
  100.     @Override
  101.     public boolean isDefined(final ImmutableNode node) {
  102.         return checkIfNodeDefined(node);
  103.     }

  104.     @Override
  105.     public String nodeName(final ImmutableNode node) {
  106.         return node.getNodeName();
  107.     }
  108. }