jlibs.core.util.CollectionUtil Maven / Gradle / Ivy
The newest version!
/**
* Copyright 2015 Santhosh Kumar Tekuri
*
* The JLibs authors license 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 jlibs.core.util;
import jlibs.core.graph.Filter;
import jlibs.core.lang.Util;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
/**
* @author Santhosh Kumar T
*/
public class CollectionUtil{
/**
* Reads Properties from given inputStream and returns it.
* NOTE: the given stream is closed by this method
*/
public static Properties readProperties(InputStream is, Properties props) throws IOException{
if(props==null)
props = new Properties();
try{
props.load(is);
}finally{
is.close();
}
return props;
}
/**
* Adds objects in array to the given collection
*
* @return the same collection which is passed as argument
*/
@SuppressWarnings({"unchecked", "ManualArrayToCollectionCopy"})
public static Collection addAll(Collection c, T... array){
for(T obj: array)
c.add(obj);
return c;
}
/**
* Removes objects in array to the given collection
*
* @return the same collection which is passed as argument
*/
@SuppressWarnings("unchecked")
public static Collection removeAll(Collection c, T... array){
for(T obj: array)
c.remove(obj);
return c;
}
/**
* Adds the given item to the list at specified index
.
* if index
is greater than list size, it simply appends
* to the list.
*/
public static void add(List list, int index, T item){
if(index List filter(Collection c, Filter filter){
if(c.size()==0)
return Collections.emptyList();
List filteredList = new ArrayList(c.size());
for(T element: c){
if(filter.select(element))
filteredList.add(element);
}
return filteredList;
}
/**
* returns key whose value matches with specified value from given map
* if the given map contains multiple keys mapped to specified value, it
* returns first key encountered
*/
public static K getKey(Map map, V value){
for(Map.Entry entry : map.entrySet()){
if(Util.equals(entry.getValue(), value))
return entry.getKey();
}
return null;
}
/*-------------------------------------------------[ To Primitive Array ]---------------------------------------------------*/
public static boolean[] toBooleanArray(Collection c){
boolean arr[] = new boolean[c.size()];
int i=0;
for(Boolean item: c)
arr[i++] = item;
return arr;
}
public static int[] toIntArray(Collection extends Number> c){
int arr[] = new int[c.size()];
int i=0;
for(Number item: c)
arr[i++] = item.intValue();
return arr;
}
public static long[] toLongArray(Collection extends Number> c){
long arr[] = new long[c.size()];
int i=0;
for(Number item: c)
arr[i++] = item.longValue();
return arr;
}
public static float[] toFloatArray(Collection extends Number> c){
float arr[] = new float[c.size()];
int i=0;
for(Number item: c)
arr[i++] = item.floatValue();
return arr;
}
public static double[] toDoubleArray(Collection extends Number> c){
double arr[] = new double[c.size()];
int i=0;
for(Number item: c)
arr[i++] = item.doubleValue();
return arr;
}
public static byte[] toByteArray(Collection extends Number> c){
byte arr[] = new byte[c.size()];
int i=0;
for(Number item: c)
arr[i++] = item.byteValue();
return arr;
}
public static short[] toShortArray(Collection extends Number> c){
short arr[] = new short[c.size()];
int i=0;
for(Number item: c)
arr[i++] = item.shortValue();
return arr;
}
}