All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jbasics.collection.CollectionUtilities Maven / Gradle / Ivy

Go to download

jBasics is a collection of useful utility classes for Java. This includes helper for XML, mathematic functions, restful web services helper, pattern oriented programming interfaces and more. Currently Java7 and up is supported. Version 1.0 will required at leaset Java8.

There is a newer version: 2.0.0p3
Show newest version
/*
 * Copyright (c) 2009-2015
 * 	IT-Consulting Stephan Schloepke (http://www.schloepke.de/)
 * 	klemm software consulting Mirko Klemm (http://www.klemm-scs.com/)
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jbasics.collection;

import org.jbasics.annotation.ImmutableState;
import org.jbasics.annotation.Nullable;
import org.jbasics.annotation.ThreadSafe;
import org.jbasics.pattern.delegation.Delegate;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Utilities dealing with the Java Collections.
 *
 * @author Stephan Schloepke
 * @since 1.0
 */
@ThreadSafe
@ImmutableState
public final class CollectionUtilities {

	/**
	 * Creates an unmodifiable shallow copy of the given original {@link Map}. 

While the copy returns an immutable * copy of the {@link Map} the content is not cloned in any way. Unless the content is immutable by itself the * result is not fully immutable. In the shallow copy all references are the same as in the original {@link Map}! *

* * @param original The {@link Map} to copy the elements fro. * @param The type of the key * @param The type of the value * * @return Returns an immutable (unmodifiable) copy of the original {@link Map} with all the elements added but not * cloned! */ public static Map createUnmodifiableShallowCopy(final Map original) { if (original == null || original.isEmpty()) { return Collections.emptyMap(); } else { return Collections.unmodifiableMap(new HashMap(original)); } } /** * Creates an unmodifiable shallow copy of the given original {@link Set}.

While the copy returns an immutable * copy of the {@link Set} the content is not cloned in any way. Unless the content is immutable by itself the * result is not fully immutable. In the shallow copy all references are the same as in the original {@link Set}! *

* * @param original The {@link Set} to copy the elements fro. * @param The type of the elements * * @return Returns an immutable (unmodifiable) copy of the original {@link Set} with all the elements added but not * cloned! */ public static Set createUnmodifiableShallowCopy(final Set original) { if (original == null || original.isEmpty()) { return Collections.emptySet(); } else { return Collections.unmodifiableSet(new HashSet(original)); } } /** * Creates an unmodifiable shallow copy of the given original {@link List}.

While the copy returns an immutable * copy of the {@link List} the content is not cloned in any way. Unless the content is immutable by itself the * result is not fully immutable. In the shallow copy all references are the same as in the original {@link List}! *

* * @param original The {@link List} to copy the elements fro. * @param The type of the elements * * @return Returns an immutable (unmodifiable) copy of the original {@link List} with all the elements added but not * cloned! */ public static List createUnmodifiableShallowCopy(final List original) { if (original == null || original.isEmpty()) { return Collections.emptyList(); } else { return Collections.unmodifiableList(new ArrayList(original)); } } /** * Dereferences the {@link Map} in the {@link Delegate} or returns an empty {@link Map} if either the given {@link * Delegate} or delegated {@link Map} is null. The returned {@link Map} is unmodifiable. * * @param The type of the {@link Map} key * @param The type of the {@link Map} value * @param delegate The {@link Delegate} to the {@link Map}. (Can be null) * * @return An unmodifiable {@link Map} either from the {@link Delegate} or an empty one. */ public static Map dereferenceUnmodifiableMapDelegate(Delegate> delegate) { if (delegate != null) { Map temp = delegate.delegate(); if (temp != null) { return Collections.unmodifiableMap(temp); } } return Collections.emptyMap(); } /** * Dereferences the {@link Map} in the {@link Delegate} or returns null if either the given {@link * Delegate} or delegated {@link Map} is null. * * @param The type of the {@link Map} key * @param The type of the {@link Map} value * @param delegate The {@link Delegate} to the {@link Map}. (Can be null) * * @return The {@link Map} of the {@link Delegate} or null if the {@link Delegate} is null * or references a null {@link Map}. */ public static Map dereferenceMapDelegate(Delegate> delegate) { Map temp = null; if (delegate != null) { temp = delegate.delegate(); } return temp; } /** * Dereferences the {@link List} in the {@link Delegate} or returns an empty {@link List} if either the given {@link * Delegate} or delegated {@link List} is null. The returned {@link List} is unmodifiable. * * @param The type of the {@link Delegate} * @param delegate The {@link Delegate} to the {@link List}. (Can be null) * * @return An unmodifiable {@link List} either from the {@link Delegate} or an empty one. */ public static List dereferenceUnmodifiableListDelegate(Delegate> delegate) { if (delegate != null) { List temp = delegate.delegate(); if (temp != null) { return Collections.unmodifiableList(temp); } } return Collections.emptyList(); } /** * Dereferences the {@link List} in the {@link Delegate} or returns null if either the given {@link * Delegate} or delegated {@link List} is null. * * @param The type of the {@link List} elements * @param delegate The {@link Delegate} to the {@link List}. (Can be null) * * @return The {@link List} of the {@link Delegate} or null if the {@link Delegate} is * null or references a null {@link List}. */ public static List dereferenceListDelegate(Delegate> delegate) { List temp = null; if (delegate != null) { temp = delegate.delegate(); } return temp; } /** * Dereferences the {@link Set} in the {@link Delegate} or returns an empty {@link Set} if either the given {@link * Delegate} or delegated {@link Set} is null. The returned {@link Set} is unmodifiable. * * @param The type of the {@link Delegate} * @param delegate The {@link Delegate} to the {@link Set}. (Can be null) * * @return An unmodifiable {@link Set} either from the {@link Delegate} or an empty one. */ public static Set dereferenceUnmodifiableSetDelegate(Delegate> delegate) { if (delegate != null) { Set temp = delegate.delegate(); if (temp != null) { return Collections.unmodifiableSet(temp); } } return Collections.emptySet(); } /** * Dereferences the {@link Set} in the {@link Delegate} or returns null if either the given {@link * Delegate} or delegated {@link Set} is null. * * @param The type of the {@link Set} elements * @param delegate The {@link Delegate} to the {@link Set}. (Can be null) * * @return The {@link Set} of the {@link Delegate} or null if the {@link Delegate} is null * or references a null {@link Set}. */ public static Set dereferenceSetDelegate(Delegate> delegate) { Set temp = null; if (delegate != null) { temp = delegate.delegate(); } return temp; } /** * Dereferences the {@link Collection} in the {@link Delegate} or returns an empty {@link Collection} if either the * given {@link Delegate} or delegated {@link Collection} is null. The returned {@link Collection} is * unmodifiable. * * @param The type of the {@link Delegate} * @param delegate The {@link Delegate} to the {@link Collection}. (Can be null) * * @return An unmodifiable {@link Collection} either from the {@link Delegate} or an empty one. */ public static Collection dereferenceUnmodifiableCollectionDelegate(Delegate> delegate) { if (delegate != null) { Collection temp = delegate.delegate(); if (temp != null) { return Collections.unmodifiableCollection(temp); } } return Collections.emptySet(); } /** * Dereferences the {@link Collection} in the {@link Delegate} or returns null if either the given * {@link Delegate} or delegated {@link Collection} is null. * * @param The type of the {@link Collection} elements * @param delegate The {@link Delegate} to the {@link Collection}. (Can be null) * * @return The {@link Collection} of the {@link Delegate} or null if the {@link Delegate} is * null or references a null {@link Collection}. */ public static Collection dereferenceCollectionDelegate(Delegate> delegate) { Collection temp = null; if (delegate != null) { temp = delegate.delegate(); } return temp; } /** * Join the two given maps to one by checking if one of the two maps already fulfills everything. Be aware that the * newly created collection is unmodifiable but will change if the underlying collections change! * * @param The key type * @param The value type * @param first The first {@link Map} to join * @param second The second {@link Map} to join * * @return A {@link Map} with the joined content (can be one of the given map if the other is empty or null) */ public static Map joinMapUnmodifiable(Map first, Map second) { if (first == null || first.isEmpty()) { if (second == null || second.isEmpty()) { return Collections.emptyMap(); } else { return Collections.unmodifiableMap(second); } } else if (second == null || second.isEmpty()) { return Collections.unmodifiableMap(first); } else { Map temp = new LinkedHashMap(first.size() + second.size()); temp.putAll(first); temp.putAll(second); return Collections.unmodifiableMap(temp); } } /** * Returns an immutable {@link Map} and an empty one if the given input is null.

This method guarantees that the * resulting {@link Map} is never null and cannot be modified. Since the result is not copied the result changes * whenever the input {@link Map} changes. Or when the elements change its state.

* * @param input The original {@link Map} * @param The type of the key * @param The type of the value * * @return An immutable {@link Map} which is guaranteed to be not null. */ @Nullable(false) public static Map emptyIfNullUnmodifiable(@Nullable(true) Map input) { return input == null ? Collections.emptyMap() : Collections.unmodifiableMap(input); } public static Map emptyIfNull(Map input) { return input == null ? new HashMap() : input; } }