Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* (c) 2005 David B. Bracewell
*
* 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 com.davidbracewell.collection;
import com.davidbracewell.conversion.Cast;
import com.davidbracewell.conversion.Convert;
import com.davidbracewell.io.CSV;
import com.davidbracewell.io.structured.csv.CSVReader;
import com.davidbracewell.string.StringUtils;
import com.davidbracewell.tuple.Tuple2;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import lombok.NonNull;
import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Array;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.BinaryOperator;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
/**
* Static methods for working with collections and iterables.
*
* @author David B. Bracewell
*/
public interface Collect {
/**
* From stream.
*
* @param the type parameter
* @param iterator the iterator
* @return the stream
*/
static Stream from(Iterator iterator) {
if (iterator == null) {
return Collections.emptyList().stream();
}
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), false);
}
/**
* From stream.
*
* @param the type parameter
* @param iterable the iterable
* @return the stream
*/
static Stream from(Iterable iterable) {
if (iterable == null) {
return Collections.emptyList().stream();
}
return StreamSupport.stream(iterable.spliterator(), false);
}
/**
* Paralle from.
*
* @param the type parameter
* @param iterator the iterator
* @return the stream
*/
static Stream parallelFrom(Iterator iterator) {
if (iterator == null) {
return Collections.emptyList().stream();
}
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, Spliterator.ORDERED), true);
}
/**
* Paralle from.
*
* @param the type parameter
* @param iterable the iterable
* @return the stream
*/
static Stream parallelFrom(Iterable iterable) {
if (iterable == null) {
return Collections.emptyList().stream();
}
return StreamSupport.stream(iterable.spliterator(), true);
}
static void put(@NonNull Map map, Map.Entry entry) {
if (entry != null) {
map.put(entry.getKey(), entry.getValue());
}
}
/**
* Wraps an array as an Iterable
*
* @param the type parameter
* @param array The array to wrap
* @param itemClass the type of item in the array
* @return An Iterable wrapping the iterator.
*/
static Iterable asIterable(@NonNull final Object array, @NonNull final Class itemClass) {
Preconditions.checkArgument(array.getClass().isArray());
if (array.getClass().getComponentType().isPrimitive()) {
return new PrimitiveArrayList<>(array, itemClass);
}
return () -> new Iterator() {
int pos = 0;
@Override
public boolean hasNext() {
return pos < Array.getLength(array);
}
@Override
public T next() {
return itemClass.cast(Array.get(array, pos++));
}
};
}
/**
* Wraps an Iterator as an Iterable
*
* @param the type parameter
* @param iterator The iterator to wrap
* @return An Iterable wrapping the iterator.
*/
static Iterable asIterable(final Iterator iterator) {
if (iterator == null) {
return () -> Cast.as(Iterators.emptyIterator());
}
return () -> iterator;
}
/**
*
Fills a map with an iterable converting the even elements of the iterable to the keys and the odd elements to
* the values using the given key and value converters. A null or empty iterable results in an empty map.
*
* @param The key type
* @param The value type
* @param map The map to fill
* @param iterable The iterable to convert into a map
* @param keyConverter The converter to use for the keys (even elements)
* @param valueConverter The converter to use for the values (odd elements)
* @return The map.
*/
static Map fillMap(@NonNull Map map, Iterable> iterable, @NonNull Function