001 /*
002 * Created on Jan 11, 2011
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 @2011 the original author or authors.
014 */
015 package org.fest.assertions.error;
016
017 import static org.fest.util.Strings.isEmpty;
018
019 import org.fest.assertions.description.Description;
020 import org.fest.util.VisibleForTesting;
021
022 /**
023 * Formats the <code>{@link Description}</code>s to be included in assertion errors.
024 *
025 * @author Alex Ruiz
026 */
027 public class DescriptionFormatter {
028
029 private static final DescriptionFormatter INSTANCE = new DescriptionFormatter();
030
031 /**
032 * Returns the singleton instance of this class.
033 * @return the singleton instance of this class.
034 */
035 public static DescriptionFormatter instance() {
036 return INSTANCE;
037 }
038
039 @VisibleForTesting DescriptionFormatter() {}
040
041 /**
042 * Formats the given <code>{@link Description}</code> by surrounding its text value with square brackets and adding a
043 * space at the end.
044 * @param d the description to format. It can be {@code null}.
045 * @return the formatted description, or an empty {@code String} if the the {@code Description} is {@code null}.
046 */
047 public String format(Description d) {
048 String s = (d != null) ? d.value() : null;
049 if (isEmpty(s)) return "";
050 return String.format("[%s] ", s);
051 }
052
053 }