001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.wicket.util.value;
018
019import java.io.Serializable;
020
021import org.apache.wicket.util.lang.Primitives;
022
023
024/**
025 * A base class based on the Java <code>int</code> primitive for value classes that want to
026 * implement standard operations on that value without the pain of aggregating an
027 * <code>Integer</code> object.
028 * 
029 * @author Jonathan Locke
030 * @since 1.2.6
031 */
032public class IntValue implements Comparable<IntValue>, Serializable
033{
034        private static final long serialVersionUID = 1L;
035
036        /** the <code>int</code> value */
037        protected final int value;
038
039        /**
040         * Constructor.
041         * 
042         * @param value
043         *            the <code>int</code> value
044         */
045        public IntValue(final int value)
046        {
047                this.value = value;
048        }
049
050        /**
051         * @param that
052         *            The object to compare with
053         * @return 0 if equal, -1 if less than or 1 if greater than
054         */
055        @Override
056        public final int compareTo(final IntValue that)
057        {
058                if (value < that.value)
059                {
060                        return -1;
061                }
062
063                if (value > that.value)
064                {
065                        return 1;
066                }
067
068                return 0;
069        }
070
071        /**
072         * Compares this <code>Object</code> to a given <code>Object</code>.
073         * 
074         * @param that
075         *            the <code>Object</code> to compare with
076         * @return 0 if equal, -1 if less than the given <code>Object</code>'s value, or 1 if greater
077         *         than given <code>Object</code>'s value
078         */
079        @Override
080        public final boolean equals(final Object that)
081        {
082                if (that instanceof IntValue)
083                {
084                        return value == ((IntValue)that).value;
085                }
086
087                return false;
088        }
089
090        /**
091         * Compares this <code>IntValue</code> with a primitive <code>int</code> value.
092         * 
093         * @param value
094         *            the <code>int</code> value to compare with
095         * @return <code>true</code> if this <code>IntValue</code> is greater than the given
096         *         <code>int</code> value
097         */
098        public final boolean greaterThan(final int value)
099        {
100                return this.value > value;
101        }
102
103        /**
104         * Compares this <code>IntValue</code> with another <code>IntValue</code>.
105         * 
106         * @param that
107         *            the <code>IntValue</code> to compare with
108         * @return <code>true</code> if this <code>IntValue</code> is greater than the given
109         *         <code>IntValue</code>
110         */
111        public final boolean greaterThan(final IntValue that)
112        {
113                return value > that.value;
114        }
115
116        /**
117         * Returns the hash code for this <code>Object</code>.
118         * 
119         * @return hash code for this <code>Object</code>
120         */
121        @Override
122        public final int hashCode()
123        {
124                return Primitives.hashCode(value);
125        }
126
127        /**
128         * Compares this <code>IntValue</code> with a primitive <code>int</code> value.
129         * 
130         * @param that
131         *            the <code>int</code> value to compare with
132         * @return <code>true</code> if this <code>IntValue</code> is less than the given
133         *         <code>int</code> value
134         */
135        public final boolean lessThan(final int that)
136        {
137                return value < that;
138        }
139
140        /**
141         * Compares this <code>IntValue</code> with another <code>IntValue</code>.
142         * 
143         * @param that
144         *            the <code>IntValue</code> to compare with
145         * @return <code>true</code> if this <code>IntValue</code> is less than the given
146         *         <code>IntValue</code>
147         */
148        public final boolean lessThan(final IntValue that)
149        {
150                return value < that.value;
151        }
152
153        /**
154         * Converts this <code>LongValue</code> to a <code>String</code>.
155         * 
156         * @return a <code>String</code> representation of this <code>LongValue</code>
157         */
158        @Override
159        public String toString()
160        {
161                return String.valueOf(value);
162        }
163}