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

org.ggp.base.util.Immutables Maven / Gradle / Ivy

The newest version!
package org.ggp.base.util;

import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.ImmutableSortedSet;

import net.alloyggp.escaperope.rope.ropify.ListWeaver;
import net.alloyggp.escaperope.rope.ropify.RopeBuilder;
import net.alloyggp.escaperope.rope.ropify.RopeList;
import net.alloyggp.escaperope.rope.ropify.Weaver;

public class Immutables {
    private Immutables() {
        // Not instantiable
    }

    public static  Collector> collectList() {
        return new Collector, ImmutableList>() {
            @Override
            public Supplier> supplier() {
                return () -> ImmutableList.builder();
            }

            @Override
            public BiConsumer, T> accumulator() {
                return (builder, elem) -> builder.add(elem);
            }

            @Override
            public BinaryOperator> combiner() {
                return (b1, b2) -> b1.addAll(b2.build());
            }

            @Override
            public Function, ImmutableList> finisher() {
                return builder -> builder.build();
            }

            @Override
            public Set characteristics() {
                return ImmutableSet.of();
            }
        };
    }

    public static > Collector> collectSortedSet() {
        return new Collector, ImmutableSortedSet>() {
            @Override
            public Supplier> supplier() {
                return () -> ImmutableSortedSet.naturalOrder();
            }

            @Override
            public BiConsumer, T> accumulator() {
                return (builder, elem) -> builder.add(elem);
            }

            @Override
            public BinaryOperator> combiner() {
                return (b1, b2) -> b1.addAll(b2.build());
            }

            @Override
            public Function, ImmutableSortedSet> finisher() {
                return builder -> builder.build();
            }

            @Override
            public Set characteristics() {
                return ImmutableSet.of();
            }
        };
    }

    public static  Collector> collectSortedSet(Comparator comparator) {
        return new Collector, ImmutableSortedSet>() {
            @Override
            public Supplier> supplier() {
                return () -> new ImmutableSortedSet.Builder(comparator);
            }

            @Override
            public BiConsumer, T> accumulator() {
                return (builder, elem) -> builder.add(elem);
            }

            @Override
            public BinaryOperator> combiner() {
                return (b1, b2) -> b1.addAll(b2.build());
            }

            @Override
            public Function, ImmutableSortedSet> finisher() {
                return builder -> builder.build();
            }

            @Override
            public Set characteristics() {
                return ImmutableSet.of();
            }
        };
    }

    public static  Collector> collectSet() {
        return new Collector, ImmutableSet>() {
            @Override
            public Supplier> supplier() {
                return () -> ImmutableSet.builder();
            }

            @Override
            public BiConsumer, T> accumulator() {
                return (builder, elem) -> builder.add(elem);
            }

            @Override
            public BinaryOperator> combiner() {
                return (b1, b2) -> b1.addAll(b2.build());
            }

            @Override
            public Function, ImmutableSet> finisher() {
                return builder -> builder.build();
            }

            @Override
            public Set characteristics() {
                return ImmutableSet.of();
            }
        };
    }

    public static  Weaver> listWeaver(Weaver innerWeaver) {
        return new ListWeaver>() {
            @Override
            protected void addToList(ImmutableList objects, RopeBuilder list) {
                for (T object : objects) {
                    list.add(object, innerWeaver);
                }
            }

            @Override
            protected ImmutableList fromRope(RopeList list) {
                ImmutableList.Builder builder = ImmutableList.builder();
                for (int i = 0; i < list.size(); i++) {
                    builder.add(list.get(i, innerWeaver));
                }
                return builder.build();
            }
        };
    }

    public static  Weaver> setMultimapWeaver(Weaver keyWeaver, Weaver valueWeaver) {
        return new ListWeaver>() {
            @Override
            protected void addToList(ImmutableSetMultimap map, RopeBuilder list) {
                for (K key : map.keySet()) {
                    list.add(key, keyWeaver);
                    list.add(map.get(key), setWeaver(valueWeaver));
                }
            }

            @Override
            protected ImmutableSetMultimap fromRope(RopeList list) {
                Preconditions.checkArgument(list.size() % 2 == 0);
                ImmutableSetMultimap.Builder builder = ImmutableSetMultimap.builder();
                for (int i = 0; i < list.size(); i += 2) {
                    K key = list.get(i, keyWeaver);
                    ImmutableSet values = list.get(i + 1, setWeaver(valueWeaver));
                    builder.putAll(key, values);
                }
                return builder.build();
            }
        };
    }

    public static  Weaver> setWeaver(Weaver innerWeaver) {
        return new ListWeaver>() {
            @Override
            protected void addToList(ImmutableSet set, RopeBuilder list) {
                for (T element : set) {
                    list.add(element, innerWeaver);
                }
            }

            @Override
            protected ImmutableSet fromRope(RopeList list) {
                ImmutableSet.Builder builder = ImmutableSet.builder();
                for (int i = 0; i < list.size(); i++) {
                    builder.add(list.get(i, innerWeaver));
                }
                return builder.build();
            }
        };
    }

    public static  Weaver> mapWeaver(Weaver keyWeaver, Weaver valueWeaver) {
        return new ListWeaver>() {
            @Override
            protected void addToList(ImmutableMap map, RopeBuilder list) {
                for (Map.Entry entry : map.entrySet()) {
                    list.add(entry.getKey(), keyWeaver);
                    list.add(entry.getValue(), valueWeaver);
                }
            }

            @Override
            protected ImmutableMap fromRope(RopeList list) {
                Preconditions.checkArgument(list.size() % 2 == 0);
                ImmutableMap.Builder builder = ImmutableMap.builder();
                for (int i = 0; i < list.size(); i += 2) {
                    K key = list.get(i, keyWeaver);
                    V value = list.get(i + 1, valueWeaver);
                    builder.put(key, value);
                }
                return builder.build();
            }
        };
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy