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

commonMain.com.apollographql.apollo.api.Input.kt Maven / Gradle / Ivy

There is a newer version: 4.0.0
Show newest version
package com.apollographql.apollo.api

import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic

/**
 * A wrapper class of provide [value] with a [defined] state.
 *
 * Wrapper can be in one of 2 states:
 * - *defined* (`defined == true`) means reference to [value] is set explicitly
 * - *undefined* (`defined == false`) means reference is not set.
 *
 * It provides a convenience way to distinguish the case when [value] is provided explicitly and should be
 * serialized (even if it's null) and the case when [value] is undefined (means it won't be serialized).
 */
class Input internal constructor(
    @JvmField val value: V?,
    @JvmField val defined: Boolean
) {

  override fun equals(other: Any?): Boolean {
    if (this === other) return true
    if (other !is Input<*>) return false

    if (value != other.value) return false
    if (defined != other.defined) return false

    return true
  }

  override fun hashCode(): Int {
    var result = value?.hashCode() ?: 0
    result = 31 * result + defined.hashCode()
    return result
  }

  companion object {

    /**
     * Creates a new [Input] instance that is defined in case if [value] is not-null
     * and undefined otherwise.
     */
    @JvmStatic
    fun  optional(value: V?): Input {
      return value?.let { fromNullable(it) } ?: absent()
    }

    /**
     * Creates a new [Input] instance that is always defined for any provided [value].
     */
    @JvmStatic
    fun  fromNullable(value: V?): Input {
      return Input(value, true)
    }

    /**
     * Creates a new [Input] instance that is always undefined.
     */
    @JvmStatic
    fun  absent(): Input {
      return Input(null, false)
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy