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

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

There is a newer version: 8.0.0.Alpha1
Show newest version
/*
 * SPDX-License-Identifier: Apache-2.0
 * Copyright Red Hat Inc. and Hibernate Authors
 */
package org.hibernate.search.engine.cfg.impl;

import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.regex.Pattern;

import org.hibernate.search.engine.cfg.spi.ConvertUtils;
import org.hibernate.search.engine.cfg.spi.DefaultedPropertyContext;
import org.hibernate.search.engine.cfg.spi.OptionalConfigurationProperty;
import org.hibernate.search.engine.cfg.spi.OptionalPropertyContext;

final class OptionalPropertyContextImpl implements OptionalPropertyContext {

	private static final Pattern MULTI_VALUE_SEPARATOR_PATTERN = Pattern.compile( "," );

	private final String key;
	private final Function converter;

	OptionalPropertyContextImpl(String key, Function converter) {
		this.key = key;
		this.converter = converter;
	}

	@Override
	public OptionalPropertyContext substitute(UnaryOperator substitution) {
		return new OptionalPropertyContextImpl<>( key, substitution.andThen( converter ) );
	}

	@Override
	public OptionalPropertyContext substitute(Object expected, Object replacement) {
		return substitute( v -> {
			if ( Objects.equals( v, expected ) ) {
				return replacement;
			}
			else {
				return v;
			}
		} );
	}

	@Override
	public OptionalPropertyContext validate(Consumer validation) {
		return new OptionalPropertyContextImpl<>( key, converter.andThen( value -> {
			validation.accept( value );
			return value;
		} ) );
	}

	@Override
	public OptionalPropertyContext> multivalued() {
		return new OptionalPropertyContextImpl<>(
				key,
				v -> ConvertUtils.convertMultiValue( MULTI_VALUE_SEPARATOR_PATTERN, converter, v )
		);
	}

	@Override
	public DefaultedPropertyContext withDefault(T defaultValue) {
		return new DefaultedPropertyContextImpl<>( key, converter, () -> defaultValue );
	}

	@Override
	public DefaultedPropertyContext withDefault(Supplier defaultValueSupplier) {
		return new DefaultedPropertyContextImpl<>( key, converter, defaultValueSupplier );
	}

	@Override
	public OptionalConfigurationProperty build() {
		return new OptionalConfigurationPropertyImpl<>( key, converter );
	}
}