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.Args;
022import org.apache.wicket.util.lang.Primitives;
023
024
025/**
026 * A base class based on the Java <code>long</code> primitive for value classes that want to
027 * implement standard operations on that value without the pain of aggregating a <code>Long</code>
028 * object.
029 * 
030 * @author Jonathan Locke
031 * @since 1.2.6
032 */
033public class LongValue implements Comparable<LongValue>, Serializable
034{
035        private static final long serialVersionUID = 1L;
036
037        /** the <code>long</code> value */
038        protected final long value;
039
040        /**
041         * Constructor.
042         * 
043         * @param value
044         *            the <code>long</code> value
045         */
046        public LongValue(final long value)
047        {
048                this.value = value;
049        }
050
051        /**
052         * Compares this <code>Object</code> to a given <code>Object</code>.
053         * 
054         * @param that
055         *            the <code>Object</code> to compare with
056         * @return 0 if equal, -1 if less than the given <code>Object</code>'s value, or 1 if greater
057         *         than given <code>Object</code>'s value
058         */
059        @Override
060        public final int compareTo(final LongValue that)
061        {
062                if (value < that.value)
063                {
064                        return -1;
065                }
066
067                if (value > that.value)
068                {
069                        return 1;
070                }
071
072                return 0;
073        }
074
075        /**
076         * Tests for equality.
077         * 
078         * @param that
079         *            the <code>Object</code> to compare with
080         * @return <code>true</code> if this <code>Object</code>'s value is equal to the given
081         *         <code>Object</code>'s value
082         */
083        @Override
084        public final boolean equals(final Object that)
085        {
086                if (that instanceof LongValue)
087                {
088                        return value == ((LongValue)that).value;
089                }
090
091                return false;
092        }
093
094        /**
095         * Compares this <code>LongValue</code> with a primitive <code>long</code> value.
096         * 
097         * @param value
098         *            the <code>long</code> value to compare with
099         * @return <code>true</code> if this <code>LongValue</code> is greater than the given
100         *         <code>long</code> value
101         */
102        public final boolean greaterThan(final long value)
103        {
104                return this.value > value;
105        }
106
107        /**
108         * Compares this <code>LongValue</code> with a primitive <code>long</code> value.
109         * 
110         * @param value
111         *            the <code>long</code> value to compare with
112         * @return <code>true</code> if this <code>LongValue</code> is greater than or equal to the
113         *         given <code>long</code> value
114         */
115        public final boolean greaterThanOrEqual(final long value)
116        {
117                return this.value >= value;
118        }
119
120        /**
121         * Compares this <code>LongValue</code> with another <code>LongValue</code>.
122         * 
123         * @param that
124         *            the <code>LongValue</code> to compare with
125         * @return <code>true</code> if this <code>LongValue</code> is greater than the given
126         *         <code>LongValue</code>
127         */
128        public final boolean greaterThan(final LongValue that)
129        {
130                return value > that.value;
131        }
132
133        /**
134         * Compares this <code>LongValue</code> with another <code>LongValue</code>.
135         * 
136         * @param that
137         *            the <code>LongValue</code> to compare with
138         * @return <code>true</code> if this <code>LongValue</code> is greater than or equal to the
139         *         given <code>LongValue</code>
140         */
141        public final boolean greaterThanOrEqual(final LongValue that)
142        {
143                return value >= that.value;
144        }
145
146        /**
147         * Returns the hash code for this <code>Object</code>.
148         * 
149         * @return hash code for this <code>Object</code>
150         */
151        @Override
152        public final int hashCode()
153        {
154                return Primitives.hashCode(value);
155        }
156
157        /**
158         * Compares this <code>LongValue</code> with a primitive <code>long</code> value.
159         * 
160         * @param that
161         *            the <code>long</code> value to compare with
162         * @return <code>true</code> if this <code>LongValue</code> is less than the given
163         *         <code>long</code> value
164         */
165        public final boolean lessThan(final long that)
166        {
167                return value < that;
168        }
169
170        /**
171         * Compares this <code>LongValue</code> with a primitive <code>long</code> value.
172         * 
173         * @param that
174         *            the <code>long</code> value to compare with
175         * @return <code>true</code> if this <code>LongValue</code> is less than or equal to the given
176         *         <code>long</code> value
177         */
178        public final boolean lessThanOrEqual(final long that)
179        {
180                return value <= that;
181        }
182
183        /**
184         * Compares this <code>LongValue</code> with another <code>LongValue</code>.
185         * 
186         * @param that
187         *            the <code>LongValue</code> value to compare with
188         * @return <code>true</code> if this <code>LongValue</code> is less than the given
189         *         <code>LongValue</code>
190         */
191        public final boolean lessThan(final LongValue that)
192        {
193                return value < that.value;
194        }
195
196        /**
197         * Compares this <code>LongValue</code> with another <code>LongValue</code>.
198         * 
199         * @param that
200         *            the <code>LongValue</code> value to compare with
201         * @return <code>true</code> if this <code>LongValue</code> is less than or equal to the given
202         *         <code>LongValue</code>
203         */
204        public final boolean lessThanOrEqual(final LongValue that)
205        {
206                return value <= that.value;
207        }
208
209        /**
210         * Converts this <code>LongValue</code> to a <code>String</code>.
211         * 
212         * @return a <code>String</code> representation of this <code>LongValue</code>
213         */
214        @Override
215        public String toString()
216        {
217                return String.valueOf(value);
218        }
219
220        /**
221         * Returns the min of the two long values.
222         * 
223         * @param <T>
224         * @param lhs
225         * @param rhs
226         * @throws IllegalArgumentException
227         *             if either argument is {@code null}
228         * @return min value
229         */
230        public static <T extends LongValue> T min(final T lhs, final T rhs)
231        {
232                Args.notNull(lhs, "lhs");
233                Args.notNull(rhs, "rhs");
234                if (lhs.compareTo(rhs) < 0)
235                {
236                        return lhs;
237                }
238                return rhs;
239        }
240
241        /**
242         * Returns the max of the two long values.
243         * 
244         * @param <T>
245         * @param lhs
246         * @param rhs
247         * @throws IllegalArgumentException
248         *             if either argument is {@code null}
249         * @return max value
250         */
251        public static <T extends LongValue> T max(final T lhs, final T rhs)
252        {
253                Args.notNull(lhs, "lhs");
254                Args.notNull(rhs, "rhs");
255                if (lhs.compareTo(rhs) > 0)
256                {
257                        return lhs;
258                }
259                return rhs;
260        }
261
262        /**
263         * Null-safe version of {@link LongValue#max}. Nulls are considered less then any concrete
264         * value.
265         * 
266         * @param <T>
267         * @param lhs
268         * @param rhs
269         * @return max of two values or {@code null} if they are both null
270         */
271        public static <T extends LongValue> T maxNullSafe(final T lhs, final T rhs)
272        {
273                if (lhs == rhs)
274                {
275                        return lhs;
276                }
277                else if (lhs == null)
278                {
279                        return rhs;
280                }
281                else if (rhs == null)
282                {
283                        return lhs;
284                }
285                return max(lhs, rhs);
286        }
287}