2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.yang.model.export;
10 import static java.util.Objects.requireNonNull;
11 import static org.eclipse.jdt.annotation.DefaultLocation.PARAMETER;
12 import static org.eclipse.jdt.annotation.DefaultLocation.RETURN_TYPE;
14 import com.google.common.annotations.Beta;
15 import java.util.Iterator;
17 import java.util.Spliterator;
18 import java.util.Spliterators;
19 import java.util.stream.Collectors;
20 import java.util.stream.Stream;
21 import java.util.stream.StreamSupport;
22 import org.eclipse.jdt.annotation.NonNull;
23 import org.eclipse.jdt.annotation.NonNullByDefault;
24 import org.opendaylight.yangtools.concepts.Immutable;
25 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
26 import org.opendaylight.yangtools.yang.model.api.meta.StatementDefinition;
29 * A YANG text snippet generated from a {@link DeclaredStatement}. Generated {@link #stream()} or {@link #iterator()}
30 * are guaranteed to not contain null nor empty strings. Furthermore, newlines are always emitted at the end
31 * on the generated string -- which can be checked with {@link #isEolString(String)} utility method.
34 * This allows individual strings to be escaped as needed and external indentation can be accounted for by inserting
35 * outer document indentation after the string which matched {@link #isEolString(String)} is emitted to the stream.
37 * @author Robert Varga
40 @NonNullByDefault({ PARAMETER, RETURN_TYPE })
41 public final class YangTextSnippet implements Immutable, Iterable<@NonNull String> {
42 private final Set<@NonNull StatementDefinition> ignoredStatements;
43 private final StatementPrefixResolver resolver;
44 private final DeclaredStatement<?> statement;
45 private final boolean omitDefaultStatements;
47 YangTextSnippet(final DeclaredStatement<?> statement, final StatementPrefixResolver resolver,
48 final Set<@NonNull StatementDefinition> ignoredStatements, final boolean omitDefaultStatements) {
49 this.statement = requireNonNull(statement);
50 this.resolver = requireNonNull(resolver);
51 this.ignoredStatements = requireNonNull(ignoredStatements);
52 this.omitDefaultStatements = omitDefaultStatements;
56 public Iterator<@NonNull String> iterator() {
57 return new YangTextSnippetIterator(statement, resolver, ignoredStatements, omitDefaultStatements);
61 @SuppressWarnings("null")
62 public Spliterator<@NonNull String> spliterator() {
63 return Spliterators.spliteratorUnknownSize(iterator(),
64 Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL);
67 @SuppressWarnings("null")
68 public Stream<@NonNull String> stream() {
69 return StreamSupport.stream(spliterator(), false);
73 * Check if an emitted string contains End-Of-Line character.
75 * @param str String to be checked
76 * @return True if the string contains end of line.
77 * @throws NullPointerException if str is null
79 public static boolean isEolString(final String str) {
80 return !str.isEmpty() && str.charAt(str.length() - 1) == '\n';
84 @SuppressWarnings("null")
85 public String toString() {
86 return stream().collect(Collectors.joining());