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

ch.openchvote.utilities.tuples.Tuple Maven / Gradle / Ivy

Go to download

This module provides a collection of utility classes, especially for various mathematical concepts.

The newest version!
/*
 * Copyright (C) 2024 Berner Fachhochschule https://e-voting.bfh.ch
 *
 *  - This program is free software: you can redistribute it and/or modify                           -
 *  - it under the terms of the GNU Affero General Public License as published by                    -
 *  - the Free Software Foundation, either version 3 of the License, or                              -
 *  - (at your option) any later version.                                                            -
 *  -                                                                                                -
 *  - This program is distributed in the hope that it will be useful,                                -
 *  - but WITHOUT ANY WARRANTY; without even the implied warranty of                                 -
 *  - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                                   -
 *  - GNU General Public License for more details.                                                   -
 *  -                                                                                                -
 *  - You should have received a copy of the GNU Affero General Public License                       -
 *  - along with this program. If not, see .                           -
 */
package ch.openchvote.utilities.tuples;

import ch.openchvote.utilities.sequence.Vector;
import ch.openchvote.utilities.sequence.internal.MutableVector;
import ch.openchvote.utilities.tools.Hashable;
import ch.openchvote.utilities.tools.Streamable;
import ch.openchvote.utilities.tuples.decuple.*;
import ch.openchvote.utilities.tuples.viguple.*;

import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * This abstract class is the base class for implementing tuples (pairs, triples, etc.) of different lengths. Tuples are
 * immutable objects.
 */
public abstract sealed class Tuple extends Hashable implements Streamable permits NullTuple, Singleton, Pair, Triple, Quadruple, Quintuple,
        Sextuple, Septuple, Octuple, Nonuple, Decuple, UnDecuple, DuoDecuple, TreDecuple, QuattuorDecuple, QuinDecuple,
        SexDecuple, SeptenDecuple, OctoDecuple, NovemDecuple, Viguple, UnViguple, DuoViguple, TreViguple, QuattuorViguple,
        QuinViguple, SexViguple, SeptenViguple, OctoViguple, NovemViguple {

    /**
     * Returns a vector of type {@code Object} containing all elements of the tuple.
     *
     * @return A vector of type {@code Object} containing all elements of the tuple
     */
    public final Vector toVector() {
        var values = this.toStream().toArray(Object[]::new);
        return new MutableVector<>(values);
    }

    @Override
    public abstract Stream toStream();

    @Override
    public String toString() {
        var prefix = "";
        if (!this.getClass().getSuperclass().equals(Tuple.class)) {
            prefix = this.getClass().getSimpleName();
        }
        return prefix + this.toStream().map(Object::toString).collect(Collectors.joining(",", "(", ")"));
    }

    @Override
    public int hashCode() {
        return this.toStream()
                .mapToInt(Object::hashCode)
                .reduce(1, (result, hash) -> 31 * result + hash);
    }

    @Override
    public boolean equals(Object object) {
        if (this == object) return true;
        if (object instanceof Tuple other) {
            return this.toVector().equals(other.toVector());
        }
        return false;
    }

}