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

com.iodesystems.fn.Fn Maven / Gradle / Ivy

Go to download

Fn is a lazy Java Library that helps utilize some rudimentary functional concepts with more nounular objects

There is a newer version: 3.0.4
Show newest version
package com.iodesystems.fn;

import com.iodesystems.fn.aspects.Exceptions;
import com.iodesystems.fn.aspects.Generators;
import com.iodesystems.fn.aspects.Groups;
import com.iodesystems.fn.aspects.Indexes;
import com.iodesystems.fn.aspects.Iterables;
import com.iodesystems.fn.aspects.Joins;
import com.iodesystems.fn.aspects.Maps;
import com.iodesystems.fn.aspects.Pairs;
import com.iodesystems.fn.aspects.Ranges;
import com.iodesystems.fn.aspects.Sets;
import com.iodesystems.fn.aspects.SizedIterables;
import com.iodesystems.fn.aspects.Strings;
import com.iodesystems.fn.aspects.Trees;
import com.iodesystems.fn.aspects.Values;
import com.iodesystems.fn.aspects.Wheres;
import com.iodesystems.fn.data.Combine;
import com.iodesystems.fn.data.From;
import com.iodesystems.fn.data.From2;
import com.iodesystems.fn.data.Generator;
import com.iodesystems.fn.data.Option;
import com.iodesystems.fn.data.Pair;
import com.iodesystems.fn.logic.Condition;
import com.iodesystems.fn.logic.Handler;
import com.iodesystems.fn.logic.Where;
import com.iodesystems.fn.thread.Async;
import com.iodesystems.fn.thread.Deferred;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Executor;

public class Fn extends Option {

  public static final Fn EMPTY = of(Iterables.empty());
  private final Iterable contents;

  public Fn(Iterable contents) {
    this.contents = contents;
  }

  public static  Pair pair(A a, B b) {
    return Pair.of(a, b);
  }

  public static  Fn of(A source, From getSize, From2 getItem) {
    return of(SizedIterables.of(source, getSize, getItem));
  }

  public static  Fn of(Iterable contents) {
    if (contents instanceof Fn) {
      return (Fn) contents;
    }
    return new Fn<>(contents);
  }

  public static  Fn of(B contents) {
    return of(Iterables.of(contents));
  }

  @SafeVarargs
  public static  Fn of(B... contents) {
    return of(Iterables.of(contents));
  }

  public static  Fn of(Enumeration contents) {
    return of(Iterables.of(contents));
  }

  public static  Fn of(B initial, From next) {
    return of(Generators.of(initial, next));
  }

  public static  Fn of(Generator contents) {
    return of(Iterables.of(contents));
  }

  public static  Condition condition(Where where) {
    return Condition.of(where);
  }

  public static  Condition isNot(A value) {
    return Condition.isNotValue(value);
  }

  public static  Condition is(A value) {
    return Condition.isValue(value);
  }

  public static  Fn ofPresent(Iterable> options) {
    return of(Option.unwrap(options));
  }

  public static  Map mapOf(K key, V value, Object... rest) {
    return Maps.mapOf(key, value, rest);
  }

  public static  Map mapOf(Iterator keys, Iterator values) {
    return Maps.mapOf(keys, values);
  }

  public static  Map mapOf(Iterable keys) {
    Iterator iterator = keys.iterator();
    return Maps.mapOf(iterator, iterator);
  }

  public static  Map put(Map map, K key, V value) {
    return Maps.put(map, key, value);
  }

  public static  Map convertValues(Map map, From valueConverter) {
    return Maps.convertValues(map, valueConverter);
  }

  public static  Map convertKeys(Map map, From keyConverter) {
    return Maps.convertKeys(map, keyConverter);
  }

  public static  Map putAll(
      Map map, Iterable items, From keyFromItem, From valueFromItem) {
    return Maps.putAll(map, items, keyFromItem, valueFromItem);
  }

  public static  Map putAll(Map map, Iterable items, From keyFromItem) {
    return Maps.putAll(map, items, keyFromItem);
  }

  public static  Option get(Map from, A key) {
    return Maps.get(from, key);
  }

  public static  B getOrAdd(Map from, A key, Generator orAdd) {
    return Maps.getOrAdd(from, key, orAdd);
  }

  public static  A ifNull(A value, A ifNull) {
    return Values.ifNull(value, ifNull);
  }

  public static  Async async(A value) {
    return Async.async(value);
  }

  public static  Async async(Callable initial) {
    return Async.async(initial);
  }

  public static  Async async(Executor executor, Callable initial) {
    return Async.async(executor, initial);
  }

  @SafeVarargs
  public static  Async> when(Executor executor, Async... asyncs) {
    return Async.when(executor, asyncs);
  }

  public static  Deferred defer() {
    return Async.defer();
  }

  public static  Deferred defer(Executor executor) {
    return Async.defer(executor);
  }

