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.
package com.epam.commons;
/*
* Copyright 2004-2016 EPAM Systems
*
* This file is part of JDI project.
*
* JDI is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* JDI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with JDI. If not, see .
*/
import com.epam.commons.linqinterfaces.JActionTEx;
import com.epam.commons.linqinterfaces.JFuncTR;
import com.epam.commons.linqinterfaces.JFuncTREx;
import com.epam.commons.linqinterfaces.JFuncTTREx;
import com.epam.commons.map.MapArray;
import com.epam.commons.pairs.Pair;
import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import static java.lang.String.format;
import static java.util.Arrays.asList;
/**
* Created by roman.i on 30.09.2014.
*/
public final class LinqUtils {
private LinqUtils() {
}
public static List copyList(Collection list) {
List result = new ArrayList<>();
for (T el : list)
result.add(el);
return result;
}
public static List
select(Collection list, JFuncTREx func) {
if (list == null)
throw new RuntimeException("Can't do select. Collection is Null");
try {
List
result = new CopyOnWriteArrayList<>();
for (T el : list)
result.add(func.invoke(el));
return result;
} catch (Exception ex) {
throw new RuntimeException("Can't do select. Exception: " + ex.getMessage());
}
}
public static List
select(T[] array, JFuncTREx func) {
return select(asList(array), func);
}
public static List selectMap(Map map, JFuncTREx, R> func) {
if (map == null)
throw new RuntimeException("Can't do selectMap. Collection is Null");
try {
List result = new CopyOnWriteArrayList<>();
for (Map.Entry el : map.entrySet())
result.add(func.invoke(el));
return result;
} catch (Exception ex) {
throw new RuntimeException("Can't do select. Exception: " + ex.getMessage());
}
}
public static Map select(Map map, JFuncTREx func) {
if (map == null)
throw new RuntimeException("Can't do select. Collection is Null");
try {
Map result = new HashMap<>();
for (Map.Entry el : map.entrySet())
result.put(el.getKey(), func.invoke(el.getValue()));
return result;
} catch (Exception ex) {
throw new RuntimeException("Can't do select. Exception: " + ex.getMessage());
}
}
public static List
toList(Map map, JFuncTTREx func) {
if (map == null)
throw new RuntimeException("Can't do select. Collection is Null");
try {
List
result = new CopyOnWriteArrayList<>();
for (Map.Entry el : map.entrySet())
result.add(func.invoke(el.getKey(), el.getValue()));
return result;
} catch (Exception ex) {
throw new RuntimeException("Can't do select. Exception: " + ex.getMessage());
}
}
public static Map toMap(List list, JFuncTR nameFunc) {
Map map = new HashMap<>();
for(T el : list)
map.put(nameFunc.invoke(el), el);
return map;
}
public static Map toMap(List list, JFuncTR key, JFuncTR value) {
Map map = new HashMap<>();
for(T el : list)
map.put(key.invoke(el), value.invoke(el));
return map;
}
public static Map toMap(T[] list, JFuncTR nameFunc) {
return toMap(asList(list), nameFunc);
}
public static Map toMap(T[] list, JFuncTR key, JFuncTR value) {
return toMap(asList(list), key, value);
}
public static List where(Collection list, JFuncTREx func) {
if (list == null)
throw new RuntimeException("Can't do where. Collection is Null");
try {
List result = new ArrayList<>();
for (T el : list)
if (func.invoke(el))
result.add(el);
return result;
} catch (Exception ex) {
throw new RuntimeException("Can't do where. Exception: " + ex.getMessage());
}
}
public static List where(T[] list, JFuncTREx func) {
return where(asList(list), func);
}
public static Map where(Map map, JFuncTREx, Boolean> func) {
if (map == null)
throw new RuntimeException("Can't do where. Collection is Null");
try {
Map result = new HashMap<>();
for (Map.Entry el : map.entrySet())
if (func.invoke(el))
result.put(el.getKey(), el.getValue());
return result;
} catch (Exception ex) {
throw new RuntimeException("Can't do where. Exception: " + ex.getMessage());
}
}
public static void foreach(Collection list, JActionTEx action) {
if (list == null)
throw new RuntimeException("Can't do foreach. Collection is Null");
try {
for (T el : list)
action.invoke(el);
} catch (Exception ex) {
throw new RuntimeException("Can't do foreach. Exception: " + ex.getMessage());
}
}
public static void foreach(T[] list, JActionTEx action) {
foreach(asList(list), action);
}
public static void foreach(Map map, JActionTEx> action) {
if (map == null)
throw new RuntimeException("Can't do foreach. Collection is Null");
try {
for (Map.Entry e : map.entrySet())
action.invoke(e);
} catch (Exception ex) {
throw new RuntimeException("Can't do foreach. Exception: " + ex.getMessage());
}
}
public static T first(Collection list) {
if (list == null || list.size() == 0)
throw new RuntimeException("Can't do first. Collection is Null");
return list.iterator().next();
}
public static T first(T[] list) {
return first(asList(list));
}
public static V first(Map map) {
if (map == null || map.size() == 0)
throw new RuntimeException("Can't do first. Collection is Null");
return map.entrySet().iterator().next().getValue();
}
public static T first(Collection list, JFuncTREx func) {
if (list == null || list.size() == 0)
return null;
try {
for (T el : list)
if (func.invoke(el))
return el;
} catch (Exception ex) {
throw new RuntimeException("Can't do first. Exception: " + ex.getMessage());
}
return null;
}
public static boolean any(Collection list, JFuncTREx func) {
return first(list, func) != null;
}
public static boolean any(T[] array, JFuncTREx func) {
return any(asList(array), func);
}
public static T single(Collection list, JFuncTREx func) {
if (list == null || list.size() == 0)
return null;
T found = null;
try {
for (T el : list)
if (func.invoke(el)) {
if (found != null) return null;
else found = el;
}
} catch (Exception ex) {
throw new RuntimeException("Can't do first. Exception: " + ex.getMessage());
}
return found;
}
public static T single(T[] array, JFuncTREx func) {
return single(asList(array), func);
}
public static int firstIndex(List list, JFuncTREx func) {
if (list == null)
throw new RuntimeException("Can't get firstIndex. Collection is Null");
try {
for (int i = 0; i < list.size(); i++)
if (func.invoke(list.get(i)))
return i;
} catch (Exception ex) {
throw new RuntimeException("Can't get firstIndex. Exception: " + ex.getMessage());
}
return -1;
}
public static int firstIndex(T[] array, JFuncTREx func) {
try {
if (array == null)
return -1;
for (int i = 0; i < array.length; i++)
if (func.invoke(array[i]))
return i;
return -1;
} catch (Exception ex) {
throw new RuntimeException("Can't get firstIndex. Exception: " + ex.getMessage());
}
}
public static T first(T[] list, JFuncTREx func) {
return first(asList(list), func);
}
public static V first(Map map, JFuncTREx func) {
if (map == null)
throw new RuntimeException("Can't do first. Collection is Null");
try {
for (Map.Entry el : map.entrySet())
if (func.invoke(el.getKey()))
return el.getValue();
} catch (Exception ex) {
throw new RuntimeException("Can't do first. Exception: " + ex.getMessage());
}
return null;
}
public static V first(MapArray map, JFuncTREx func) {
if (map == null)
throw new RuntimeException("Can't do first. Collection is Null");
try {
for (Pair pair : map.pairs)
if (func.invoke(pair.key))
return pair.value;
} catch (Exception ex) {
throw new RuntimeException("Can't do first. Exception: " + ex.getMessage());
}
return null;
}
public static T last(Collection list) {
if (list == null)
throw new RuntimeException("Can't do last. Collection is Null");
T result = null;
for (T el : list)
result = el;
return result;
}
public static T last(T[] list) {
return last(asList(list));
}
public static T last(Collection list, JFuncTREx func) {
if (list == null)
throw new RuntimeException("Can't do last. Collection is Null");
T result = null;
try {
for (T el : list)
if (func.invoke(el))
result = el;
} catch (Exception ex) {
throw new RuntimeException("Can't do last. Exception: " + ex.getMessage());
}
return result;
}
public static T last(T[] list, JFuncTREx func) {
return last(asList(list), func);
}
public static String[] toStringArray(Collection collection) {
if (collection == null)
throw new RuntimeException("Can't do toStringArray. Collection is Null");
return collection.toArray(new String[collection.size()]);
}
public static int[] toIntArray(Collection collection) {
if (collection == null)
throw new RuntimeException("Can't do toIntArray. Collection is Null");
int[] result = new int[collection.size()];
int i = 0;
for (Integer el : collection)
result[i++] = el;
return result;
}
public static int getIndex(String[] array, String value) {
if (array == null)
throw new RuntimeException("Can't do index. Collection is Null");
for (int i = 0; i < array.length; i++)
if (array[i].equals(value))
return i;
return -1;
}
public static int getIndex(List list, String value) {
if (list == null)
throw new RuntimeException("Can't do index. Collection is Null");
for (int i = 0; i < list.size(); i++)
if (list.get(i).equals(value))
return i;
return -1;
}
public static List listCopy(List list, int from) {
return listCopy(list, from, list.size() - 1);
}
public static List listCopy(List list, int from, int to) {
if (from*to < 0)
throw new RuntimeException(format("from and to should have same sign (%s, %s)", from, to));
if (from < 0)
from = list.size() + from - 1;
if (to < 0)
to = list.size() + to - 1;
List result = new ArrayList<>();
for (int i = from; i <= to; i++)
result.add(list.get(i));
return result;
}
public static List listCopyUntil(List list, int to) {
return listCopy(list, 0, to);
}
public static List selectMany(List list, JFuncTREx> func) {
try {
List result = new ArrayList<>();
for (T el : list)
for (T element : func.invoke(el))
result.add(element);
return result;
} catch (Exception ex) {
throw new RuntimeException("Can't do selectMany. Exception: " + ex.getMessage());
}
}
public static List selectManyArray(List list, JFuncTREx func) {
try {
List result = new ArrayList<>();
for (T el : list)
for (T element : func.invoke(el))
result.add(element);
return result;
} catch (Exception ex) {
throw new RuntimeException("Can't do selectMany. Exception: " + ex.getMessage());
}
}
}