01: package it.unimi.dsi.fastutil;
02:
03: /*
04: * fastutil: Fast & compact type-specific collections for Java
05: *
06: * Copyright (C) 2002-2008 Sebastiano Vigna
07: *
08: * This library is free software; you can redistribute it and/or
09: * modify it under the terms of the GNU Lesser General Public
10: * License as published by the Free Software Foundation; either
11: * version 2.1 of the License, or (at your option) any later version.
12: *
13: * This library is distributed in the hope that it will be useful,
14: * but WITHOUT ANY WARRANTY; without even the implied warranty of
15: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16: * Lesser General Public License for more details.
17: *
18: * You should have received a copy of the GNU Lesser General Public
19: * License along with this library; if not, write to the Free Software
20: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21: *
22: */
23:
24: /** A class providing static methods and objects that do useful things with arrays.
25: *
26: * @see Arrays
27: */
28:
29: public class Arrays {
30:
31: private Arrays() {
32: }
33:
34: /** Ensures that a range given by its first (inclusive) and last (exclusive) elements fits an array of given length.
35: *
36: * <P>This method may be used whenever an array range check is needed.
37: *
38: * @param arrayLength an array length.
39: * @param from a start index (inclusive).
40: * @param to an end index (inclusive).
41: * @throws IllegalArgumentException if <code>from</code> is greater than <code>to</code>.
42: * @throws ArrayIndexOutOfBoundsException if <code>from</code> or <code>to</code> are greater than <code>arrayLength</code> or negative.
43: */
44: public static void ensureFromTo(final int arrayLength,
45: final int from, final int to) {
46: if (from < 0)
47: throw new ArrayIndexOutOfBoundsException("Start index ("
48: + from + ") is negative");
49: if (from > to)
50: throw new IllegalArgumentException("Start index (" + from
51: + ") is greater than end index (" + to + ")");
52: if (to > arrayLength)
53: throw new ArrayIndexOutOfBoundsException("End index (" + to
54: + ") is greater than array length (" + arrayLength
55: + ")");
56: }
57:
58: /** Ensures that a range given by an offset and a length fits an array of given length.
59: *
60: * <P>This method may be used whenever an array range check is needed.
61: *
62: * @param arrayLength an array length.
63: * @param offset a start index for the fragment
64: * @param length a length (the number of elements in the fragment).
65: * @throws IllegalArgumentException if <code>length</code> is negative.
66: * @throws ArrayIndexOutOfBoundsException if <code>offset</code> is negative or <code>offset</code>+<code>length</code> is greater than <code>arrayLength</code>.
67: */
68: public static void ensureOffsetLength(final int arrayLength,
69: final int offset, final int length) {
70: if (offset < 0)
71: throw new ArrayIndexOutOfBoundsException("Offset ("
72: + offset + ") is negative");
73: if (length < 0)
74: throw new IllegalArgumentException("Length (" + length
75: + ") is negative");
76: if (offset + length > arrayLength)
77: throw new ArrayIndexOutOfBoundsException("Last index ("
78: + (offset + length)
79: + ") is greater than array length (" + arrayLength
80: + ")");
81: }
82:
83: }
|