  @SafeVarargs
  public static  Async> when(Async... asyncs) {
    return Async.when(asyncs);
  }

  public static String stackString(Throwable e) {
    return Exceptions.stackString(e);
  }

  public static  Fn repeat(A a, int times) {
    return of(Iterables.repeat(a, times));
  }

  public static  Map index(Iterable contents, From keyExtractor) {
    return Indexes.index(contents, keyExtractor);
  }

  public static  Map index(
      Iterable contents, From keyExtractor, From valueExtractor) {
    return Indexes.index(contents, keyExtractor, valueExtractor);
  }

  public static  Map> group(Iterable contents, From keyExtractor) {
    return Groups.group(contents, keyExtractor);
  }

  public static > Fn flatten(Iterable contents) {
    return of(Iterables.flatten(contents));
  }

  public static String ifBlank(String thing, String ifBlank) {
    return Strings.ifBlank(thing, ifBlank);
  }

  public static boolean isBlank(String str) {
    return Strings.isBlank(str);
  }

  public static Fn lines(String input) {
    return of(Strings.lines(input));
  }

  public static Fn lines(InputStream input) throws IOException {
    return of(Strings.lines(input));
  }

  public static String readFully(InputStream input) throws IOException {
    return Strings.readFully(input);
  }

  public static Fn split(String input, String on, int limit) {
    return of(Strings.split(input, on, limit));
  }

  public static Fn split(String input, String on) {
    return of(Strings.split(input, on));
  }

  public static Map splitMap(
      String input, String groupSeparator, String valueSeparator) {
    return Fn.flatten(Fn.split(input, groupSeparator).convert(s -> Fn.split(s, valueSeparator, 2)))
        .toMap();
  }

  public static  Fn none() {
    //noinspection unchecked
    return (Fn) EMPTY;
  }

  public static Fn range(int min, int max) {
    return of(Ranges.of(min, max));
  }

  public static Fn range(int min, int max, int by) {
    return of(Ranges.of(min, max, by));
  }

  @SafeVarargs
  public static  List list(T... ts) {
    if (ts == null) {
      ArrayList objects = new ArrayList<>();
      objects.add(null);
      return objects;
    } else {
      return Arrays.asList(ts);
    }
  }

  public static  boolean isEqual(A isThis, A theSameAs) {
    return Values.isEqual(isThis, theSameAs);
  }

  public  Map putAll(Map map, From keyFromItem, From valueFromItem) {
    return Maps.putAll(map, this, keyFromItem, valueFromItem);
  }

  public  Map putAll(Map map, From keyFromItem) {
    return Maps.putAll(map, this, keyFromItem);
  }

  public Fn> withIndex() {
    return of(Pairs.index(contents));
  }

  public Fn> optionally(Where condition) {
    return convert(
        new From>() {
          @Override
          public Option from(A a) {
            return condition.is(a) ? Option.of(a) : Option.empty();
          }
        });
  }

  public A[] toArray(A[] preallocated) {
    return Iterables.toArray(contents, preallocated);
  }

  public Fn takeWhile(Where condition) {
    return of(Iterables.takeWhile(contents, condition));
  }

  public Fn dropWhile(Where condition) {
    return of(Iterables.dropWhile(contents, condition));
  }

  public  B combine(B initial, Combine condenser) {
    return Iterables.combine(contents, initial, condenser);
  }

  public  Fn where(Class cls) {
    //noinspection unchecked
    return of(Iterables.where((Iterable) contents, Wheres.is(cls)));
  }

  public Fn where(Where condition) {
    return of(Iterables.where(contents, condition));
  }

  public Fn where(A value) {
    return of(Iterables.where(contents, Wheres.is(value)));
  }

  public Fn notNull() {
    return of(Iterables.where(contents, Wheres.notNull()));
  }

  public Fn not(A value) {
    return of(Iterables.where(contents, Wheres.not(value)));
  }

  public  Fn not(Class cls) {
    //noinspection unchecked
    return of(Iterables.where(contents, (Where) Wheres.not(cls)));
  }

  public Fn not(Where condition) {
    return of(Iterables.where(contents, Wheres.not(condition)));
  }

  public  Fn> parallel(Iterable bs) {
    return of(Iterables.parallel(contents, bs));
  }

  @Override
  public Iterator iterator() {
    return contents.iterator();
  }

  @Override
  public A get() {
    return first().get();
  }

  @Override
  public A orElse(A ifEmpty) {
    return first().orElse(ifEmpty);
  }

  @Override
  public boolean isEmpty() {
    return first().isEmpty();
  }

  @Override
  public boolean isPresent() {
    return first().isPresent();
  }

  @Override
  public A orResolve(Generator with) {
    return first().orResolve(with);
  }

  public Iterable contents() {
    return contents;
  }

  public Fn loop() {
    return of(Iterables.loop(contents));
  }

