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

io.virtdata.libbasics.shared.from_long.to_collection.StringSet Maven / Gradle / Ivy

There is a newer version: 2.12.15
Show newest version
package io.virtdata.libbasics.shared.from_long.to_collection;

import io.virtdata.annotations.Categories;
import io.virtdata.annotations.Category;
import io.virtdata.annotations.Example;
import io.virtdata.annotations.ThreadSafeMapper;

import java.util.HashSet;
import java.util.function.LongFunction;
import java.util.function.LongToIntFunction;

/**
 * Create a {@code Set} from a long
 * based on two functions, the first to
 * determine the set size, and the second to populate the set with
 * String values. The input fed to the second function is incremented
 * between elements. Regardless of the object type provided by the
 * second function, {@link java.lang.Object#toString()} is used to get
 * the value to add to the list.
 *
 * To create Sets of any type of object simply use {@link Set} with
 * a specific value mapping function.
 */
@Categories({Category.collections})
@ThreadSafeMapper
public class StringSet implements LongFunction> {

    private final LongToIntFunction sizeFunc;
    private final LongFunction valueFunc;

    @Example({"StringSet(HashRange(3,7),Add(15L))", "create a set between 3 and 7 elements of String representations of Long values"})
    public StringSet(LongToIntFunction sizeFunc,
                     LongFunction valueFunc) {
        this.sizeFunc = sizeFunc;
        this.valueFunc = valueFunc;
    }

    @Override
    public java.util.Set apply(long value) {
        int size = sizeFunc.applyAsInt(value);
        java.util.Set set = new HashSet<>(size);
        for (int i = 0; i < size; i++) {
            set.add(valueFunc.apply(value + i).toString());
        }
        return set;
    }
}