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.*;
023    
024    /**
025     * Obtains the {@code toString} representation of an object.
026     *
027     * @author Alex Ruiz
028     * @author Joel Costigliola
029     * @author Yvonne Wang
030     */
031    public final class ToString {
032    
033      /**
034       * Returns the {@code toString} representation of the given object. It may or not the object's own implementation of
035       * {@code toString}.
036       * @param o the given object.
037       * @return the {@code toString} representation of the given object.
038       */
039      public static String toStringOf(Object o) {
040        if (isArray(o)) return Arrays.format(o);
041        if (o instanceof Calendar) return toStringOf((Calendar) o);
042        if (o instanceof Class<?>) return toStringOf((Class<?>) o);
043        if (o instanceof Collection<?>) return toStringOf((Collection<?>) o);
044        if (o instanceof Date) return toStringOf((Date) o);
045        if (o instanceof Dimension) return toStringOf((Dimension) o);
046        if (o instanceof File) return toStringOf((File) o);
047        if (o instanceof Map<?, ?>) return toStringOf((Map<?, ?>) o);
048        if (o instanceof String) return quote((String) o);
049        return o == null ? null : o.toString();
050      }
051    
052      private static String toStringOf(Calendar c) {
053              return Dates.format(c);
054      }
055    
056      private static String toStringOf(Class<?> c) {
057        return c.getName();
058      }
059    
060      private static String toStringOf(Collection<?> c) {
061        return Collections.format(c);
062      }
063    
064      private static String toStringOf(Date d) {
065              return Dates.format(d);
066      }
067    
068      private static String toStringOf(Dimension d) {
069        return String.format("(w=%s, h=%s)", d.width, d.height);
070      }
071    
072      private static String toStringOf(File f) {
073        return f.getAbsolutePath();
074      }
075    
076      private static String toStringOf(Map<?, ?> m) {
077        return Maps.format(m);
078      }
079    
080      private ToString() {}
081    }