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

com.epam.commons.LinqUtils Maven / Gradle / Ivy

There is a newer version: 1.1.34
Show newest version
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.map.MapArray;
import com.epam.commons.pairs.Pair;

import java.util.*;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

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, Function 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.apply(el));
            return result;
        } catch (Exception ex) {
            throw new RuntimeException("Can't do select. Exception: " + ex.getMessage());
        }
    }

    public static  List select(T[] array, Function func) {
        return select(asList(array), func);
    }

    public static  List selectMap(Map map, Function, R> func) {
        if (map == null)
            throw new RuntimeException("Can't do selectMap. Collection is Null");
        try {
            return map.entrySet().stream().map(func).collect(Collectors.toList());
        } catch (Exception ex) {
            throw new RuntimeException("Can't do select. Exception: " + ex.getMessage());
        }
    }

    public static  Map select(Map map, Function 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.apply(el.getValue()));
            return result;
        } catch (Exception ex) {
            throw new RuntimeException("Can't do select. Exception: " + ex.getMessage());
        }
    }

    public static  List toList(Map map, BiFunction func) {
        if (map == null)
            throw new RuntimeException("Can't do select. Collection is Null");
        try {
            return map.entrySet().stream().map(el -> func.apply(el.getKey(), el.getValue())).collect(Collectors.toList());
        } catch (Exception ex) {
            throw new RuntimeException("Can't do select. Exception: " + ex.getMessage());
        }
    }

    public static  List where(Collection list, Function 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.apply(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, Function func) {
        return where(asList(list), func);
    }

    public static  Map where(Map map, Function, Boolean> func) {
        if (map == null)
            throw new RuntimeException("Can't do where. Collection is Null");
        try {
            Map result = new HashMap<>();
            map.entrySet().stream().filter(func::apply).forEach(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, Consumer action) {
        if (list == null)
            throw new RuntimeException("Can't do foreach. Collection is Null");
        try {
            for (T el : list)
                action.accept(el);
        } catch (Exception ex) {
            throw new RuntimeException("Can't do foreach. Exception: " + ex.getMessage());
        }
    }

    public static  void foreach(T[] list, Consumer action) {
        foreach(asList(list), action);
    }

    public static  void foreach(Map map, Consumer> action) {
        if (map == null)
            throw new RuntimeException("Can't do foreach. Collection is Null");
        try {
            map.entrySet().forEach(action::accept);
        } 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, Function func) {
        if (list == null || list.size() == 0)
            return null;
        try {
            for (T el : list)
                if (func.apply(el))
                    return el;
        } catch (Exception ex) {
            throw new RuntimeException("Can't do first. Exception: " + ex.getMessage());
        }
        return null;
    }

    public static  boolean any(Collection list, Function func) {
        return first(list, func) != null;
    }

    public static  int firstIndex(List list, Function 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.apply(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, Function func) {
        if (array == null)
            return -1;
        for (int i = 0; i < array.length; i++)
            if (func.apply(array[i]))
                return i;
        return -1;
    }

    public static  T first(T[] list, Function func) {
        return first(asList(list), func);
    }

    public static  V first(Map map, Function func) {
        if (map == null)
            throw new RuntimeException("Can't do first. Collection is Null");
        try {
            for (Map.Entry el : map.entrySet())
                if (func.apply(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, Function func) {
        if (map == null)
            throw new RuntimeException("Can't do first. Collection is Null");
        try {
            for (Pair pair : map.pairs)
                if (func.apply(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, Function func) {
        if (list == null)
            throw new RuntimeException("Can't do last. Collection is Null");
        T result = null;
        try {
            for (T el : list)
                if (func.apply(el))
                    result = el;
        } catch (Exception ex) {
            throw new RuntimeException("Can't do last. Exception: " + ex.getMessage());
        }
        return result;
    }

    public static  T last(T[] list, Function 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, Function> func) {
        List result = new ArrayList<>();
        for (T el : list)
            result.addAll(func.apply(el));
        return result;
    }

    public static  List selectManyArray(List list, Function func) {
        List result = new ArrayList<>();
        for (T el : list)
            result.addAll(asList(func.apply(el)));
        return result;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy