
eu.cloudnetservice.node.cluster.sync.prettyprint.GulfPrettyPrint Maven / Gradle / Ivy
/*
* Copyright 2019-2023 CloudNetService team & contributors
*
* 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 eu.cloudnetservice.node.cluster.sync.prettyprint;
import dev.derklaro.gulf.diff.Change;
import dev.derklaro.gulf.diff.array.ArrayChange;
import dev.derklaro.gulf.diff.array.CollectionChange;
import dev.derklaro.gulf.diff.map.MapChange;
import dev.derklaro.gulf.path.ObjectPath;
import java.util.Collection;
import java.util.function.Consumer;
import lombok.NonNull;
import org.jetbrains.annotations.Nullable;
public final class GulfPrettyPrint {
private GulfPrettyPrint() {
throw new UnsupportedOperationException();
}
public static @NonNull String[] prettyPrint(@NonNull String entryName, @NonNull Collection> changes) {
// initial information
var builder = new StringBuilder("&rThere were &6")
.append(changes.size())
.append("&r changes to synced element &c")
.append(entryName)
.append("&r:")
.append(System.lineSeparator());
// append all changes
printChanges(builder, " ", changes, true);
// stringify
return builder.append(System.lineSeparator()).toString().split(System.lineSeparator());
}
@SuppressWarnings({"rawtypes", "unchecked"}) // generics (╯°□°)╯︵ ┻━┻
static void printChanges(
@NonNull StringBuilder builder,
@NonNull String indent,
@NonNull Collection> changes,
boolean withPath
) {
for (var change : changes) {
if (change instanceof ArrayChange arrayChange) {
// changes in an array
GulfArrayDiffPrinter.printArrayChange(builder, indent, arrayChange, withPath);
} else if (change instanceof CollectionChange collectionChange) {
// changes in a collection
GulfArrayDiffPrinter.printCollectionChange(builder, indent, collectionChange, withPath);
} else if (change instanceof MapChange mapChange) {
// changes in a map
GulfMapDiffPrinter.printMapChange(builder, indent, mapChange, withPath);
} else {
// general change
printValueChange(builder, indent, change, withPath);
}
}
}
static void printValueChange(
@NonNull StringBuilder builder,
@NonNull String indent,
@NonNull Change
© 2015 - 2025 Weber Informatics LLC | Privacy Policy