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

it.unibo.alchemist.model.geometry.GeometricShapeFactory.kt Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2010-2023, Danilo Pianini and contributors
 * listed, for each module, in the respective subproject's build.gradle.kts file.
 *
 * This file is part of Alchemist, and is distributed under the terms of the
 * GNU General Public License, with a linking exception,
 * as described in the file LICENSE in the Alchemist distribution's top directory.
 */

package it.unibo.alchemist.model.geometry

import it.unibo.alchemist.model.geometry.shapes.AdimensionalShape
import java.io.Serializable

/**
 * Generic factory for [Shape].
 */
interface GeometricShapeFactory, A : Transformation> : Serializable {
    /**
     * A special shape which does not occupy space and does not intersect with any other, not even with itself.
     */
    fun adimensional(): AdimensionalShape

    /**
     * Requires that the given shape is compatible with the ones provided by this factory,
     * otherwise throws an exception.
     *
     * @param shape the shape to check
     * @return the same shape
     */
    fun requireCompatible(shape: Shape<*, *>): Shape

    /**
     * Factory methods for [Shape].
     */
    companion object {
        /**
         * Retrieves a factory of [Shape] compatible with the given vector type.
         *
         * @param  The type of vector used
         * @param  The supported geometric transformations
         * @param  The interface of the factory requested
         * @return the factory
         */
        inline fun  getInstance(): F
            where S : Vector,
                  A : Transformation,
                  F : GeometricShapeFactory =
            getInstance(F::class.java)

        /**
         * Retrieves a factory of [Shape] compatible with the given space.
         * (This method is meant for compatibility with java).
         *
         * @param  The type of vector used
         * @param  The supported geometric transformations
         * @param  The interface of the factory requested
         * @param type The interface of the factory requested
         * @return the factory
         */
        @JvmStatic
        @Suppress("UNCHECKED_CAST")
        fun  getInstance(
            type: Class,
        ): F
            where S : Vector,
                  A : Transformation,
                  F : GeometricShapeFactory =
            when (type) {
                Euclidean2DShapeFactory::class.java -> AwtEuclidean2DShapeFactory()
                else -> TODO("No implementation found for GeometricShapeFactory<" + type.simpleName + ">")
            } as F
    }
}