001 /* 002 * Created on Oct 26, 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.HASH_CODE_PRIME; 018 019 /** 020 * An index. 021 * 022 * @author Alex Ruiz 023 */ 024 public class Index { 025 026 /** The value of this index. */ 027 public final int value; 028 029 /** 030 * Creates a new <code>{@link Index}</code>. 031 * @param index the value of the index. 032 * @return the created <code>Index</code>. 033 * @throws IllegalArgumentException if the given value is negative. 034 */ 035 public static Index atIndex(int index) { 036 if (index < 0) throw new IllegalArgumentException("The value of the index should not be negative"); 037 return new Index(index); 038 } 039 040 private Index(int value) { 041 this.value = value; 042 } 043 044 @Override public boolean equals(Object obj) { 045 if (this == obj) return true; 046 if (obj == null) return false; 047 if (getClass() != obj.getClass()) return false; 048 Index other = (Index) obj; 049 return value == other.value; 050 } 051 052 @Override public int hashCode() { 053 int result = 1; 054 result = HASH_CODE_PRIME * result + value; 055 return result; 056 } 057 058 @Override public String toString() { 059 return String.format("%s[value=%d]", getClass().getSimpleName(), value); 060 } 061 }