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

com.cloudinary.transformation.Transformation.kt Maven / Gradle / Ivy

package com.cloudinary.transformation

@TransformationDsl
class Transformation(internal val actions: List = emptyList()) : ITransformable {
    constructor(options: Builder.() -> Unit) : this(fromBuilder(options))

    constructor(action: Action) : this(listOf(action))

    override fun toString() = actions.joinToString(DEFAULT_COMPONENT_SEPARATOR)

    override fun add(action: Action) = Transformation(actions + action)

    override fun addTransformation(transformation: Transformation) = Transformation(actions + transformation.actions)

    class Builder(private val components: MutableList = mutableListOf()) : ITransformable {
        override fun add(action: Action) = apply { components.add(action) }
        override fun addTransformation(transformation: Transformation) = apply {
            components.addAll(transformation.actions)
        }
        fun build() = Transformation(components)
    }

    companion object {
        fun transformation(t: Builder.() -> Unit): Transformation {
            val builder = Builder()
            builder.t()
            return builder.build()
        }
    }
}

@TransformationDsl
interface AnimatedComponentBuilder {
    fun build(): Action
}

@TransformationDsl
interface TransformationComponentBuilder {
    fun build(): Action
}

private fun fromBuilder(options: Transformation.Builder.() -> Unit): List {
    val builder = Transformation.Builder()
    builder.options()
    return builder.build().actions
}

private fun fromImageBuilder(options: ImageTransformation.Builder.() -> Unit): List {
    val builder = ImageTransformation.Builder()
    builder.options()
    return builder.build().actions
}

private fun fromVideoBuilder(options: VideoTransformation.Builder.() -> Unit): List {
    val builder = VideoTransformation.Builder()
    builder.options()
    return builder.build().actions
}

@TransformationDsl
class ImageTransformation(internal val actions: List = emptyList()) : ITransformableImage {

    constructor(action: Action) : this(listOf(action))
    constructor(options: Builder.() -> Unit) : this(fromImageBuilder(options))

    override fun toString() = actions.joinToString(DEFAULT_COMPONENT_SEPARATOR)

    override fun add(action: Action) = ImageTransformation(actions + action)

    override fun addTransformation(transformation: Transformation) = ImageTransformation(actions + transformation.actions)

    class Builder(private val components: MutableList = mutableListOf()) : ITransformableImage {
        override fun add(action: Action) = apply { components.add(action) }
        override fun addTransformation(transformation: Transformation) = apply { components.addAll(transformation.actions) }
        fun build() = ImageTransformation(components)
    }
}

@TransformationDsl
class VideoTransformation(internal val actions: List = emptyList()) : ITransformableVideo {

    constructor(action: Action) : this(listOf(action))
    constructor(options: Builder.() -> Unit) : this(fromVideoBuilder(options))

    override fun toString() = actions.joinToString(DEFAULT_COMPONENT_SEPARATOR)

    override fun add(action: Action) = VideoTransformation(actions + action)

    override fun addTransformation(transformation: Transformation) = VideoTransformation(actions + transformation.actions)

    class Builder(private val components: MutableList = mutableListOf()) : ITransformableVideo {
        override fun add(action: Action) = apply { components.add(action) }
        override fun addTransformation(transformation: Transformation) = apply { components.addAll(transformation.actions) }
        fun build() = VideoTransformation(components)
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy