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

io.avaje.validation.adapter.OptionalValidationAdapter Maven / Gradle / Ivy

Go to download

validator for annotated pojos using constraint annotations and source code generation

There is a newer version: 2.4
Show newest version
package io.avaje.validation.adapter;

import java.util.Optional;
import java.util.OptionalDouble;
import java.util.OptionalInt;
import java.util.OptionalLong;

final class OptionalValidationAdapter extends ContainerAdapter {

  OptionalValidationAdapter(ValidationAdapter adapters) {
    super(adapters);
  }

  @Override
  @SuppressWarnings("unchecked")
  public boolean validate(T value, ValidationRequest req, String propertyName) {
    if (value == null) {
      return true;
    }
    if (value instanceof final Optional o) {
      o.ifPresent(v -> initalAdapter.validate((T) v, req, propertyName));
    } else if (value instanceof final OptionalInt i) {
      i.ifPresent(v -> initalAdapter.validate((T) (Integer) v, req, propertyName));
    } else if (value instanceof final OptionalLong l) {
      l.ifPresent(v -> initalAdapter.validate((T) (Long) v, req, propertyName));
    } else if (value instanceof final OptionalDouble d) {
      d.ifPresent(v -> initalAdapter.validate((T) (Double) v, req, propertyName));
    }
    return true;
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy