Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6dbf3197da6231dd7bc61b39b0e5664813945b2f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*******************************************************************************
 * Copyright (c) 2016, 2017 Obeo.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Obeo - initial API and implementation
 *******************************************************************************/
package org.eclipse.eef.common.api.utils;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.stream.Collectors;

/**
 * Shared utility methods.
 *
 * @author Pierre-Charles David <pierre-charles.david@obeo.fr>
 */
public final class Util {
	/**
	 * Prevent instantiation.
	 */
	private Util() {
		// Prevent instantiation.
	}

	/**
	 * Returns the first of a series of alternative string values which is not null.
	 *
	 * @param alternatives
	 *            the alernative strings, in order of preference.
	 * @return the first non-null alternative, or an empty string if they are all null.
	 */
	public static String firstNonNull(String... alternatives) {
		for (String s : alternatives) {
			if (s != null) {
				return s;
			}
		}
		return null;
	}

	/**
	 * Tests if a string is blank (i.e. null, empty, or containing only whitespace).
	 *
	 * @param s
	 *            the string to test.
	 * @return <code>true</code> if and only if the string is blank.
	 */
	public static boolean isBlank(String s) {
		return s == null || s.trim().length() == 0;
	}

	/**
	 * Returns the given object as a collection and filter it with the given type. If the object is a single object, the
	 * we will return a collection containing said object, it the object is already a collection, we will return a new
	 * collection with all its elements.
	 *
	 * @param rawValue
	 *            The raw value
	 * @param clazz
	 *            The class of the result wanted
	 * @param <T>
	 *            The type of the result wanted
	 * @return A collection
	 */
	public static <T> Collection<T> asCollection(Object rawValue, Class<T> clazz) {
		final Collection<T> result;
		if (rawValue instanceof Collection<?>) {
			// @formatter:off
			result = ((Collection<?>) rawValue).stream()
						.filter(clazz::isInstance)
						.map(clazz::cast)
						.collect(Collectors.toList());
			// @formatter:on
		} else if (clazz.isInstance(rawValue)) {
			result = Collections.singleton(clazz.cast(rawValue));
		} else if (rawValue != null && rawValue.getClass().isArray()) {
			// @formatter:off
			result = Arrays.stream((Object[]) rawValue)
						.filter(clazz::isInstance)
						.map(clazz::cast)
						.collect(Collectors.toList());
			// @formatter:on
		} else {
			result = Collections.emptySet();
		}
		return result;
	}
}

Back to the top