Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
*
* Copyright (c) 2006-2017, Speedment, Inc. All Rights Reserved.
*
* Licensed 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 com.speedment.runtime.config.util;
import com.speedment.common.mapstream.MapStream;
import com.speedment.runtime.config.Document;
import com.speedment.runtime.config.internal.util.Trees;
import com.speedment.runtime.config.trait.HasAlias;
import com.speedment.runtime.config.trait.HasName;
import com.speedment.runtime.config.trait.HasParent;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import static java.util.Objects.requireNonNull;
import java.util.Optional;
import java.util.StringJoiner;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Supplier;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import java.util.stream.Stream;
/**
* Common utility methods for working with instances of the {@code Document}
* interface.
*
* @author Per Minborg
* @author Emil Forslund
* @since 2.3.0
*/
public final class DocumentUtil {
/**
* Traverses all the documents at and below the specified document in a
* tree. Traversal is done depth first. The order of sub-document traversal
* within a specific Document is unspecified (For example, a tables columns
* may be traversed in any order).
*
* @param document the document to start at
* @return stream of descendants
*/
@SuppressWarnings("unchecked")
public static Stream traverseOver(Document document) {
requireNonNull(document);
return Trees.traverse(
document,
d -> d.children(),
Trees.TraversalOrder.DEPTH_FIRST_PRE
);
}
/**
* Returns the first ancestor found of the specified type to the specified
* document when walking up the tree. If there was no ancestor of the
* specified type and the root was reached, an empty {@code Optional} is
* returned.
*
* @param ancestor type
* @param document the starting point
* @param clazz the ancestor type to look for
* @return first ancestor found or empty
*/
public static Optional ancestor(
final Document document,
final Class clazz) {
requireNonNull(document);
requireNonNull(clazz);
return document.ancestors()
.filter(clazz::isInstance)
.map(clazz::cast)
.findFirst();
}
/**
* Returns a stream of child documents to a specified document by using the
* supplied constructor.
*
* @param the expected child type
* @param document the parent document
* @param childConstructor child constructor
* @return stream of children
*/
@SuppressWarnings("unchecked")
public static Stream childrenOf(
final Document document,
final BiFunction, E> childConstructor) {
requireNonNull(document);
requireNonNull(childConstructor);
return document.getData().values().stream()
.filter(obj -> obj instanceof List)
.map(list -> (List