001 /*
002 * Created on Dec 21, 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.data;
016
017 import static org.fest.util.Objects.*;
018 import static org.fest.util.Strings.quote;
019
020 import java.util.Map;
021
022 /**
023 * Understands an entry in a <code>{@link Map}</code>.
024 *
025 * @author Yvonne Wang
026 */
027 public class MapEntry {
028
029 /** The key corresponding to this entry. */
030 public final Object key;
031
032 /** The value corresponding to this entry. */
033 public final Object value;
034
035 /**
036 * Creates a new map entry.
037 * @param key the key of the entry to create.
038 * @param value the value of the entry to create.
039 * @return the created map entry.
040 */
041 public static MapEntry entry(Object key, Object value) {
042 return new MapEntry(key, value);
043 }
044
045 private MapEntry(Object key, Object value) {
046 this.key = key;
047 this.value = value;
048 }
049
050 @Override public boolean equals(Object obj) {
051 if (this == obj) return true;
052 if (obj == null) return false;
053 if (getClass() != obj.getClass()) return false;
054 MapEntry other = (MapEntry) obj;
055 if (!areEqual(key, other.key)) return false;
056 return areEqual(value, other.value);
057 }
058
059 @Override public int hashCode() {
060 int result = 1;
061 result = HASH_CODE_PRIME * result + hashCodeFor(key);
062 result = HASH_CODE_PRIME * result + hashCodeFor(value);
063 return result;
064 }
065
066 @Override public String toString() {
067 return String.format("%s[key=%s, value=%s]", getClass().getSimpleName(), quote(key), quote(value));
068 }
069 }