001/*
002 * Copyright (C) 2022 - 2024, the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *    http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.github.ascopes.jct.repr;
017
018import static java.util.stream.Collectors.joining;
019
020import io.github.ascopes.jct.diagnostics.TraceDiagnostic;
021import java.util.Collection;
022import javax.tools.JavaFileObject;
023import org.assertj.core.presentation.Representation;
024import org.jspecify.annotations.Nullable;
025
026/**
027 * Representation of a collection of diagnostics.
028 *
029 * @author Ashley Scopes
030 * @since 0.0.1
031 */
032public final class TraceDiagnosticListRepresentation implements Representation {
033
034  private static final TraceDiagnosticListRepresentation INSTANCE
035      = new TraceDiagnosticListRepresentation();
036
037  /**
038   * Get an instance of this diagnostic collection representation.
039   *
040   * @return the instance.
041   */
042  public static TraceDiagnosticListRepresentation getInstance() {
043    return INSTANCE;
044  }
045
046  private TraceDiagnosticListRepresentation() {
047    // Nothing to see here, move along now.
048  }
049
050  @Override
051  public String toStringOf(@Nullable Object object) {
052    if (object == null) {
053      return "null";
054    }
055
056    @SuppressWarnings("unchecked")
057    var diagnostics = (Collection<? extends TraceDiagnostic<? extends JavaFileObject>>) object;
058
059    return "\n" + diagnostics
060        .stream()
061        .map(TraceDiagnosticRepresentation.getInstance()::toStringOf)
062        .map(this::indentAndBullet)
063        .collect(joining("\n\n"));
064  }
065
066  private String indentAndBullet(String repr) {
067    return " - " + repr.lines().collect(joining("\n   "));
068  }
069}