001 /* 002 * Created on Oct 18, 2010 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 @2010-2011 the original author or authors. 014 */ 015 package org.fest.assertions.error; 016 017 import static java.lang.String.format; 018 019 import static org.fest.util.Arrays.format; 020 import static org.fest.util.Objects.*; 021 import static org.fest.util.Strings.quote; 022 023 import java.util.Arrays; 024 025 import org.fest.assertions.description.Description; 026 import org.fest.util.VisibleForTesting; 027 028 /** 029 * A factory of error messages typically shown when an assertion fails. 030 * 031 * @author Alex Ruiz 032 */ 033 public class BasicErrorMessageFactory implements ErrorMessageFactory { 034 035 private final String format; 036 private final Object[] arguments; 037 038 @VisibleForTesting 039 MessageFormatter formatter = MessageFormatter.instance(); 040 041 /** 042 * Creates a new </code>{@link BasicErrorMessageFactory}</code>. 043 * @param format the format string. 044 * @param arguments arguments referenced by the format specifiers in the format string. 045 */ 046 public BasicErrorMessageFactory(String format, Object... arguments) { 047 this.format = format; 048 this.arguments = arguments; 049 } 050 051 /** {@inheritDoc} */ 052 public String create(Description d) { 053 return formatter.format(d, format, arguments); 054 } 055 056 @Override 057 public boolean equals(Object obj) { 058 if (this == obj) return true; 059 if (obj == null) return false; 060 if (getClass() != obj.getClass()) return false; 061 BasicErrorMessageFactory other = (BasicErrorMessageFactory) obj; 062 if (!areEqual(format, other.format)) return false; 063 return Arrays.equals(arguments, other.arguments); 064 } 065 066 @Override 067 public int hashCode() { 068 int result = 1; 069 result = HASH_CODE_PRIME * result + hashCodeFor(format); 070 result = HASH_CODE_PRIME * result + Arrays.hashCode(arguments); 071 return result; 072 } 073 074 @Override 075 public String toString() { 076 return format("%s[format=%s, arguments=%s]", getClass().getSimpleName(), quote(format), format(arguments)); 077 } 078 079 }