// Copyright (C) 2017 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 com.google.gerrit.truth;

import static com.google.common.truth.Fact.fact;
import static com.google.common.truth.Truth.assertAbout;

import com.google.common.truth.CustomSubjectBuilder;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.StandardSubjectBuilder;
import com.google.common.truth.Subject;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;

public class OptionalSubject<S extends Subject, T> extends Subject {

  private final Optional<T> optional;
  private final BiFunction<StandardSubjectBuilder, ? super T, ? extends S> valueSubjectCreator;

  // TODO(aliceks): Remove when all relevant usages are adapted to new check()/factory approach.
  public static <S extends Subject, T> OptionalSubject<S, T> assertThat(
      Optional<T> optional, Function<? super T, ? extends S> elementAssertThatFunction) {
    Subject.Factory<S, T> valueSubjectFactory =
        (metadata, value) -> elementAssertThatFunction.apply(value);
    return assertThat(optional, valueSubjectFactory);
  }

  public static <S extends Subject, T> OptionalSubject<S, T> assertThat(
      Optional<T> optional, Subject.Factory<? extends S, ? super T> valueSubjectFactory) {
    return assertAbout(optionals()).thatCustom(optional, valueSubjectFactory);
  }

  public static OptionalSubject<Subject, ?> assertThat(Optional<?> optional) {
    return assertAbout(optionals()).that(optional);
  }

  public static CustomSubjectBuilder.Factory<OptionalSubjectBuilder> optionals() {
    return OptionalSubjectBuilder::new;
  }

  private OptionalSubject(
      FailureMetadata failureMetadata,
      Optional<T> optional,
      BiFunction<StandardSubjectBuilder, ? super T, ? extends S> valueSubjectCreator) {
    super(failureMetadata, optional);
    this.optional = optional;
    this.valueSubjectCreator = valueSubjectCreator;
  }

  public void isPresent() {
    isNotNull();
    if (!optional.isPresent()) {
      failWithoutActual(fact("expected to have", "value"));
    }
  }

  public void isAbsent() {
    isNotNull();
    if (optional.isPresent()) {
      failWithoutActual(fact("expected not to have", "value"));
    }
  }

  public void isEmpty() {
    isAbsent();
  }

  public S value() {
    isNotNull();
    isPresent();
    return valueSubjectCreator.apply(check("value()"), optional.get());
  }

  public static class OptionalSubjectBuilder extends CustomSubjectBuilder {

    OptionalSubjectBuilder(FailureMetadata failureMetadata) {
      super(failureMetadata);
    }

    public <S extends Subject, T> OptionalSubject<S, T> thatCustom(
        Optional<T> optional, Subject.Factory<? extends S, ? super T> valueSubjectFactory) {
      return that(optional, (builder, value) -> builder.about(valueSubjectFactory).that(value));
    }

    public OptionalSubject<Subject, ?> that(Optional<?> optional) {
      return that(optional, StandardSubjectBuilder::that);
    }

    public <S extends Subject, T> OptionalSubject<S, T> that(
        Optional<T> optional,
        BiFunction<StandardSubjectBuilder, ? super T, ? extends S> valueSubjectCreator) {
      return new OptionalSubject<>(metadata(), optional, valueSubjectCreator);
    }
  }
}

@@ -1007,44 +1007,8 @@
 ion;
-%0Aimport java.util.function.Function;
 %0A%0Apu
@@ -1214,444 +1214,8 @@
 r;%0A%0A
-  // TODO(aliceks): Remove when all relevant usages are adapted to new check()/factory approach.%0A  public static %3CS extends Subject, T%3E OptionalSubject%3CS, T%3E assertThat(%0A      Optional%3CT%3E optional, Function%3C? super T, ? extends S%3E elementAssertThatFunction) %7B%0A    Subject.Factory%3CS, T%3E valueSubjectFactory =%0A        (metadata, value) -%3E elementAssertThatFunction.apply(value);%0A    return assertThat(optional, valueSubjectFactory);%0A  %7D%0A%0A
   pu

Remove now unnecessary method