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.
/*
* 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.brooklyn.util.yaml;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicBoolean;
import javax.annotation.Nullable;
import org.apache.brooklyn.util.collections.Jsonya;
import org.apache.brooklyn.util.collections.MutableList;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.exceptions.UserFacingException;
import org.apache.brooklyn.util.text.Strings;
import org.apache.brooklyn.util.yaml.Yamls;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.error.Mark;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeId;
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.SequenceNode;
import com.google.common.annotations.Beta;
import com.google.common.collect.Iterables;
public class Yamls {
private static final Logger log = LoggerFactory.getLogger(Yamls.class);
/** returns the given (yaml-parsed) object as the given yaml type.
*
* if the object is an iterable or iterator this method will fully expand it as a list.
* if the requested type is not an iterable or iterator, and the list contains a single item, this will take that single item.
*
* in other cases this method simply does a type-check and cast (no other type coercion).
*
* @throws IllegalArgumentException if the input is an iterable not containing a single element,
* and the cast is requested to a non-iterable type
* @throws ClassCastException if cannot be casted */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static T getAs(Object x, Class type) {
if (x==null) return null;
if (x instanceof Iterable || x instanceof Iterator) {
List result = new ArrayList();
Iterator xi;
if (Iterator.class.isAssignableFrom(x.getClass())) {
xi = (Iterator)x;
} else {
xi = ((Iterable)x).iterator();
}
while (xi.hasNext()) {
result.add( xi.next() );
}
if (type.isAssignableFrom(List.class)) return (T)result;
if (type.isAssignableFrom(Iterator.class)) return (T)result.iterator();
x = Iterables.getOnlyElement(result);
}
if (type.isInstance(x)) return (T)x;
throw new ClassCastException("Cannot convert "+x+" ("+x.getClass()+") to "+type);
}
/**
* Parses the given yaml, and walks the given path to return the referenced object.
*
* @see #getAt(Object, List)
*/
@Beta
public static Object getAt(String yaml, List path) {
Iterable