public final class

EspressoOptional<T>

extends java.lang.Object

 java.lang.Object

↳androidx.test.espresso.util.EspressoOptional<T>

Gradle dependencies

compile group: 'androidx.test.espresso', name: 'espresso-core', version: '3.6.1'

  • groupId: androidx.test.espresso
  • artifactId: espresso-core
  • version: 3.6.1

Artifact androidx.test.espresso:espresso-core:3.6.1 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.test.espresso:espresso-core com.android.support.test.espresso:espresso-core

Androidx class mapping:

androidx.test.espresso.util.EspressoOptional android.support.test.espresso.util.EspressoOptional

Overview

This class is a reimplementation of to maintain API compatibility with older versions of espresso.

Summary

Methods
public static EspressoOptional<java.lang.Object>absent()

public java.util.Set<java.lang.Object>asSet()

public booleanequals(java.lang.Object object)

public static EspressoOptional<java.lang.Object>fromNullable(java.lang.Object nullableReference)

public java.lang.Objectget()

public inthashCode()

public booleanisPresent()

public static EspressoOptional<java.lang.Object>of(java.lang.Object reference)

public Optional<java.lang.Object>or(Optional<java.lang.Object> secondChoice)

public java.lang.Objector(Supplier<java.lang.Object> supplier)

public java.lang.Objector(java.lang.Object defaultValue)

public java.lang.ObjectorNull()

public static java.lang.Iterable<java.lang.Object>presentInstances(java.lang.Iterable<Optional> optionals)

public java.lang.StringtoString()

public Optional<java.lang.Object>transform(Function<java.lang.Object, java.lang.Object> function)

from java.lang.Objectclone, finalize, getClass, notify, notifyAll, wait, wait, wait

Methods

public static EspressoOptional<java.lang.Object> of(java.lang.Object reference)

public static EspressoOptional<java.lang.Object> absent()

public static EspressoOptional<java.lang.Object> fromNullable(java.lang.Object nullableReference)

public boolean isPresent()

public java.lang.Object get()

public Optional<java.lang.Object> or(Optional<java.lang.Object> secondChoice)

public java.lang.Object or(Supplier<java.lang.Object> supplier)

public java.lang.Object or(java.lang.Object defaultValue)

public java.lang.Object orNull()

public java.util.Set<java.lang.Object> asSet()

public boolean equals(java.lang.Object object)

public int hashCode()

public java.lang.String toString()

public Optional<java.lang.Object> transform(Function<java.lang.Object, java.lang.Object> function)

public static java.lang.Iterable<java.lang.Object> presentInstances(java.lang.Iterable<Optional> optionals)

Source

/*
 * Copyright (C) 2018 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.test.espresso.util;

import static androidx.test.internal.util.Checks.checkNotNull;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;

import androidx.annotation.Nullable;
import androidx.test.espresso.core.internal.deps.guava.base.Function;
import androidx.test.espresso.core.internal.deps.guava.base.Optional;
import androidx.test.espresso.core.internal.deps.guava.base.Supplier;
import java.util.Set;

/**
 * This class is a reimplementation of {@link com.google.common.base.Optional} to maintain API
 * compatibility with older versions of espresso.
 *
 * @param <T> the type of instance that can be contained. {@code Optional} is naturally covariant on
 *     this type, so it is safe to cast an {@code Optional<T>} to {@code Optional<S>} for any
 *     supertype {@code S} of {@code T}.
 * @see com.google.common.base.Optional
 * @deprecated use androidx.annotation.Nullable/NonNull instead
 */
@Deprecated
public final class EspressoOptional<T> {

  private static final EspressoOptional ABSENT = new EspressoOptional<>(null);
  @Nullable private final T value;

  public static <T> EspressoOptional<T> of(T reference) {
    return new EspressoOptional<>(checkNotNull(reference));
  }

  public static <T> EspressoOptional<T> absent() {
    return ABSENT;
  }

  public static <T> EspressoOptional<T> fromNullable(T nullableReference) {
    return new EspressoOptional<>(nullableReference);
  }

  private EspressoOptional(@Nullable T value) {
    this.value = value;
  }

  public boolean isPresent() {
    return value != null;
  }

  public T get() {
    return checkNotNull(value);
  }

  public Optional<T> or(Optional<? extends T> secondChoice) {
    return isPresent() ? Optional.of(value) : (Optional<T>) secondChoice;
  }

  public T or(Supplier<? extends T> supplier) {
    return isPresent() ? value : supplier.get();
  }

  public T or(T defaultValue) {
    return isPresent() ? value : defaultValue;
  }

  public T orNull() {
    return isPresent() ? value : null;
  }

  public Set<T> asSet() {
    return isPresent() ? singleton(value) : emptySet();
  }

  @Override
  public boolean equals(Object object) {
    if (object instanceof EspressoOptional) {
      EspressoOptional<?> other = (EspressoOptional<?>) object;
      if (isPresent()) {
        return value.equals(other.get());
      } else if (!other.isPresent()) {
        return true;
      }
    }
    return false;
  }

  @Override
  public int hashCode() {
    // copy the guava implementation
    return isPresent() ? 0x598df91c + value.hashCode() : 0x79a31aac;
  }

  @Override
  public String toString() {
    return isPresent() ? value.toString() : "null";
  }

  public <V> Optional<V> transform(Function<? super T, V> function) {
    return Optional.fromNullable(value).transform(function);
  }

  public static <T> Iterable<T> presentInstances(
      final Iterable<? extends Optional<? extends T>> optionals) {
    return Optional.presentInstances(optionals);
  }


  private static final long serialVersionUID = 0;
}