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 io.github.ascopes.jct.diagnostics.TraceDiagnostic; 019import java.util.Locale; 020import org.assertj.core.presentation.Representation; 021import org.jspecify.annotations.Nullable; 022 023/** 024 * Simplified representation of a diagnostic. 025 * 026 * @author Ashley Scopes 027 * @since 0.0.1, rewritten in 4.0.0 028 */ 029public final class TraceDiagnosticRepresentation implements Representation { 030 031 private static final TraceDiagnosticRepresentation INSTANCE 032 = new TraceDiagnosticRepresentation(); 033 034 /** 035 * Get an instance of this diagnostic representation. 036 * 037 * @return the instance. 038 */ 039 public static TraceDiagnosticRepresentation getInstance() { 040 return INSTANCE; 041 } 042 043 private TraceDiagnosticRepresentation() { 044 // Nothing to see here, move along now. 045 } 046 047 @Override 048 public String toStringOf(@Nullable Object object) { 049 if (object == null) { 050 return "null"; 051 } 052 053 var diagnostic = (TraceDiagnostic<?>) object; 054 055 var builder = new StringBuilder("[") 056 .append(diagnostic.getKind()) 057 .append("]"); 058 059 var code = diagnostic.getCode(); 060 if (code != null) { 061 builder.append(' ') 062 .append(code); 063 } 064 065 if (diagnostic.getSource() != null) { 066 builder 067 .append(" in ") 068 .append(diagnostic.getSource().getName()) 069 .append(" (line ") 070 .append(diagnostic.getLineNumber()) 071 .append(", col ") 072 .append(diagnostic.getColumnNumber()) 073 .append(")"); 074 } 075 076 return builder 077 .append("\n") 078 .append("\n") 079 .append(diagnostic.getMessage(Locale.ROOT)) 080 .toString(); 081 } 082}