001: /*
002: * Primitive Collections for Java.
003: * Copyright (C) 2003 S�ren Bak
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package com.uwyn.rife.pcj.util;
020:
021: import com.uwyn.rife.pcj.map.NoSuchMappingException;
022: import java.util.NoSuchElementException;
023:
024: /**
025: * This class provides static methods for throwing exceptions.
026: * It is only provided as a utility class for the collection
027: * implementations and is not a part of the API.
028: *
029: * @author Søren Bak
030: * @version 1.0 21-08-2003 18:44
031: */
032: public class Exceptions {
033:
034: public static void indexOutOfBounds(int index, int low, int high)
035: throws IndexOutOfBoundsException {
036: throw new IndexOutOfBoundsException("Index out of bounds: "
037: + index + ", valid range is " + low + " to " + high);
038: }
039:
040: public static void nullArgument(String name)
041: throws NullPointerException {
042: throw new NullPointerException("The specified " + name
043: + " is null");
044: }
045:
046: public static void negativeArgument(String name, Object value)
047: throws IllegalArgumentException {
048: throw new IllegalArgumentException(name
049: + " cannot be negative: " + String.valueOf(value));
050: }
051:
052: public static void negativeOrZeroArgument(String name, Object value)
053: throws IllegalArgumentException {
054: throw new IllegalArgumentException(name
055: + " must be a positive value: " + String.valueOf(value));
056: }
057:
058: // ---------------------------------------------------------------
059: // Iterator errors
060: // ---------------------------------------------------------------
061:
062: public static void endOfIterator() throws NoSuchElementException {
063: throw new NoSuchElementException(
064: "Attempt to iterate past iterator's last element.");
065: }
066:
067: public static void startOfIterator() throws NoSuchElementException {
068: throw new NoSuchElementException(
069: "Attempt to iterate past iterator's first element.");
070: }
071:
072: public static void noElementToRemove() throws IllegalStateException {
073: throw new IllegalStateException(
074: "Attempt to remove element from iterator that has no current element.");
075: }
076:
077: public static void noElementToGet() throws IllegalStateException {
078: throw new IllegalStateException(
079: "Attempt to get element from iterator that has no current element. Call next() first.");
080: }
081:
082: public static void noElementToSet() throws IllegalStateException {
083: throw new IllegalStateException(
084: "Attempt to set element in iterator that has no current element.");
085: }
086:
087: // ---------------------------------------------------------------
088: // Map errors
089: // ---------------------------------------------------------------
090:
091: public static void noLastElement() throws IllegalStateException {
092: throw new IllegalStateException(
093: "No value to return. Call containsKey() first.");
094: }
095:
096: public static void noSuchMapping(Object key)
097: throws NoSuchMappingException {
098: throw new NoSuchMappingException("No such key in map: "
099: + String.valueOf(key));
100: }
101:
102: // ---------------------------------------------------------------
103: // Deque errors
104: // ---------------------------------------------------------------
105:
106: public static void dequeNoFirst() throws IndexOutOfBoundsException {
107: throw new IndexOutOfBoundsException(
108: "Attempt to get first element of empty deque");
109: }
110:
111: public static void dequeNoLast() throws IndexOutOfBoundsException {
112: throw new IndexOutOfBoundsException(
113: "Attempt to get last element of empty deque");
114: }
115:
116: public static void dequeNoFirstToRemove()
117: throws IndexOutOfBoundsException {
118: throw new IndexOutOfBoundsException(
119: "Attempt to remove last element of empty deque");
120: }
121:
122: public static void dequeNoLastToRemove()
123: throws IndexOutOfBoundsException {
124: throw new IndexOutOfBoundsException(
125: "Attempt to remove last element of empty deque");
126: }
127:
128: // ---------------------------------------------------------------
129: // Adapter value errors
130: // ---------------------------------------------------------------
131:
132: public static void nullElementNotAllowed()
133: throws IllegalArgumentException {
134: throw new IllegalArgumentException(
135: "Attempt to add a null value to an adapted primitive set.");
136: }
137:
138: public static void cannotAdapt(String name)
139: throws IllegalStateException {
140: throw new IllegalStateException(
141: "The "
142: + name
143: + " contains values preventing it from being adapted to a primitive "
144: + name);
145: }
146:
147: // ---------------------------------------------------------------
148: //
149: // ---------------------------------------------------------------
150:
151: public static void unsupported(String name)
152: throws UnsupportedOperationException {
153: throw new UnsupportedOperationException(
154: "Attempt to invoke unsupported operation: " + name);
155: }
156:
157: public static void unmodifiable(String name)
158: throws UnsupportedOperationException {
159: throw new UnsupportedOperationException(
160: "Attempt to modify unmodifiable " + name);
161: }
162:
163: public static void cloning() throws RuntimeException {
164: throw new RuntimeException("Clone is not supported");
165: }
166:
167: public static void invalidRangeBounds(Object first, Object last)
168: throws IllegalArgumentException {
169: throw new IllegalArgumentException("First (" + first
170: + ") cannot be greater than last (" + last + ")");
171: }
172:
173: public static void cannotMergeRanges(Object r1, Object r2)
174: throws IllegalArgumentException {
175: throw new IllegalArgumentException("Ranges cannot be merged: "
176: + r1.toString() + " and " + r2.toString());
177: }
178:
179: // ---------------------------------------------------------------
180: // Sorted set errors
181: // ---------------------------------------------------------------
182:
183: public static void setNoFirst() throws NoSuchElementException {
184: throw new NoSuchElementException(
185: "Attempt to get first element of empty set");
186: }
187:
188: public static void setNoLast() throws NoSuchElementException {
189: throw new NoSuchElementException(
190: "Attempt to get last element of empty set");
191: }
192:
193: public static void invalidSetBounds(Object low, Object high)
194: throws IllegalArgumentException {
195: throw new IllegalArgumentException("Lower bound (" + low
196: + ") cannot be greater than upper bound (" + high + ")");
197: }
198:
199: public static void valueNotInSubRange(Object value)
200: throws IllegalArgumentException {
201: throw new IllegalArgumentException(
202: "Attempt to add a value outside valid range: " + value);
203: }
204:
205: public static void invalidUpperBound(Object value)
206: throws IllegalArgumentException {
207: throw new IllegalArgumentException(
208: "Upper bound is not in valid sub-range: " + value);
209: }
210:
211: public static void invalidLowerBound(Object value)
212: throws IllegalArgumentException {
213: throw new IllegalArgumentException(
214: "Lower bound is not in valid sub-range: " + value);
215: }
216:
217: }
|