001 /*
002 * Created on Oct 23, 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
019 /**
020 * A positive offset.
021 * @param <T> the type of the offset value.
022 *
023 * @author Alex Ruiz
024 * @author Yvonne Wang
025 */
026 public class Offset<T extends Number> {
027
028 /** The value of this offset. */
029 public final T value;
030
031 /**
032 * Creates a new double </code>{@link Offset}</code>.
033 * @param value the value of the offset.
034 * @return the created {@code Offset}.
035 * @throws NullPointerException if the given value is {@code null}.
036 * @throws IllegalArgumentException if the given value is negative.
037 */
038 public static Offset<Double> offset(Double value) {
039 checkIsNotNull(value);
040 if (value.doubleValue() < 0d) throw valueNotPositive();
041 return new Offset<Double>(value);
042 }
043
044 /**
045 * Creates a new float </code>{@link Offset}</code>.
046 * @param value the value of the offset.
047 * @return the created {@code Offset}.
048 * @throws NullPointerException if the given value is {@code null}.
049 * @throws IllegalArgumentException if the given value is negative.
050 */
051 public static Offset<Float> offset(Float value) {
052 checkIsNotNull(value);
053 if (value.floatValue() < 0f) throw valueNotPositive();
054 return new Offset<Float>(value);
055 }
056
057 /**
058 * Creates a new integer </code>{@link Offset}</code>.
059 * @param value the value of the offset.
060 * @return the created {@code Offset}.
061 * @throws NullPointerException if the given value is {@code null}.
062 * @throws IllegalArgumentException if the given value is negative.
063 */
064 public static Offset<Integer> offset(Integer value) {
065 checkIsNotNull(value);
066 if (value.intValue() < 0) throw valueNotPositive();
067 return new Offset<Integer>(value);
068 }
069
070 private static <T extends Number> void checkIsNotNull(T value) {
071 if (value == null) throw new NullPointerException("The value of the offset to create should not be null");
072 }
073
074 private static IllegalArgumentException valueNotPositive() {
075 return new IllegalArgumentException("The value of the offset should be greater than zero");
076 }
077
078 private Offset(T value) {
079 this.value = value;
080 }
081
082 @Override public boolean equals(Object obj) {
083 if (this == obj) return true;
084 if (obj == null) return false;
085 if (getClass() != obj.getClass()) return false;
086 Offset<?> other = (Offset<?>) obj;
087 return (areEqual(value, other.value));
088 }
089
090 @Override public int hashCode() {
091 int result = 1;
092 result = HASH_CODE_PRIME * result + hashCodeFor(value);
093 return result;
094 }
095
096 @Override public String toString() {
097 return String.format("%s[value=%s]", getClass().getSimpleName(), value);
098 }
099 }