KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > NullArgumentException


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.util;
23
24 /**
25  * Thrown to indicate that a method argument was <tt>null</tt> and
26  * should <b>not</b> have been.
27  *
28  * @version <tt>$Revision: 1958 $</tt>
29  * @author <a HREF="mailto:jason@planet57.com">Jason Dillon</a>
30  */

31 public class NullArgumentException
32    extends IllegalArgumentException JavaDoc
33 {
34    /** The name of the argument that was <tt>null</tt>. */
35    protected final String JavaDoc name;
36
37    /** The index of the argument or null if no index. */
38    protected final Object JavaDoc index;
39    
40    /**
41     * Construct a <tt>NullArgumentException</tt>.
42     *
43     * @param name Argument name.
44     */

45    public NullArgumentException(final String JavaDoc name) {
46       super(makeMessage(name));
47
48       this.name = name;
49       this.index = null;
50    }
51
52    /**
53     * Construct a <tt>NullArgumentException</tt>.
54     *
55     * @param name Argument name.
56     * @param index Argument index.
57     */

58    public NullArgumentException(final String JavaDoc name, final long index) {
59       super(makeMessage(name, new Long JavaDoc(index)));
60
61       this.name = name;
62       this.index = new Long JavaDoc(index);
63    }
64
65    /**
66     * Construct a <tt>NullArgumentException</tt>.
67     *
68     * @param name Argument name.
69     * @param index Argument index.
70     */

71    public NullArgumentException(final String JavaDoc name, final Object JavaDoc index) {
72       super(makeMessage(name, index));
73
74       this.name = name;
75       this.index = index;
76    }
77    
78    /**
79     * Construct a <tt>NullArgumentException</tt>.
80     */

81    public NullArgumentException() {
82       this.name = null;
83       this.index = null;
84    }
85
86    /**
87     * Get the argument name that was <tt>null</tt>.
88     *
89     * @return The argument name that was <tt>null</tt>.
90     */

91    public final String JavaDoc getArgumentName() {
92       return name;
93    }
94
95    /**
96     * Get the argument index.
97     *
98     * @return The argument index.
99     */

100    public final Object JavaDoc getArgumentIndex() {
101       return index;
102    }
103    
104    /**
105     * Make a execption message for the argument name.
106     */

107    private static String JavaDoc makeMessage(final String JavaDoc name) {
108       return "'" + name + "' is null";
109    }
110
111    /**
112     * Make a execption message for the argument name and index
113     */

114    private static String JavaDoc makeMessage(final String JavaDoc name, final Object JavaDoc index) {
115       return "'" + name + "[" + index + "]' is null";
116    }
117 }
118
Popular Tags