Skip to contentMethod: mustNotBeArrayOrCollection(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.CheckForNull;
30: import javax.annotation.Nonnull;
31: import java.util.ArrayList;
32: import java.util.Collection;
33: import java.util.List;
34: import lombok.AccessLevel;
35: import lombok.RequiredArgsConstructor;
36:
37: /***********************************************************************************************************************
38: *
39: * This class provides a few static utility methods to manipulate arguments to methods.
40: *
41: * @author Fabrizio Giudici
42: * @it.tidalwave.javadoc.stable
43: *
44: **********************************************************************************************************************/
45: @RequiredArgsConstructor(access = AccessLevel.PRIVATE)
46: public final class Parameters
47: {
48: /*******************************************************************************************************************
49: *
50: * A convenience method for transforming a varargs of objects to a {@link Collection}. It supports concatenating
51: * collections: that is, each varargs item that is a {@link Collection} is flattened.
52: *
53: * @param objects the objects as varargs
54: * @return the objects as collection
55: * @since 3.2-ALPHA-3
56: * @it.tidalwave.javadoc.experimental
57: *
58: ******************************************************************************************************************/
59: @Nonnull
60: public static Collection<Object> r (@Nonnull final Object... objects)
61: {
62: // Don't use streams() for performance reasons.
63: final List<Object> result = new ArrayList<>();
64:
65: for (final Object object : objects)
66: {
67: if (!(object instanceof Collection))
68: {
69: result.add(object);
70: }
71: else
72: {
73: result.addAll((Collection<?>)object);
74: }
75: }
76:
77: return result;
78: }
79:
80: /*******************************************************************************************************************
81: *
82: * Extracts a singled-value parameter of the given type from an array. If the parameter is not found, the default
83: * value is returned. If more than a single parameter is found, an {@link IllegalArgumentException} is thrown.
84: *
85: * @param <T> the static type of the parameter
86: * @param parameterClass the dynamic type of the parameter
87: * @param defaultOption the default value of the parameter
88: * @param parameters the array of parameters
89: * @return the value of the parameter
90: * @throws IllegalArgumentException if more than a single value is found
91: *
92: ******************************************************************************************************************/
93: @CheckForNull
94: public static <T> T find (@Nonnull final Class<T> parameterClass,
95: @CheckForNull final T defaultOption,
96: @Nonnull final Object... parameters)
97: throws IllegalArgumentException
98: {
99: // Don't use streams() for performance reasons.
100: final Collection<T> c = find(parameterClass, parameters);
101:
102: if (c.size() > 1)
103: {
104: throw new IllegalArgumentException(String.format("Multiple values for %s have been specified",
105: parameterClass.getSimpleName()));
106: }
107:
108: return c.isEmpty() ? defaultOption : c.iterator().next();
109: }
110:
111: /*******************************************************************************************************************
112: *
113: * Extracts multiple-value parameters of the given type from an array. If the parameter is not found, an empty
114: * collection is returned.
115: *
116: * @param <T> the static type of the parameter
117: * @param parameterClass the class of the parameter to retrieve
118: * @param parameters the array of parameters
119: * @return the value of the parameter
120: *
121: ******************************************************************************************************************/
122: @Nonnull
123: public static <T> Collection<T> find (@Nonnull final Class<T> parameterClass, @Nonnull final Object... parameters)
124: {
125: // Don't use streams() for performance reasons.
126: final Collection<T> result = new ArrayList<>();
127:
128: for (final Object parameter : parameters)
129: {
130: if (parameterClass.isAssignableFrom(parameter.getClass()))
131: {
132: result.add(parameterClass.cast(parameter));
133: }
134: }
135:
136: return result;
137: }
138:
139: /*******************************************************************************************************************
140: *
141: * <b>This method is for internal implementation only.</b> Ensures that a given object is neither an array nor a
142: * collection.
143: *
144: * @param object the object to check
145: * @param message the message to put in the exception
146: * @return the object itself for calling this method as a function
147: *
148: ******************************************************************************************************************/
149: @Nonnull
150: public static Object mustNotBeArrayOrCollection (@Nonnull final Object object, @Nonnull final String message)
151: {
152:• if (object instanceof Collection)
153: {
154: throw new IllegalArgumentException(message + " can't be a Collection");
155: }
156:
157:• if (object.getClass().isArray())
158: {
159: throw new IllegalArgumentException(message + " can't be an array");
160: }
161:
162: return object;
163: }
164: }