001/*-------------------------------------------------------------------------+ 002| | 003| Copyright (c) 2009-2018 CQSE GmbH | 004| | 005+-------------------------------------------------------------------------*/ 006 007package eu.cqse.check.framework.preprocessor; 008 009import java.util.function.Function; 010 011import org.assertj.core.presentation.StandardRepresentation; 012 013import eu.cqse.check.framework.scanner.IToken; 014 015/** 016 * Generic, configurable representation. Can be used with 017 * {@link org.assertj.core.api.AbstractAssert#withRepresentation(org.assertj.core.presentation.Representation)}. 018 */ 019public final class GenericRepresentation<T> extends StandardRepresentation { 020 021 /** 022 * Representation for tokens that prints the token offset 023 * ({@link IToken#getOffset()}) in addition to the token text. 024 */ 025 public static final GenericRepresentation<IToken> TOKEN_OFFSET_REPRESENTATION = new GenericRepresentation<>( 026 (token) -> "offset " + token.getOffset() + " " + token.getText()); 027 /** 028 * Representation for tokens that prints the token line number 029 * ({@link IToken#getLineNumber()}) in addition to the token text. 030 */ 031 public static final GenericRepresentation<IToken> TOKEN_LINE_REPRESENTATION = new GenericRepresentation<>( 032 (token) -> "line " + token.getLineNumber() + " " + token.getText()); 033 034 private Function<T, String> representationProvider; 035 036 public GenericRepresentation(Function<T, String> representationProvider) { 037 this.representationProvider = representationProvider; 038 } 039 040 @SuppressWarnings("unchecked") 041 @Override 042 public String toStringOf(Object object) { 043 if (object instanceof IToken) { 044 return representationProvider.apply((T) object); 045 } 046 return super.toStringOf(object); 047 } 048}