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

org.hibernate.search.engine.cfg.impl.AbstractConfigurationProperty Maven / Gradle / Ivy

The newest version!
/*
 * SPDX-License-Identifier: Apache-2.0
 * Copyright Red Hat Inc. and Hibernate Authors
 */
package org.hibernate.search.engine.cfg.impl;

import java.lang.invoke.MethodHandles;
import java.util.Optional;
import java.util.function.Function;

import org.hibernate.search.engine.cfg.ConfigurationPropertySource;
import org.hibernate.search.engine.cfg.spi.ConfigurationProperty;
import org.hibernate.search.engine.cfg.spi.ConvertUtils;
import org.hibernate.search.engine.logging.impl.Log;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;

abstract class AbstractConfigurationProperty implements ConfigurationProperty {

	private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

	private final String key;

	AbstractConfigurationProperty(String key) {
		this.key = key;
	}

	@Override
	public T get(ConfigurationPropertySource source) {
		return doGet( source, Function.identity() );
	}

	@Override
	public  R getAndTransform(ConfigurationPropertySource source, Function transform) {
		return doGet( source, transform );
	}

	abstract  R convert(Optional rawValue, Function transform);

	 R doGet(ConfigurationPropertySource source, Function transform) {
		Optional rawValue = source.get( key ).map( ConvertUtils::trimIfString );
		try {
			return convert( rawValue, transform );
		}
		catch (RuntimeException e) {
			String displayedKey = key;

			// Try to display the key that must be set by the user, if possible
			try {
				Optional resolvedKey = source.resolve( key );
				if ( resolvedKey.isPresent() ) {
					displayedKey = resolvedKey.get();
				}
			}
			catch (RuntimeException e2) {
				// Take care not to erase the original exception
				e.addSuppressed( e2 );
			}

			throw log.unableToConvertConfigurationProperty(
					displayedKey, rawValue.isPresent() ? rawValue.get() : "", e.getMessage(), e
			);
		}
	}

	@Override
	public Optional resolve(ConfigurationPropertySource source) {
		return source.resolve( key );
	}

	@Override
	public String resolveOrRaw(ConfigurationPropertySource source) {
		return resolve( source ).orElse( key );
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy