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

org.apache.geode.internal.util.CollectionUtils Maven / Gradle / Ivy

Go to download

Apache Geode provides a database-like consistency model, reliable transaction processing and a shared-nothing architecture to maintain very low latency performance with high concurrency processing

There is a newer version: 1.15.1
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package org.apache.geode.internal.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;

import org.apache.geode.internal.lang.Filter;

/**
 * The CollectionUtils class is a utility class for working with the Java Collections framework of
 * classes, data structures and algorithms.
 * 

* * @see org.apache.geode.internal.lang.Filter * @see java.util.Arrays * @see java.util.Collection * @see java.util.Collections * @see java.util.Iterator * @see java.util.List * @see java.util.Map * @see java.util.Set * @since GemFire 7.0 */ public abstract class CollectionUtils { /** * Returns the specified array as a List of elements. *

* * @param the class type of the elements in the array. * @param array the object array of elements to convert to a List. * @return a List of elements contained in the specified array. * @see java.util.Arrays#asList(Object[]) */ @SafeVarargs public static List asList(final T... array) { List arrayList = new ArrayList(array.length); Collections.addAll(arrayList, array); return arrayList; } /** * Returns the specified array as a Set of elements. *

* * @param the class type of the elements in the array. * @param array the object array of elements to convert to a Set. * @return a Set of elements contained in the specified array. * @see java.util.Arrays#asList(Object[]) */ @SafeVarargs public static Set asSet(final T... array) { Set arraySet = new HashSet(array.length); Collections.addAll(arraySet, array); return arraySet; } /** * Creates an Properties object initialized with the value from the given Map. *

* * @param map the Map supply the keys and value for the Properties object. * @return a Properties object initialized with the key and value from the Map. * @see java.util.Map * @see java.util.Properties */ public static Properties createProperties(final Map map) { Properties properties = new Properties(); if (!(map == null || map.isEmpty())) { for (Entry entry : map.entrySet()) { properties.setProperty(entry.getKey(), entry.getValue()); } } return properties; } /** * Null-safe implementation for method invocations that return a List Collection. If the returned * List is null, then this method will return an empty List in it's place. *

* * @param the class type of the List's elements. * @param list the target List to verify as not null. * @return the specified List if not null otherwise return an empty List. */ public static List emptyList(final List list) { return (list != null ? list : Collections.emptyList()); } /** * Null-safe implementation for method invocations that return a Set Collection. If the returned * Set is null, then this method will return an empty Set in it's place. *

* * @param the class type of the Set's elements. * @param set the target Set to verify as not null. * @return the specified Set if not null otherwise return an empty Set. */ public static Set emptySet(final Set set) { return (set != null ? set : Collections.emptySet()); } /** * Iterates the Collection and finds all object elements that match the Filter criteria. *

* * @param the class type of the Collection elements. * @param collection the Collection of elements to iterate and filter. * @param filter the Filter applied to the Collection of elements in search of all matching * elements. * @return a List of elements from the Collection matching the criteria of the Filter in the order * in which they were found. If no elements match the Filter criteria, then an empty List * is returned. */ public static List findAll(final Collection collection, final Filter filter) { final List matches = new ArrayList(collection.size()); for (final T element : collection) { if (filter.accept(element)) { matches.add(element); } } return matches; } /** * Iterates the Collection and finds all object elements that match the Filter criteria. *

* * @param the class type of the Collection elements. * @param collection the Collection of elements to iterate and filter. * @param filter the Filter applied to the Collection of elements in search of the matching * element. * @return a single element from the Collection that match the criteria of the Filter. If multiple * elements match the Filter criteria, then this method will return the first one. If no * element of the Collection matches the criteria of the Filter, then this method returns * null. */ public static T findBy(final Collection collection, final Filter filter) { for (T element : collection) { if (filter.accept(element)) { return element; } } return null; } /** * Removes keys from the Map based on a Filter. *

* * @param the Class type of the key. * @param the Class type of the value. * @param map the Map from which to remove key-value pairs based on a Filter. * @param filter the Filter to apply to the Map entries to ascertain their "value". * @return the Map with entries filtered by the specified Filter. * @see java.util.Map * @see java.util.Map.Entry * @see org.apache.geode.internal.lang.Filter */ public static Map removeKeys(final Map map, final Filter> filter) { for (final Iterator> mapEntries = map.entrySet().iterator(); mapEntries .hasNext();) { if (!filter.accept(mapEntries.next())) { mapEntries.remove(); } } return map; } /** * Removes keys with null values in the Map. *

* * @param map the Map from which to remove null key-value pairs. * @return the Map without any null keys or values. * @see #removeKeys * @see java.util.Map */ public static Map removeKeysWithNullValues(final Map map) { return removeKeys(map, new Filter>() { @Override public boolean accept(final Map.Entry entry) { return (entry.getValue() != null); } }); } /** * Add all elements of an {@link Enumeration} to a {@link Collection}. * * @param collection to add from enumeration. * @param enumeration to add to collection. * @return true if collection is modified, otherwise false. * @since GemFire 8.1 * @see Collection#addAll(Collection) */ public static final boolean addAll(final Collection collection, final Enumeration enumeration) { if (null == enumeration) { return false; } boolean modified = false; while (enumeration.hasMoreElements()) { modified |= collection.add(enumeration.nextElement()); } return modified; } /** * Construct a new unmodifiable {@link Iterable} backed by the supplied iterable. * * {@link Iterable#iterator()} will return an umodifiable {@link Iterator} on which calling * {@link Iterator#remove()} will throw {@link UnsupportedOperationException}. * * @param iterable to wrap as unmodifiable * @return unmodifiable {@link Iterable} * @since GemFire 8.1 */ public static final Iterable unmodifiableIterable(final Iterable iterable) { return new UnmodifiableIterable(iterable); } /** * Unmodifiable {@link Iterable} in the style of * {@link Collections#unmodifiableCollection(Collection)}. * * * @since GemFire 8.1 */ static private class UnmodifiableIterable implements Iterable { final private Iterable iterable; private UnmodifiableIterable(final Iterable iterable) { this.iterable = iterable; } @Override public Iterator iterator() { return new Iterator() { final private Iterator iterator = iterable.iterator(); @Override public boolean hasNext() { return iterator.hasNext(); } @Override public T next() { return iterator.next(); } @Override public void remove() { throw new UnsupportedOperationException(); } }; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy