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

org.whispersystems.signalservice.api.util.OptionalUtil.kt Maven / Gradle / Ivy

There is a newer version: 2.15.3_unofficial_107
Show newest version
package org.whispersystems.signalservice.api.util

import okio.ByteString
import java.util.Optional

object OptionalUtil {
  @JvmStatic
  @SafeVarargs
  fun  or(vararg optionals: Optional): Optional {
    return optionals.firstOrNull { it.isPresent } ?: Optional.empty()
  }

  @JvmStatic
  fun byteArrayEquals(a: Optional, b: Optional): Boolean {
    return if (a.isPresent != b.isPresent) {
      false
    } else if (a.isPresent) {
      a.get().contentEquals(b.get())
    } else {
      true
    }
  }

  @JvmStatic
  fun byteArrayHashCode(bytes: Optional): Int {
    return if (bytes.isPresent) {
      bytes.get().contentHashCode()
    } else {
      0
    }
  }

  @JvmStatic
  fun absentIfEmpty(value: String?): Optional {
    return if (value.isNullOrEmpty()) {
      Optional.empty()
    } else {
      Optional.of(value)
    }
  }

  @JvmStatic
  fun absentIfEmpty(value: ByteString?): Optional {
    return if (value == null || value.size == 0) {
      Optional.empty()
    } else {
      Optional.of(value.toByteArray())
    }
  }

  @JvmStatic
  fun  emptyIfListEmpty(list: List?): Optional> {
    return list.asOptional()
  }

  fun  E?.asOptional(): Optional {
    return Optional.ofNullable(this)
  }

  fun  List?.asOptional(): Optional> {
    return Optional.ofNullable(this?.takeIf { it.isNotEmpty() })
  }

  fun String?.emptyIfStringEmpty(): Optional {
    return absentIfEmpty(this)
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy