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

tech.jhipster.lite.module.domain.properties.JHipsterModuleParameters Maven / Gradle / Ivy

There is a newer version: 1.22.0
Show newest version
package tech.jhipster.lite.module.domain.properties;

import java.util.Map;
import java.util.function.Predicate;
import tech.jhipster.lite.shared.collection.domain.JHipsterCollections;
import tech.jhipster.lite.shared.error.domain.Assert;

record JHipsterModuleParameters(Map parameters) {
  public JHipsterModuleParameters(Map parameters) {
    this.parameters = JHipsterCollections.immutable(parameters);
  }

   T getOrDefault(String key, T defaultValue, Class clazz) {
    return getOrDefault(key, defaultValue, clazz, t -> false);
  }

   T getOrDefault(String key, T defaultValue, Class clazz, Predicate isEmpty) {
    Assert.notBlank("key", key);

    if (!parameters.containsKey(key)) {
      return defaultValue;
    }

    T value = get(key, clazz);

    if (isEmpty.test(value)) {
      return defaultValue;
    }

    return value;
  }

   T get(String key, Class clazz) {
    Assert.notBlank("key", key);

    Object property = parameters.get(key);

    if (property == null) {
      throw new UnknownPropertyException(key);
    }

    if (clazz.isInstance(property)) {
      return clazz.cast(property);
    }

    throw InvalidPropertyTypeException.builder().key(key).expectedType(clazz).actualType(property.getClass());
  }

  public Map get() {
    return parameters();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy