001/* Copyright (C) 2013 TU Dortmund 002 * This file is part of AutomataLib, http://www.automatalib.net/. 003 * 004 * AutomataLib is free software; you can redistribute it and/or 005 * modify it under the terms of the GNU Lesser General Public 006 * License version 3.0 as published by the Free Software Foundation. 007 * 008 * AutomataLib is distributed in the hope that it will be useful, 009 * but WITHOUT ANY WARRANTY; without even the implied warranty of 010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 011 * Lesser General Public License for more details. 012 * 013 * You should have received a copy of the GNU Lesser General Public 014 * License along with AutomataLib; if not, see 015 * http://www.gnu.de/documents/lgpl.en.html. 016 */ 017package net.automatalib.commons.util.strings; 018 019import java.io.IOException; 020 021/** 022 * Abstract base class for printables. 023 * 024 * Besides implementing the {@link Printable} interface, it provides a 025 * standard {@link #toString()} implementation using a {@link StringBuilder} 026 * and the {@link #print(Appendable)} method. 027 * 028 * @author Malte Isberner <malte.isberner@gmail.com> 029 * 030 */ 031public abstract class AbstractPrintable implements Printable { 032 033 public static String toString(Printable p) { 034 StringBuilder sb = new StringBuilder(); 035 try { 036 p.print(sb); 037 } 038 catch(IOException e) { 039 throw new IllegalStateException("Unexpected IOException thrown during operation on StringBuilder.", e); 040 // THIS SHOULD NOT HAPPEN 041 // since the StringBuilder methods do not throw. 042 } 043 return sb.toString(); 044 } 045 046 047 /* 048 * (non-Javadoc) 049 * @see java.lang.Object#toString() 050 */ 051 @Override 052 public String toString() { 053 return toString(this); 054 } 055}