Skip to contentMethod: throwWhenNull(Object, String)
1: /*
2: * *********************************************************************************************************************
3: *
4: * TheseFoolishThings: Miscellaneous utilities
5: * http://tidalwave.it/projects/thesefoolishthings
6: *
7: * Copyright (C) 2009 - 2023 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/thesefoolishthings-src
23: * git clone https://github.com/tidalwave-it/thesefoolishthings-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.util;
28:
29: import javax.annotation.Nonnull;
30: import javax.annotation.Nullable;
31: import java.util.Collection;
32:
33: /***********************************************************************************************************************
34: *
35: * Notifies that a searched object couldn't be found.
36: *
37: * @author Fabrizio Giudici
38: * @it.tidalwave.javadoc.stable
39: *
40: **********************************************************************************************************************/
41: public class NotFoundException extends Exception
42: {
43: private static final long serialVersionUID = 3453465498093L;
44:
45: /*******************************************************************************************************************
46: *
47: * Creates an empty exception.
48: *
49: ******************************************************************************************************************/
50: public NotFoundException()
51: {
52: }
53:
54: /*******************************************************************************************************************
55: *
56: * Creates an exception with a message.
57: *
58: * @param message the message
59: *
60: ******************************************************************************************************************/
61: public NotFoundException (@Nonnull final String message)
62: {
63: super(message);
64: }
65:
66: /*******************************************************************************************************************
67: *
68: * Creates an exception with a cause.
69: *
70: * @param cause the cause
71: *
72: ******************************************************************************************************************/
73: public NotFoundException (@Nonnull final Throwable cause)
74: {
75: super(cause);
76: }
77:
78: /*******************************************************************************************************************
79: *
80: * Creates an exception with a message and a cause.
81: *
82: * @param message the message
83: * @param cause the cause
84: *
85: ******************************************************************************************************************/
86: public NotFoundException (@Nonnull final String message, @Nonnull final Throwable cause)
87: {
88: super(message, cause);
89: }
90:
91: /*******************************************************************************************************************
92: *
93: * Throws the {@code NotFoundException} when the passed object is {@code null}. The method returns the object
94: * itself, so it can be used with fluent interfaces.
95: *
96: * @param <T> the type of the object
97: * @param object the object to be tested
98: * @param message the error message to be thrown
99: * @return the object
100: * @throws NotFoundException if the object is null
101: *
102: ******************************************************************************************************************/
103: @Nonnull // needed for binary backward compatibility (this method interprets % in message in verbatim mode)
104: public static <T> T throwWhenNull (@Nullable final T object, @Nonnull final String message)
105: throws NotFoundException
106: {
107:• if (object == null)
108: {
109: throw new NotFoundException(message);
110: }
111:
112: return object;
113: }
114:
115: /*******************************************************************************************************************
116: *
117: * Throws the {@code NotFoundException} when the passed object is {@code null}. The method returns the object
118: * itself, so it can be used with fluent interfaces.
119: *
120: * @param <T> the type of the object
121: * @param object the object to be tested
122: * @param message the error message to be thrown (formatted as in {@link String#format}
123: * @param args the arguments to format the error message
124: * @return the object
125: * @throws NotFoundException if the object is null
126: *
127: ******************************************************************************************************************/
128: @Nonnull
129: public static <T> T throwWhenNull (@Nullable final T object,
130: @Nonnull final String message,
131: @Nonnull final Object... args)
132: throws NotFoundException
133: {
134: if (object == null)
135: {
136: throw new NotFoundException(String.format(message, args));
137: }
138:
139: return object;
140: }
141:
142: /*******************************************************************************************************************
143: *
144: * Throws the {@code NotFoundException} when the passed collection is {@code null} or empty. The method returns the
145: * collection itself, so it can be used with fluent interfaces.
146: *
147: * @param <T> the type of collection items
148: * @param collection the collection to be tested
149: * @param message the error message to be thrown
150: * @return the collection
151: * @throws NotFoundException if the collection is null or empty
152: *
153: ******************************************************************************************************************/
154: @Nonnull // needed for binary backward compatibility (this method interprets % in message in verbatim mode)
155: public static <T extends Collection<?>> T throwWhenEmpty (@Nullable final T collection,
156: @Nonnull final String message)
157: throws NotFoundException
158: {
159: if ((collection == null) || collection.isEmpty())
160: {
161: throw new NotFoundException(message);
162: }
163:
164: return collection;
165: }
166:
167: /*******************************************************************************************************************
168: *
169: * Throws the {@code NotFoundException} when the passed collection is {@code null} or empty. The method returns the
170: * collection itself, so it can be used with fluent interfaces.
171: *
172: * @param <T> the type of collection items
173: * @param collection the collection to be tested
174: * @param message the error message to be thrown (formatted as in {@link String#format}
175: * @param args the arguments to format the error message
176: * @return the collection
177: * @throws NotFoundException if the collection is null or empty
178: *
179: ******************************************************************************************************************/
180: @Nonnull
181: public static <T extends Collection<?>> T throwWhenEmpty (@Nullable final T collection,
182: @Nonnull final String message,
183: @Nonnull final Object... args)
184: throws NotFoundException
185: {
186: if ((collection == null) || collection.isEmpty())
187: {
188: throw new NotFoundException(String.format(message, args));
189: }
190:
191: return collection;
192: }
193: }