001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.commons.util;
021
022import java.util.Date;
023
024import org.apache.isis.core.commons.lang.DateExtensions;
025import org.apache.isis.core.commons.lang.ObjectExtensions;
026
027public final class ToString {
028
029
030    public static ToString createAnonymous(final Object object) {
031        final ToString string = new ToString();
032        string.append(ObjectExtensions.classBaseName(object));
033        string.append("[");
034        return string;
035    }
036
037    // //////////////////////////////////////
038
039    private boolean addComma = false;
040    private final StringBuffer buf;
041    private boolean useLineBreaks;
042
043    private ToString() {
044        buf = new StringBuffer();
045    }
046
047    public ToString(final Object forObject) {
048        buf = new StringBuffer();
049        ObjectExtensions.appendToString(forObject, buf);
050        buf.append("[");
051    }
052
053    public ToString(final Object forObject, final int id) {
054        buf = new StringBuffer();
055        buf.append(ObjectExtensions.classBaseName(forObject));
056        buf.append("#");
057        buf.append(id);
058        buf.append("[");
059    }
060
061    public ToString(final Object forObject, final String text) {
062        this(forObject);
063        buf.append(text);
064        addComma = text.length() > 0;
065    }
066
067    // //////////////////////////////////////
068
069    public ToString append(final String text) {
070        buf.append(text);
071        return this;
072    }
073
074    public ToString append(final String name, final boolean flag) {
075        append(name, flag ? "true" : "false");
076        return this;
077    }
078
079    public ToString append(final String name, final byte number) {
080        append(name, Byte.toString(number));
081        return this;
082    }
083
084    public ToString append(final String name, final double number) {
085        append(name, Double.toString(number));
086        return this;
087    }
088
089    public ToString append(final String name, final float number) {
090        append(name, Float.toString(number));
091        return this;
092    }
093
094    public ToString append(final String name, final int number) {
095        append(name, Integer.toString(number));
096        return this;
097    }
098
099    public ToString append(final String name, final long number) {
100        append(name, Long.toString(number));
101        return this;
102    }
103
104    public ToString append(final String name, final Object object) {
105        append(name, object == null ? "null" : object.toString());
106        return this;
107    }
108
109    public ToString append(final String name, final short number) {
110        append(name, Short.toString(number));
111        return this;
112    }
113
114    public ToString append(final String name, final String string) {
115        if (addComma) {
116            this.buf.append(',');
117            if (useLineBreaks) {
118                this.buf.append("\n\t");
119            }
120        } else {
121            addComma = true;
122        }
123        this.buf.append(name);
124        this.buf.append('=');
125        this.buf.append(string);
126
127        return this;
128    }
129
130    public ToString appendAsHex(final String name, final long number) {
131        append(name, "#" + Long.toHexString(number));
132        return this;
133    }
134
135    public void appendAsTimestamp(final String name, final Date date) {
136        final String dateString = DateExtensions.asTimestamp(date);
137        append(name, dateString);
138    }
139
140    public void appendTruncated(final String name, final String string, final int maxLength) {
141        if (string.length() > maxLength) {
142            append(name, string.substring(0, maxLength));
143            append("...");
144        } else {
145            append(name, string);
146        }
147    }
148
149    public void setAddComma() {
150        this.addComma = true;
151    }
152
153    public void setUseLineBreaks(final boolean useLineBreaks) {
154        this.useLineBreaks = useLineBreaks;
155    }
156
157    // //////////////////////////////////////
158
159    @Override
160    public String toString() {
161        buf.append(']');
162        return buf.toString();
163    }
164}