org.apache.commons.configuration2.tree.NodeUpdateData Maven / Gradle / Ivy
Show all versions of commons-configuration2 Show documentation
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.configuration2.tree;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
*
* A simple data class used by node models to store parameters of an update operation.
*
*
* The {@code Configuration} interface provides a method for setting the value of a given key. The passed in value can
* be a single object or a collection of values. This makes an update operation rather complicated because a collection
* of query results selected by the passed in key has to be matched to another collection of values - and both
* collections can have different sizes. Therefore, an update operation may involve changing of existing nodes, adding
* new nodes (if there are more values than currently existing nodes), and removing nodes (if there are more existing
* nodes than provided values). This class collects all this information making it possible to actually perform the
* update based on a passed in instance.
*
*
* @since 2.0
* @param the type of nodes involved in this update operation
*/
public class NodeUpdateData {
/**
* Creates an unmodifiable defensive copy of the passed in collection with may be null.
*
* @param col the collection to be copied
* @param the element type of the collection
* @return the unmodifiable copy
*/
private static Collection copyCollection(final Collection extends T> col) {
if (col == null) {
return Collections.emptySet();
}
return Collections.unmodifiableCollection(new ArrayList<>(col));
}
/**
* Creates an unmodifiable defensive copy of the passed in map which may be null.
*
* @param map the map to be copied
* @param the type of the keys involved
* @param the type of the values involved
* @return the unmodifiable copy
*/
private static Map copyMap(final Map extends K, ? extends V> map) {
if (map == null) {
return Collections.emptyMap();
}
return Collections.unmodifiableMap(new HashMap<>(map));
}
/** The map with the query results whose value has to be changed. */
private final Map, Object> changedValues;
/** The collection with the new values to be added. */
private final Collection