• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  *  * Redistributions of source code must retain the above copyright notice,
10  *    this list of conditions and the following disclaimer.
11  *
12  *  * Redistributions in binary form must reproduce the above copyright notice,
13  *    this list of conditions and the following disclaimer in the documentation
14  *    and/or other materials provided with the distribution.
15  *
16  *  * Neither the name of JSR-310 nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 package org.threeten.bp.jdk8;
33 
34 /**
35  * A set of utility methods that provide additional functionality for working
36  * with dates and times.
37  * <p>
38  * The contents of this class replace functionality available in JDK 8.
39  *
40  * <h3>Specification for implementors</h3>
41  * This is a thread-safe utility class.
42  * All returned classes are immutable and thread-safe.
43  */
44 public final class Jdk8Methods {
45 
46     /**
47      * Private constructor since this is a utility class.
48      */
Jdk8Methods()49     private Jdk8Methods() {
50     }
51 
52     //-----------------------------------------------------------------------
53     /**
54      * Ensures that the argument is non-null.
55      *
56      * @param <T>  the value type
57      * @param value  the value to check
58      * @return the checked non-null value
59      * @throws NullPointerException if the value is null
60      */
requireNonNull(T value)61     public static <T> T requireNonNull(T value) {
62         if (value == null) {
63             throw new NullPointerException("Value must not be null");
64         }
65         return value;
66     }
67 
68     /**
69      * Ensures that the argument is non-null.
70      *
71      * @param <T>  the value type
72      * @param value  the value to check
73      * @param parameterName  the name of the parameter
74      * @return the checked non-null value
75      * @throws NullPointerException if the value is null
76      */
requireNonNull(T value, String parameterName)77     public static <T> T requireNonNull(T value, String parameterName) {
78         if (value == null) {
79             throw new NullPointerException(parameterName + " must not be null");
80         }
81         return value;
82     }
83 
84     //-----------------------------------------------------------------------
85     /**
86      * Compares two objects.
87      *
88      * @param a  the first value
89      * @param b  the second value
90      * @return the result
91      */
equals(Object a, Object b)92     public static boolean equals(Object a, Object b) {
93         if (a == null) {
94             return b == null;
95         }
96         if (b == null) {
97             return false;
98         }
99         return a.equals(b);
100     }
101 
102     /**
103      * Compares two ints.
104      *
105      * @param a  the first value
106      * @param b  the second value
107      * @return the result
108      */
compareInts(int a, int b)109     public static int compareInts(int a, int b) {
110         if (a < b) {
111             return -1;
112         }
113         if (a > b) {
114             return 1;
115         }
116         return 0;
117     }
118 
119     /**
120      * Compares two longs.
121      *
122      * @param a  the first value
123      * @param b  the second value
124      * @return the result
125      */
compareLongs(long a, long b)126     public static int compareLongs(long a, long b) {
127         if (a < b) {
128             return -1;
129         }
130         if (a > b) {
131             return 1;
132         }
133         return 0;
134     }
135 
136     //-----------------------------------------------------------------------
137     /**
138      * Safely adds two int values.
139      *
140      * @param a  the first value
141      * @param b  the second value
142      * @return the result
143      * @throws ArithmeticException if the result overflows an int
144      */
safeAdd(int a, int b)145     public static int safeAdd(int a, int b) {
146         int sum = a + b;
147         // check for a change of sign in the result when the inputs have the same sign
148         if ((a ^ sum) < 0 && (a ^ b) >= 0) {
149             throw new ArithmeticException("Addition overflows an int: " + a + " + " + b);
150         }
151         return sum;
152     }
153 
154     /**
155      * Safely adds two long values.
156      *
157      * @param a  the first value
158      * @param b  the second value
159      * @return the result
160      * @throws ArithmeticException if the result overflows a long
161      */
safeAdd(long a, long b)162     public static long safeAdd(long a, long b) {
163         long sum = a + b;
164         // check for a change of sign in the result when the inputs have the same sign
165         if ((a ^ sum) < 0 && (a ^ b) >= 0) {
166             throw new ArithmeticException("Addition overflows a long: " + a + " + " + b);
167         }
168         return sum;
169     }
170 
171     //-----------------------------------------------------------------------
172     /**
173      * Safely subtracts one int from another.
174      *
175      * @param a  the first value
176      * @param b  the second value to subtract from the first
177      * @return the result
178      * @throws ArithmeticException if the result overflows an int
179      */
safeSubtract(int a, int b)180     public static int safeSubtract(int a, int b) {
181         int result = a - b;
182         // check for a change of sign in the result when the inputs have the different signs
183         if ((a ^ result) < 0 && (a ^ b) < 0) {
184             throw new ArithmeticException("Subtraction overflows an int: " + a + " - " + b);
185         }
186         return result;
187     }
188 
189     /**
190      * Safely subtracts one long from another.
191      *
192      * @param a  the first value
193      * @param b  the second value to subtract from the first
194      * @return the result
195      * @throws ArithmeticException if the result overflows a long
196      */
safeSubtract(long a, long b)197     public static long safeSubtract(long a, long b) {
198         long result = a - b;
199         // check for a change of sign in the result when the inputs have the different signs
200         if ((a ^ result) < 0 && (a ^ b) < 0) {
201             throw new ArithmeticException("Subtraction overflows a long: " + a + " - " + b);
202         }
203         return result;
204     }
205 
206     //-----------------------------------------------------------------------
207     /**
208      * Safely multiply one int by another.
209      *
210      * @param a  the first value
211      * @param b  the second value
212      * @return the result
213      * @throws ArithmeticException if the result overflows an int
214      */
safeMultiply(int a, int b)215     public static int safeMultiply(int a, int b) {
216         long total = (long) a * (long) b;
217         if (total < Integer.MIN_VALUE || total > Integer.MAX_VALUE) {
218             throw new ArithmeticException("Multiplication overflows an int: " + a + " * " + b);
219         }
220         return (int) total;
221     }
222 
223     /**
224      * Safely multiply a long by an int.
225      *
226      * @param a  the first value
227      * @param b  the second value
228      * @return the new total
229      * @throws ArithmeticException if the result overflows a long
230      */
safeMultiply(long a, int b)231     public static long safeMultiply(long a, int b) {
232         switch (b) {
233             case -1:
234                 if (a == Long.MIN_VALUE) {
235                     throw new ArithmeticException("Multiplication overflows a long: " + a + " * " + b);
236                 }
237                 return -a;
238             case 0:
239                 return 0L;
240             case 1:
241                 return a;
242         }
243         long total = a * b;
244         if (total / b != a) {
245             throw new ArithmeticException("Multiplication overflows a long: " + a + " * " + b);
246         }
247         return total;
248     }
249 
250     /**
251      * Multiply two values throwing an exception if overflow occurs.
252      *
253      * @param a  the first value
254      * @param b  the second value
255      * @return the new total
256      * @throws ArithmeticException if the result overflows a long
257      */
safeMultiply(long a, long b)258     public static long safeMultiply(long a, long b) {
259         if (b == 1) {
260             return a;
261         }
262         if (a == 1) {
263             return b;
264         }
265         if (a == 0 || b == 0) {
266             return 0;
267         }
268         long total = a * b;
269         if (total / b != a || (a == Long.MIN_VALUE && b == -1) || (b == Long.MIN_VALUE && a == -1)) {
270             throw new ArithmeticException("Multiplication overflows a long: " + a + " * " + b);
271         }
272         return total;
273     }
274 
275     //-----------------------------------------------------------------------
276     /**
277      * Safely convert a long to an int.
278      *
279      * @param value  the value to convert
280      * @return the int value
281      * @throws ArithmeticException if the result overflows an int
282      */
safeToInt(long value)283     public static int safeToInt(long value) {
284         if (value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
285             throw new ArithmeticException("Calculation overflows an int: " + value);
286         }
287         return (int) value;
288     }
289 
290     //-----------------------------------------------------------------------
291     /**
292      * Returns the floor division.
293      * <p>
294      * This returns {@code 0} for {@code floorDiv(0, 4)}.<br />
295      * This returns {@code -1} for {@code floorDiv(-1, 4)}.<br />
296      * This returns {@code -1} for {@code floorDiv(-2, 4)}.<br />
297      * This returns {@code -1} for {@code floorDiv(-3, 4)}.<br />
298      * This returns {@code -1} for {@code floorDiv(-4, 4)}.<br />
299      * This returns {@code -2} for {@code floorDiv(-5, 4)}.<br />
300      *
301      * @param a  the dividend
302      * @param b  the divisor
303      * @return the floor division
304      */
floorDiv(long a, long b)305     public static long floorDiv(long a, long b) {
306         return (a >= 0 ? a / b : ((a + 1) / b) - 1);
307     }
308 
309     /**
310      * Returns the floor modulus.
311      * <p>
312      * This returns {@code 0} for {@code floorMod(0, 4)}.<br />
313      * This returns {@code 1} for {@code floorMod(-1, 4)}.<br />
314      * This returns {@code 2} for {@code floorMod(-2, 4)}.<br />
315      * This returns {@code 3} for {@code floorMod(-3, 4)}.<br />
316      * This returns {@code 0} for {@code floorMod(-4, 4)}.<br />
317      *
318      * @param a  the dividend
319      * @param b  the divisor
320      * @return the floor modulus (positive)
321      */
floorMod(long a, long b)322     public static long floorMod(long a, long b) {
323         return ((a % b) + b) % b;
324     }
325 
326     /**
327      * Returns the floor modulus.
328      * <p>
329      * This returns {@code 0} for {@code floorMod(0, 4)}.<br />
330      * This returns {@code 3} for {@code floorMod(-1, 4)}.<br />
331      * This returns {@code 2} for {@code floorMod(-2, 4)}.<br />
332      * This returns {@code 1} for {@code floorMod(-3, 4)}.<br />
333      * This returns {@code 0} for {@code floorMod(-4, 4)}.<br />
334      * This returns {@code 3} for {@code floorMod(-5, 4)}.<br />
335      *
336      * @param a  the dividend
337      * @param b  the divisor
338      * @return the floor modulus (positive)
339      */
floorMod(long a, int b)340     public static int floorMod(long a, int b) {
341         return (int) (((a % b) + b) % b);
342     }
343 
344     /**
345      * Returns the floor division.
346      * <p>
347      * This returns {@code 1} for {@code floorDiv(3, 3)}.<br />
348      * This returns {@code 0} for {@code floorDiv(2, 3)}.<br />
349      * This returns {@code 0} for {@code floorDiv(1, 3)}.<br />
350      * This returns {@code 0} for {@code floorDiv(0, 3)}.<br />
351      * This returns {@code -1} for {@code floorDiv(-1, 3)}.<br />
352      * This returns {@code -1} for {@code floorDiv(-2, 3)}.<br />
353      * This returns {@code -1} for {@code floorDiv(-3, 3)}.<br />
354      * This returns {@code -2} for {@code floorDiv(-4, 3)}.<br />
355      *
356      * @param a  the dividend
357      * @param b  the divisor
358      * @return the floor division
359      */
floorDiv(int a, int b)360     public static int floorDiv(int a, int b) {
361         return (a >= 0 ? a / b : ((a + 1) / b) - 1);
362     }
363 
364     /**
365      * Returns the floor modulus.
366      * <p>
367      * This returns {@code 0} for {@code floorMod(3, 3)}.<br />
368      * This returns {@code 2} for {@code floorMod(2, 3)}.<br />
369      * This returns {@code 1} for {@code floorMod(1, 3)}.<br />
370      * This returns {@code 0} for {@code floorMod(0, 3)}.<br />
371      * This returns {@code 2} for {@code floorMod(-1, 3)}.<br />
372      * This returns {@code 1} for {@code floorMod(-2, 3)}.<br />
373      * This returns {@code 0} for {@code floorMod(-3, 3)}.<br />
374      * This returns {@code 2} for {@code floorMod(-4, 3)}.<br />
375      *
376      * @param a  the dividend
377      * @param b  the divisor
378      * @return the floor modulus (positive)
379      */
floorMod(int a, int b)380     public static int floorMod(int a, int b) {
381         return ((a % b) + b) % b;
382     }
383 
384 }
385