org.openrewrite.hcl.tree.HclContainer Maven / Gradle / Ivy
Show all versions of rewrite-hcl 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.hcl.tree;
import com.fasterxml.jackson.annotation.JsonCreator;
import lombok.RequiredArgsConstructor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
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 HclContainer {
private transient Padding padding;
private static final HclContainer> EMPTY = new HclContainer<>(Space.EMPTY, emptyList(), Markers.EMPTY);
private final Space before;
private final List> elements;
private final Markers markers;
private HclContainer(Space before, List> elements, Markers markers) {
this.before = before;
this.elements = elements;
this.markers = markers;
}
public static HclContainer build(List> elements) {
return build(Space.EMPTY, elements, Markers.EMPTY);
}
@JsonCreator
public static HclContainer build(Space before, List> elements, Markers markers) {
if (before.isEmpty() && elements.isEmpty()) {
return empty();
}
return new HclContainer<>(before, elements, markers);
}
@SuppressWarnings("unchecked")
public static HclContainer empty() {
return (HclContainer) EMPTY;
}
public HclContainer withBefore(Space before) {
return getBefore() == before ? this : build(before, elements, markers);
}
public HclContainer withMarkers(Markers markers) {
return getMarkers() == markers ? this : build(before, elements, markers);
}
public Markers getMarkers() {
return markers;
}
public List getElements() {
return HclRightPadded.getElements(elements);
}
public Space getBefore() {
return before;
}
public HclContainer 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 HclContainer withLastSpace(Space space) {
final List> newElements = ListUtils.map(elements, (i, elem) -> {
if (i == elements.size() -1) {
return elem.withAfter(space);
}
return elem;
});
return elements.isEmpty() || getLastSpace() == space ? this : build(before, newElements, markers);
}
public enum Location {
FOR_VARIABLES(Space.Location.FOR_VARIABLES, HclRightPadded.Location.FOR_VARIABLE_ARGUMENT),
FUNCTION_CALL_ARGUMENTS(Space.Location.FUNCTION_CALL_ARGUMENTS, HclRightPadded.Location.FUNCTION_CALL_ARGUMENT),
OBJECT_VALUE_ATTRIBUTES(Space.Location.OBJECT_VALUE_ATTRIBUTES, HclRightPadded.Location.OBJECT_VALUE_ARGUMENT),
TUPLE_VALUES(Space.Location.TUPLE_VALUES, HclRightPadded.Location.TUPLE_VALUE);
private final Space.Location beforeLocation;
private final HclRightPadded.Location elementLocation;
Location(Space.Location beforeLocation, HclRightPadded.Location elementLocation) {
this.beforeLocation = beforeLocation;
this.elementLocation = elementLocation;
}
public Space.Location getBeforeLocation() {
return beforeLocation;
}
public HclRightPadded.Location getElementLocation() {
return elementLocation;
}
}
public Padding getPadding() {
if (padding == null) {
this.padding = new Padding<>(this);
}
return padding;
}
@RequiredArgsConstructor
public static class Padding {
private final HclContainer c;
public List> getElements() {
return c.elements;
}
public HclContainer withElements(List> elements) {
return c.elements == elements ? c : build(c.before, elements, c.markers);
}
}
public static @Nullable HclContainer withElementsNullable(@Nullable HclContainer before, @Nullable List elements) {
if (before == null) {
if (elements == null || elements.isEmpty()) {
return null;
}
return HclContainer.build(Space.EMPTY, HclRightPadded.withElements(emptyList(), elements), Markers.EMPTY);
}
if (elements == null || elements.isEmpty()) {
return null;
}
return before.getPadding().withElements(HclRightPadded.withElements(before.elements, elements));
}
public static HclContainer withElements(HclContainer before, @Nullable List elements) {
if (elements == null) {
return before.getPadding().withElements(emptyList());
}
return before.getPadding().withElements(HclRightPadded.withElements(before.elements, elements));
}
@Override
public String toString() {
return "HclContainer(before=" + before + ", elementCount=" + elements.size() + ')';
}
}