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

com.jnape.palatable.shoki.api.Sizable Maven / Gradle / Ivy

The newest version!
package com.jnape.palatable.shoki.api;

import static com.jnape.palatable.shoki.api.EquivalenceRelation.equivalent;
import static com.jnape.palatable.shoki.api.EquivalenceRelation.objectEquals;

/**
 * A generic interface representing a type that can provide information about its size.
 *
 * @see Collection
 */
public interface Sizable {

    /**
     * Returns a {@link SizeInfo} representing any information this type has about its size.
     *
     * @return the information about this type's size
     */
    SizeInfo sizeInfo();

    /**
     * Common {@link EquivalenceRelation}s between {@link Sizable}s.
     */
    final class EquivalenceRelations {

        private EquivalenceRelations() {
        }

        /**
         * An {@link EquivalenceRelation} between two {@link Sizable}s that holds if, and only if, both {@link Sizable}s
         * have equivalent {@link SizeInfo}s. O(1).
         *
         * @param  the {@link Sizable} subtype of the arguments
         * @return the {@link EquivalenceRelation}
         */
        public static  EquivalenceRelation sizeInfos() {
            return (xs, ys) -> equivalent(objectEquals(), xs.sizeInfo(), ys.sizeInfo());
        }
    }
}