001 /*
002 * Created on Oct 7, 2009
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
005 * the License. You may obtain a copy of the License at
006 *
007 * http://www.apache.org/licenses/LICENSE-2.0
008 *
009 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
010 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
011 * specific language governing permissions and limitations under the License.
012 *
013 * Copyright @2009-2011-2010 the original author or authors.
014 */
015 package org.fest.util;
016
017 import static org.fest.util.Arrays.isArray;
018 import static org.fest.util.Strings.quote;
019
020 import java.awt.Dimension;
021 import java.io.File;
022 import java.util.Calendar;
023 import java.util.Collection;
024 import java.util.Comparator;
025 import java.util.Date;
026 import java.util.Map;
027
028 /**
029 * Obtains the {@code toString} representation of an object.
030 *
031 * @author Alex Ruiz
032 * @author Joel Costigliola
033 * @author Yvonne Wang
034 */
035 public final class ToString {
036
037 /**
038 * Returns the {@code toString} representation of the given object. It may or not the object's own implementation of
039 * {@code toString}.
040 * @param o the given object.
041 * @return the {@code toString} representation of the given object.
042 */
043 public static String toStringOf(Object o) {
044 if (isArray(o)) return Arrays.format(o);
045 if (o instanceof Calendar) return toStringOf((Calendar) o);
046 if (o instanceof Class<?>) return toStringOf((Class<?>) o);
047 if (o instanceof Collection<?>) return toStringOf((Collection<?>) o);
048 if (o instanceof Date) return toStringOf((Date) o);
049 if (o instanceof Float) return toStringOf((Float) o);
050 if (o instanceof Long) return toStringOf((Long) o);
051 if (o instanceof Dimension) return toStringOf((Dimension) o);
052 if (o instanceof File) return toStringOf((File) o);
053 if (o instanceof Map<?, ?>) return toStringOf((Map<?, ?>) o);
054 if (o instanceof String) return quote((String) o);
055 if (o instanceof Comparator) return toStringOf((Comparator<?>) o);
056 return o == null ? null : o.toString();
057 }
058
059 private static String toStringOf(Comparator<?> comparator) {
060 String comparatorSimpleClassName = comparator.getClass().getSimpleName();
061 return quote(comparatorSimpleClassName.length() > 0 ? comparatorSimpleClassName : "anonymous comparator class");
062 }
063
064 private static String toStringOf(Calendar c) {
065 return Dates.formatAsDatetime(c);
066 }
067
068 private static String toStringOf(Class<?> c) {
069 return c.getName();
070 }
071
072 private static String toStringOf(Collection<?> c) {
073 return Collections.format(c);
074 }
075
076 private static String toStringOf(Date d) {
077 return Dates.formatAsDatetime(d);
078 }
079
080 private static String toStringOf(Float f) {
081 return String.format("%sf", f);
082 }
083
084 private static String toStringOf(Long l) {
085 return String.format("%sL", l);
086 }
087
088 private static String toStringOf(Dimension d) {
089 return String.format("(w=%s, h=%s)", d.width, d.height);
090 }
091
092 private static String toStringOf(File f) {
093 return f.getAbsolutePath();
094 }
095
096 private static String toStringOf(Map<?, ?> m) {
097 return Maps.format(m);
098 }
099
100 private ToString() {}
101 }