  public Fn loop(int times) {
    return of(Iterables.loop(contents, times));
  }

  public Fn drop(final int count) {
    return of(Iterables.drop(count, contents));
  }

  public Option last() {
    return Iterables.last(contents);
  }

  public Fn> split(Where splitter) {
    return of(Iterables.split(contents, splitter));
  }

  public Option last(Where condition) {
    return Iterables.last(contents, condition);
  }

  public Option first() {
    return Iterables.first(contents);
  }

  public Option first(Where where) {
    return Iterables.first(contents, where);
  }

  public  Option first(Class cls) {
    return Iterables.first(contents, cls);
  }

  public void consume() {
    Iterables.consume(contents);
  }

  public List toList() {
    return Iterables.toList(contents);
  }

  public Fn reverse() {
    List as = toList();
    Collections.reverse(as);
    return of(as);
  }

  public Enumeration toEnumeration() {
    return Iterables.toEnumeration(contents);
  }

  public Fn unique() {
    return of(Iterables.unique(contents));
  }

  @SafeVarargs
  public final Fn concat(A... items) {
    return of(Iterables.concat(contents, Iterables.of(items)));
  }

  public Fn concat(Iterable next) {
    return of(Iterables.concat(contents, next));
  }

  public Fn each(Handler handler) {
    return of(Iterables.each(contents, handler));
  }

  public Fn withNext(From nextFromCurrent) {
    return of(Iterables.withNext(contents, nextFromCurrent));
  }

  public Fn take(final int count) {
    return of(Iterables.take(count, contents));
  }

  public Set toSet() {
    return Iterables.toSet(contents);
  }

  public  Map index(From keyExtractor) {
    return Indexes.index(contents, keyExtractor);
  }

  public Fn breadth(final From> descend) {
    return of(Trees.breadth(contents, descend));
  }

  public  Fn> multiply(From> multiplier) {
    return of(Iterables.multiply(contents, multiplier));
  }

  public Fn> breadthPaths(final From> multiplier) {
    return of(Trees.breadthPaths(contents, multiplier));
  }

  public Fn depth(final From> descend) {
    return of(Trees.depth(contents, descend));
  }

  public Fn> group(int size) {
    return of(Groups.group(contents, size));
  }

  public  Map> group(From keyExtractor) {
    return Groups.group(contents, keyExtractor);
  }

  public int size() {
    return Iterables.size(contents);
  }

  public List list() {
    return toList();
  }

  @Override
  public String toString() {
    List as = take(5).toList();
    if (as.size() == 5) {
      String s = as.toString();
      return "Fn" + s.substring(0, s.length() - 1) + "...]";
    } else {
      return "Fn" + as.toString();
    }
  }

  public  Fn convert(From convert) {
    return of(Iterables.convert(contents, convert));
  }

  public Fn strings() {
    return convert(Object::toString);
  }

  public String join(String glue) {
    StringBuilder sb = new StringBuilder();
    for (A content : contents) {
      sb.append(content);
      sb.append(glue);
    }
    int length = sb.length();
    if (length > 0) {
      sb.delete(length - glue.length(), length);
    }
    return sb.toString();
  }

  public  Fn> pairs(From aExtractor, From bExtractor) {
    return of(Pairs.pairs(contents, aExtractor, bExtractor));
  }

  public  Fn> pairs(From aExtractor) {
    return of(Pairs.pairs(contents, aExtractor));
  }

  public Fn sort(Comparator comparator) {
    List as = toList();
    //noinspection Java8ListSort
    Collections.sort(as, comparator);
    return of(as);
  }

  public Fn subtract(Iterable these) {
    return of(Sets.subtract(these, contents));
  }

  public Fn intersection(Iterable these) {
    return of(Sets.intersection(these, contents));
  }

  public Fn difference(Iterable these) {
    return of(Sets.difference(contents, these));
  }

  public Fn union(Iterable these) {
    return of(Sets.union(contents, these));
  }

  public  Fn> join(Iterable bs) {
    return of(Joins.join(contents, bs));
  }

  public  Fn> join(From aIndexer, Iterable bs) {
    return of(Joins.join(contents, aIndexer, bs));
  }

  public  Fn> leftJoin(From aIndexer, Iterable bs) {
    return of(Joins.leftJoin(contents, aIndexer, bs));
  }

  public  Fn> join(From aIndexer, Iterable bs, From bIndexer) {
    return of(Joins.join(contents, aIndexer, bs, bIndexer));
  }

  public  Fn> leftJoin(From aIndexer, Iterable bs, From bIndexer) {
    return of(Joins.leftJoin(contents, aIndexer, bs, bIndexer));
  }

  public Map toMap() {
    return mapOf(this);
  }

  public  Map toMap(From valueExtractor) {
    return Maps.mapOf(this, valueExtractor);
  }
}