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

com.codeborne.selenide.CheckResult Maven / Gradle / Ivy

There is a newer version: 7.6.1
Show newest version
package com.codeborne.selenide;

import javax.annotation.CheckReturnValue;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.function.Supplier;

import static com.codeborne.selenide.CheckResult.Verdict.ACCEPT;
import static com.codeborne.selenide.CheckResult.Verdict.REJECT;
import static java.util.Objects.requireNonNull;

/**
 * @since 6.0.0
 */
@ParametersAreNonnullByDefault
public record CheckResult(
  Verdict verdict,
  @Nullable String message,
  @Nullable Object actualValue,
  LocalDateTime timestamp
) {
  private static final DateTimeFormatter timeFormat = DateTimeFormatter.ofPattern("HH:mm:ss:SSS");

  public static CheckResult rejected(String message, @Nullable Object actualValue) {
    return new CheckResult(REJECT, message, actualValue, LocalDateTime.now());
  }

  public static CheckResult accepted() {
    return new CheckResult(ACCEPT, null);
  }

  public static CheckResult accepted(@Nullable Object actualValue) {
    return new CheckResult(ACCEPT, actualValue);
  }

  public CheckResult(Verdict verdict, @Nullable Object actualValue) {
    this(verdict, null, actualValue, LocalDateTime.now());
  }

  public CheckResult(boolean checkSucceeded, @Nullable Object actualValue) {
    this(checkSucceeded ? ACCEPT : REJECT, actualValue);
  }

  public enum Verdict {
    ACCEPT,
    REJECT
  }

  @Override
  public String toString() {
    return String.format("%s @ %s%n", actualValue, timeFormat.format(timestamp));
  }

  @Nonnull
  @CheckReturnValue
  public String getMessageOrElse(Supplier defaultMessage) {
    return message() == null ? defaultMessage.get() : message();
  }

  @Nullable
  @CheckReturnValue
  @SuppressWarnings("unchecked")
  public  T getActualValue() {
    return (T) actualValue();
  }

  @Nonnull
  @CheckReturnValue
  @SuppressWarnings("unchecked")
  public  T getActualValueOrElse(T defaultValue) {
    return actualValue() == null ? defaultValue : (T) actualValue();
  }

  @Nonnull
  @CheckReturnValue
  @SuppressWarnings("unchecked")
  public  T requireActualValue() {
    return requireNonNull((T) actualValue());
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy