KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > it > unimi > dsi > fastutil > Arrays


1 package it.unimi.dsi.fastutil;
2
3 /*
4  * fastutil: Fast & compact type-specific collections for Java
5  *
6  * Copyright (C) 2002, 2003, 2004, 2005, 2006 Sebastiano Vigna
7  *
8  * This library is free software; you can redistribute it and/or
9  * 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
25 /** A class providing static methods and objects that do useful things with arrays.
26  *
27  * @see Arrays
28  */

29
30 public class Arrays {
31     
32     private Arrays() {}
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, final int from, final int to ) {
45         if ( from < 0 ) throw new ArrayIndexOutOfBoundsException JavaDoc( "Start index (" + from + ") is negative" );
46         if ( from > to ) throw new IllegalArgumentException JavaDoc( "Start index (" + from + ") is greater than end index (" + to + ")" );
47         if ( to > arrayLength ) throw new ArrayIndexOutOfBoundsException JavaDoc( "End index (" + to + ") is greater than array length (" + arrayLength + ")" );
48     }
49
50     /** Ensures that a range given by an offset and a length fits an array of given length.
51      *
52      * <P>This method may be used whenever an array range check is needed.
53      *
54      * @param arrayLength an array length.
55      * @param offset a start index for the fragment
56      * @param length a length (the number of elements in the fragment).
57      * @throws IllegalArgumentException if <code>length</code> is negative.
58      * @throws ArrayIndexOutOfBoundsException if <code>offset</code> is negative or <code>offset</code>+<code>length</code> is greater than <code>arrayLength</code>.
59      */

60     public static void ensureOffsetLength( final int arrayLength, final int offset, final int length ) {
61         if ( offset < 0 ) throw new ArrayIndexOutOfBoundsException JavaDoc( "Offset (" + offset + ") is negative" );
62         if ( length < 0 ) throw new IllegalArgumentException JavaDoc( "Length (" + length + ") is negative" );
63         if ( offset + length > arrayLength ) throw new ArrayIndexOutOfBoundsException JavaDoc( "Last index (" + ( offset + length ) + ") is greater than array length (" + arrayLength + ")" );
64     }
65
66 }
67
Popular Tags