co.unruly.control.LinkList.LinkLists Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of org.apache.karaf.jaas.command Show documentation
Show all versions of org.apache.karaf.jaas.command Show documentation
This bundle provides Karaf shell commands to manipulate JAAS security framework.
package co.unruly.control.LinkList;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import static java.util.Arrays.asList;
public class LinkLists {
public static List toList(LinkList list) {
return list.stream().collect(Collectors.toList());
}
@SafeVarargs
public static LinkList of(T... items) {
return of(asList(items));
}
public static LinkList of(List list) {
final ArrayList reversed = new ArrayList<>(list);
Collections.reverse(reversed);
LinkList rest = EmptyList.nil();
for(T t : reversed) {
rest = NonEmptyList.cons(t, rest);
}
return rest;
}
public static Optional> nonEmptyList(List list) {
return nonEmptyList(of(list));
}
public static Optional> nonEmptyList(LinkList list) {
return list.read(
(x, xs) -> Optional.of(NonEmptyList.cons(x, xs)),
Optional::empty
);
}
public static LinkList lazyMap(LinkList list, Function mappingFunction) {
return new LazyMapper<>(list, mappingFunction);
}
public static LinkList lazyConcat(LinkList first, LinkList second) {
return first.read(
(x, xs) -> second.read(
(y, ys) -> new ConcatList<>(NonEmptyList.cons(x, xs), second),
() -> first
),
() -> second
);
}
public static LinkList eagerConcat(LinkList first, LinkList second) {
return first.read(
(x, xs) -> NonEmptyList.cons(x, eagerConcat(xs, second)),
() -> second
);
}
public static R reduce(LinkList list, BiFunction combiner, R accumulator) {
return list.read(
(t, ts) -> combiner.apply(t, reduce(ts, combiner, accumulator)),
() -> accumulator
);
}
public static T reduce(NonEmptyList list, BiFunction combiner) {
return reduce(list.rest, combiner, list.first);
}
public static Collector, LinkList> toLinkList() {
return new LinkListCollector<>();
}
public static boolean listsEqual(LinkList> first, LinkList> second) {
return first.read(
(x, xs) -> second.read(
(y, ys) -> x.equals(y) && listsEqual(xs, ys),
() -> false
),
() -> second.read(
(y, ys) -> false,
() -> true
)
);
}
}
© 2015 - 2026 Weber Informatics LLC | Privacy Policy