KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > bak > pcj > util > Exceptions


1 /*
2  * Primitive Collections for Java.
3  * Copyright (C) 2003 Søren Bak
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package bak.pcj.util;
20
21 import bak.pcj.map.NoSuchMappingException;
22 import java.util.NoSuchElementException JavaDoc;
23
24 /**
25  * This class provides static methods for throwing exceptions.
26  * It is only provided as a utility class for the collection
27  * implementations and is not a part of the API.
28  *
29  * @author Søren Bak
30  * @version 1.0 21-08-2003 18:44
31  */

32 public class Exceptions {
33
34     public static void indexOutOfBounds(int index, int low, int high) throws IndexOutOfBoundsException JavaDoc {
35         throw new IndexOutOfBoundsException JavaDoc("Index out of bounds: " + index + ", valid range is " + low + " to " + high);
36     }
37
38     public static void nullArgument(String JavaDoc name) throws NullPointerException JavaDoc {
39         throw new NullPointerException JavaDoc("The specified " + name + " is null");
40     }
41
42     public static void negativeArgument(String JavaDoc name, Object JavaDoc value) throws IllegalArgumentException JavaDoc {
43         throw new IllegalArgumentException JavaDoc(name + " cannot be negative: " + String.valueOf(value));
44     }
45
46     public static void negativeOrZeroArgument(String JavaDoc name, Object JavaDoc value) throws IllegalArgumentException JavaDoc {
47         throw new IllegalArgumentException JavaDoc(name + " must be a positive value: " + String.valueOf(value));
48     }
49
50     // ---------------------------------------------------------------
51
// Iterator errors
52
// ---------------------------------------------------------------
53

54     public static void endOfIterator() throws NoSuchElementException JavaDoc {
55         throw new NoSuchElementException JavaDoc("Attempt to iterate past iterator's last element.");
56     }
57
58     public static void startOfIterator() throws NoSuchElementException JavaDoc {
59         throw new NoSuchElementException JavaDoc("Attempt to iterate past iterator's first element.");
60     }
61
62     public static void noElementToRemove() throws IllegalStateException JavaDoc {
63         throw new IllegalStateException JavaDoc("Attempt to remove element from iterator that has no current element.");
64     }
65
66     public static void noElementToGet() throws IllegalStateException JavaDoc {
67         throw new IllegalStateException JavaDoc("Attempt to get element from iterator that has no current element. Call next() first.");
68     }
69
70     public static void noElementToSet() throws IllegalStateException JavaDoc {
71         throw new IllegalStateException JavaDoc("Attempt to set element in iterator that has no current element.");
72     }
73
74     // ---------------------------------------------------------------
75
// Map errors
76
// ---------------------------------------------------------------
77

78     public static void noLastElement() throws IllegalStateException JavaDoc {
79         throw new IllegalStateException JavaDoc("No value to return. Call containsKey() first.");
80     }
81
82     public static void noSuchMapping(Object JavaDoc key) throws NoSuchMappingException {
83         throw new NoSuchMappingException("No such key in map: " + String.valueOf(key));
84     }
85
86     // ---------------------------------------------------------------
87
// Deque errors
88
// ---------------------------------------------------------------
89

90     public static void dequeNoFirst() throws IndexOutOfBoundsException JavaDoc {
91             throw new IndexOutOfBoundsException JavaDoc("Attempt to get first element of empty deque");
92     }
93
94     public static void dequeNoLast() throws IndexOutOfBoundsException JavaDoc {
95             throw new IndexOutOfBoundsException JavaDoc("Attempt to get last element of empty deque");
96     }
97
98     public static void dequeNoFirstToRemove() throws IndexOutOfBoundsException JavaDoc {
99             throw new IndexOutOfBoundsException JavaDoc("Attempt to remove last element of empty deque");
100     }
101
102     public static void dequeNoLastToRemove() throws IndexOutOfBoundsException JavaDoc {
103             throw new IndexOutOfBoundsException JavaDoc("Attempt to remove last element of empty deque");
104     }
105
106     // ---------------------------------------------------------------
107
// Adapter value errors
108
// ---------------------------------------------------------------
109

110     public static void nullElementNotAllowed() throws IllegalArgumentException JavaDoc {
111         throw new IllegalArgumentException JavaDoc("Attempt to add a null value to an adapted primitive set.");
112     }
113
114     public static void cannotAdapt(String JavaDoc name) throws IllegalStateException JavaDoc {
115         throw new IllegalStateException JavaDoc("The " + name + " contains values preventing it from being adapted to a primitive " + name);
116     }
117
118     // ---------------------------------------------------------------
119
//
120
// ---------------------------------------------------------------
121

122     public static void unsupported(String JavaDoc name) throws UnsupportedOperationException JavaDoc {
123         throw new UnsupportedOperationException JavaDoc("Attempt to invoke unsupported operation: " + name);
124     }
125
126     public static void unmodifiable(String JavaDoc name) throws UnsupportedOperationException JavaDoc {
127         throw new UnsupportedOperationException JavaDoc("Attempt to modify unmodifiable " + name);
128     }
129
130     public static void cloning() throws RuntimeException JavaDoc {
131         throw new RuntimeException JavaDoc("Clone is not supported");
132     }
133
134     public static void invalidRangeBounds(Object JavaDoc first, Object JavaDoc last) throws IllegalArgumentException JavaDoc {
135         throw new IllegalArgumentException JavaDoc("First ("+first+") cannot be greater than last ("+last+")");
136     }
137
138     public static void cannotMergeRanges(Object JavaDoc r1, Object JavaDoc r2) throws IllegalArgumentException JavaDoc {
139         throw new IllegalArgumentException JavaDoc("Ranges cannot be merged: " + r1.toString() + " and " + r2.toString());
140     }
141
142     // ---------------------------------------------------------------
143
// Sorted set errors
144
// ---------------------------------------------------------------
145

146     public static void setNoFirst() throws NoSuchElementException JavaDoc {
147         throw new NoSuchElementException JavaDoc("Attempt to get first element of empty set");
148     }
149
150     public static void setNoLast() throws NoSuchElementException JavaDoc {
151         throw new NoSuchElementException JavaDoc("Attempt to get last element of empty set");
152     }
153
154     public static void invalidSetBounds(Object JavaDoc low, Object JavaDoc high) throws IllegalArgumentException JavaDoc {
155         throw new IllegalArgumentException JavaDoc("Lower bound ("+low+") cannot be greater than upper bound ("+high+")");
156     }
157
158     public static void valueNotInSubRange(Object JavaDoc value) throws IllegalArgumentException JavaDoc {
159         throw new IllegalArgumentException JavaDoc("Attempt to add a value outside valid range: " + value);
160     }
161
162     public static void invalidUpperBound(Object JavaDoc value) throws IllegalArgumentException JavaDoc {
163         throw new IllegalArgumentException JavaDoc("Upper bound is not in valid sub-range: " + value);
164     }
165
166     public static void invalidLowerBound(Object JavaDoc value) throws IllegalArgumentException JavaDoc {
167         throw new IllegalArgumentException JavaDoc("Lower bound is not in valid sub-range: " + value);
168     }
169
170
171
172 }
Popular Tags