NodeUpdateData.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.ArrayList;
  19. import java.util.Collection;
  20. import java.util.Collections;
  21. import java.util.HashMap;
  22. import java.util.Map;

  23. /**
  24.  * <p>
  25.  * A simple data class used by node models to store parameters of an update operation.
  26.  * </p>
  27.  * <p>
  28.  * The {@code Configuration} interface provides a method for setting the value of a given key. The passed in value can
  29.  * be a single object or a collection of values. This makes an update operation rather complicated because a collection
  30.  * of query results selected by the passed in key has to be matched to another collection of values - and both
  31.  * collections can have different sizes. Therefore, an update operation may involve changing of existing nodes, adding
  32.  * new nodes (if there are more values than currently existing nodes), and removing nodes (if there are more existing
  33.  * nodes than provided values). This class collects all this information making it possible to actually perform the
  34.  * update based on a passed in instance.
  35.  * </p>
  36.  *
  37.  * @param <T> the type of nodes involved in this update operation
  38.  * @since 2.0
  39.  */
  40. public class NodeUpdateData<T> {
  41.     /**
  42.      * Creates an unmodifiable defensive copy of the passed in collection with may be null.
  43.      *
  44.      * @param col the collection to be copied
  45.      * @param <T> the element type of the collection
  46.      * @return the unmodifiable copy
  47.      */
  48.     private static <T> Collection<T> copyCollection(final Collection<? extends T> col) {
  49.         if (col == null) {
  50.             return Collections.emptySet();
  51.         }
  52.         return Collections.unmodifiableCollection(new ArrayList<>(col));
  53.     }

  54.     /**
  55.      * Creates an unmodifiable defensive copy of the passed in map which may be null.
  56.      *
  57.      * @param map the map to be copied
  58.      * @param <K> the type of the keys involved
  59.      * @param <V> the type of the values involved
  60.      * @return the unmodifiable copy
  61.      */
  62.     private static <K, V> Map<K, V> copyMap(final Map<? extends K, ? extends V> map) {
  63.         if (map == null) {
  64.             return Collections.emptyMap();
  65.         }
  66.         return Collections.unmodifiableMap(new HashMap<>(map));
  67.     }

  68.     /** The map with the query results whose value has to be changed. */
  69.     private final Map<QueryResult<T>, Object> changedValues;

  70.     /** The collection with the new values to be added. */
  71.     private final Collection<Object> newValues;

  72.     /** The collection with query results about the nodes to be removed. */
  73.     private final Collection<QueryResult<T>> removedNodes;

  74.     /** The key of the current update operation. */
  75.     private final String key;

  76.     /**
  77.      * Creates a new instance of {@code NodeUpdateData} and initializes all its properties. All passed in collections are
  78.      * optional and can be <strong>null</strong>.
  79.      *
  80.      * @param changedValues the map defining the changed values
  81.      * @param newValues the collection with the new values
  82.      * @param removedNodes the collection with the nodes to be removed
  83.      * @param key the key of the update operation
  84.      */
  85.     public NodeUpdateData(final Map<QueryResult<T>, Object> changedValues, final Collection<Object> newValues, final Collection<QueryResult<T>> removedNodes,
  86.         final String key) {
  87.         this.changedValues = copyMap(changedValues);
  88.         this.newValues = copyCollection(newValues);
  89.         this.removedNodes = copyCollection(removedNodes);
  90.         this.key = key;
  91.     }

  92.     /**
  93.      * Gets an unmodifiable map with the values to be changed. The keys of the map are the query results for the nodes
  94.      * affected, the values are the new values to be assigned to these nodes.
  95.      *
  96.      * @return the map with values to be changed
  97.      */
  98.     public Map<QueryResult<T>, Object> getChangedValues() {
  99.         return changedValues;
  100.     }

  101.     /**
  102.      * Gets the key for this update operation.
  103.      *
  104.      * @return the key for this operation
  105.      */
  106.     public String getKey() {
  107.         return key;
  108.     }

  109.     /**
  110.      * Gets a collection with the values to be newly added. For these values new nodes have to be created and added under
  111.      * the key stored in this object.
  112.      *
  113.      * @return the collection with new values
  114.      */
  115.     public Collection<Object> getNewValues() {
  116.         return newValues;
  117.     }

  118.     /**
  119.      * Adds a collection with the nodes to be removed. These nodes are no longer needed and have to be removed from the node
  120.      * model processing this request.
  121.      *
  122.      * @return the collection with nodes to be removed
  123.      */
  124.     public Collection<QueryResult<T>> getRemovedNodes() {
  125.         return removedNodes;
  126.     }
  127. }