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

org.immutables.check.OptionalChecker Maven / Gradle / Ivy

Go to download

Testing dependencies and utilities. Expected to be used only in test scope

The newest version!
/*
   Copyright 2013-2018 Immutables Authors and Contributors

   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 org.immutables.check;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;

/**
 * The optional match wrapper.
 * @param  the generic type
 */
public class OptionalChecker {

  private final Optional optionalValue;

  OptionalChecker(Optional optionalValue) {
    this.optionalValue = Preconditions.checkNotNull(optionalValue);
  }

  public StringChecker asString() {
    return new StringChecker(
        optionalValue.isPresent()
            ? optionalValue.get().toString()
            : null, false);
  }

  public void isAbsent() {
    ObjectChecker.verifyCheck("\nExpected: Optional.absent()" +
        "\n     but: was " + optionalValue, !optionalValue.isPresent());
  }

  public void isOf(Object object) {
    of(Matchers.equalTo(Preconditions.checkNotNull(object)));
  }

  public void isPresent() {
    ObjectChecker.verifyCheck("\nExpected: Optional.of(*)" +
        "\n     but: was Optional.absent()",
        optionalValue.isPresent());
  }

  public void of(Matcher matcher) {
    if (optionalValue.isPresent()) {
      Checkers.check(optionalValue.get()).is(matcher);
    } else {
      // We fail any matcher on absent value in this check
      ObjectChecker.fail(optionalValue, matcher);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy