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

nl.vpro.util.Pair Maven / Gradle / Ivy

There is a newer version: 5.3.1
Show newest version
package nl.vpro.util;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.util.Map;

/**
 * @author Michiel Meeuwissen
 * @since 1.63
 */
@Data
@AllArgsConstructor(staticName = "of")
@lombok.Builder(builderClassName = "Builder")
public class Pair implements Map.Entry {

    private F first;
    @lombok.Builder.Default
    private String firstDescription = "first";

    private S second;
    @lombok.Builder.Default
    private String secondDescription = "second";

    public static  Pair of(F first, S second){
        return Pair.builder().first(first).second(second).build();
    }

    @Override
    public String toString() {
        return "(" + firstDescription + "=" + first + ", " + secondDescription + "=" + second + ")";
    }

    @Override
    public F getKey() {
        return getFirst();
    }

    @Override
    public S getValue() {
        return getSecond();
    }

    @Override
    public S setValue(S value) {
        S prev = second;
        setSecond(value);
        return prev;
    }

    public static class Builder {

        public Builder key(F value) {
            return descriptedFirst("key", value);
        }

        public Builder value(S value) {
            return descriptedSecond("value", value);
        }


        public Builder descriptedFirst(String description, F value) {
            return
                firstDescription(description).first(value);
        }
        public Builder descriptedSecond(String description, S value) {
            return
                secondDescription(description).second(value);
        }
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy