org.openrewrite.protobuf.tree.ProtoContainer Maven / Gradle / Ivy
Show all versions of rewrite-protobuf Show documentation
/*
* Copyright 2020 the original author or authors.
*
* 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
*
* https://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.openrewrite.protobuf.tree;
import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.RequiredArgsConstructor;
import org.jspecify.annotations.Nullable;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.marker.Markers;
import java.util.List;
import java.util.function.UnaryOperator;
import static java.util.Collections.emptyList;
/**
* AST elements that contain lists of trees with some delimiter like function call arguments.
*
* @param The type of the inner list of elements.
*/
public class ProtoContainer {
private transient Padding padding;
private static final ProtoContainer> EMPTY = new ProtoContainer<>(Space.EMPTY, emptyList(), Markers.EMPTY);
private final Space before;
private final List> elements;
private final Markers markers;
private ProtoContainer(Space before, List> elements, Markers markers) {
this.before = before;
this.elements = elements;
this.markers = markers;
}
public static ProtoContainer build(List> elements) {
return build(Space.EMPTY, elements, Markers.EMPTY);
}
@JsonCreator
public static ProtoContainer build(Space before, List> elements, Markers markers) {
if (before.isEmpty() && elements.isEmpty()) {
return empty();
}
return new ProtoContainer<>(before, elements, markers);
}
@SuppressWarnings("unchecked")
public static ProtoContainer empty() {
return (ProtoContainer) EMPTY;
}
public ProtoContainer withBefore(Space before) {
return getBefore() == before ? this : build(before, elements, markers);
}
public ProtoContainer withMarkers(Markers markers) {
return getMarkers() == markers ? this : build(before, elements, markers);
}
public Markers getMarkers() {
return markers;
}
public List getElements() {
return ProtoRightPadded.getElements(elements);
}
public Space getBefore() {
return before;
}
public ProtoContainer map(UnaryOperator map) {
return getPadding().withElements(ListUtils.map(elements, t -> t.map(map)));
}
public Space getLastSpace() {
return elements.isEmpty() ? Space.EMPTY : elements.get(elements.size() - 1).getAfter();
}
public Padding getPadding() {
if (padding == null) {
this.padding = new Padding<>(this);
}
return padding;
}
@RequiredArgsConstructor
public static class Padding {
private final ProtoContainer c;
public List> getElements() {
return c.elements;
}
public ProtoContainer withElements(List> elements) {
return c.elements == elements ? c : build(c.before, elements, c.markers);
}
}
public static @Nullable ProtoContainer
withElementsNullable(@Nullable ProtoContainer
before, @Nullable List
elements) {
if (before == null) {
if (elements == null || elements.isEmpty()) {
return null;
}
return ProtoContainer.build(Space.EMPTY, ProtoRightPadded.withElements(emptyList(), elements), Markers.EMPTY);
}
if (elements == null || elements.isEmpty()) {
return null;
}
return before.getPadding().withElements(ProtoRightPadded.withElements(before.elements, elements));
}
public static
ProtoContainer
withElements(ProtoContainer
before, @Nullable List
elements) {
if (elements == null) {
return before.getPadding().withElements(emptyList());
}
return before.getPadding().withElements(ProtoRightPadded.withElements(before.elements, elements));
}
@Override
public String toString() {
return "ProtoContainer(before=" + before + ", elementCount=" + elements.size() + ')';
}
}