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.apiguardian.api.API;
024import org.apiguardian.api.API.Status;
025import org.assertj.core.presentation.Representation;
026import org.jspecify.annotations.Nullable;
027
028/**
029 * Representation of a collection of diagnostics.
030 *
031 * @author Ashley Scopes
032 * @since 0.0.1
033 */
034@API(since = "0.0.1", status = Status.STABLE)
035public final class TraceDiagnosticListRepresentation implements Representation {
036
037  private static final TraceDiagnosticListRepresentation INSTANCE
038      = new TraceDiagnosticListRepresentation();
039
040  /**
041   * Get an instance of this diagnostic collection representation.
042   *
043   * @return the instance.
044   */
045  public static TraceDiagnosticListRepresentation getInstance() {
046    return INSTANCE;
047  }
048
049  private TraceDiagnosticListRepresentation() {
050    // Nothing to see here, move along now.
051  }
052
053  @Override
054  public String toStringOf(@Nullable Object object) {
055    if (object == null) {
056      return "null";
057    }
058
059    @SuppressWarnings("unchecked")
060    var diagnostics = (Collection<? extends TraceDiagnostic<? extends JavaFileObject>>) object;
061
062    return "\n" + diagnostics
063        .stream()
064        .map(TraceDiagnosticRepresentation.getInstance()::toStringOf)
065        .map(this::indentAndBullet)
066        .collect(joining("\n\n"));
067  }
068
069  private String indentAndBullet(String repr) {
070    return " - " + repr.lines().collect(joining("\n   "));
071  }
072}