com.jparams.store.index.IndexDefinition Maven / Gradle / Ivy
package com.jparams.store.index;
import java.util.Collection;
import java.util.Collections;
import com.jparams.store.index.comparison.ComparisonPolicy;
import com.jparams.store.index.comparison.DefaultComparisonPolicy;
import com.jparams.store.index.reducer.Reducer;
/**
* Definition of an index to be created
*
* @param key type
* @param value type
*/
public final class IndexDefinition
{
private final KeyMapper, V> keyMapper;
private ComparisonPolicy comparisonPolicy;
private Reducer reducer;
private IndexDefinition(final KeyMapper, V> keyMapper)
{
this.keyMapper = keyMapper;
this.comparisonPolicy = new DefaultComparisonPolicy<>();
this.reducer = null;
}
/**
* Specify a function that reduces multiple values that map to the same key.
*
* @param reducer reduction function
* @return index build
*/
public IndexDefinition withReducer(final Reducer reducer)
{
this.reducer = reducer;
return this;
}
/**
* Define the policy for mapping and comparing indexed keys. For example, we can specify a
* {@link com.jparams.store.index.comparison.string.CaseInsensitiveComparisonPolicy} when we want store and compare
* indexes insensitive of case.
*
* @param comparisonPolicy policy to define how indexed keys are compared
* @return index build
*/
public IndexDefinition withComparisonPolicy(final ComparisonPolicy comparisonPolicy)
{
this.comparisonPolicy = comparisonPolicy;
return this;
}
/**
* Specify a function that maps a given value to a single indexed key. For example, we can index a Person object by
* its firstName field. Example: IndexDefinition.withKeyMapping(Person::getFirstName)
*
* To map a value to multiple keys, see {@link IndexDefinition#withKeyMappings(KeyMapper)}
*
* @param keyMapper a function that maps a given value to a single indexed key. Note: If the mapper returns a null, indexing will be skipped for the given value.
* @param key type
* @param value type
* @return index build
*/
public static IndexDefinition withKeyMapping(final KeyMapper keyMapper)
{
return withKeyMappings(value -> {
final K key = keyMapper.map(value);
return key == null ? Collections.emptyList() : Collections.singletonList(key);
});
}
/**
* Specify a function that maps a given value to a one or more indexed keys. For example, we can index a Person object by
* both its firstName and lastName fields. Example: IndexDefinition.withKeyMapping(person -> Arrays.asList(person.getFirstName(), person.getLastName()))
*
* @param mapper a function that maps a given value to one or more indexed keys. Note: any null values returned in the collection will be ignored
* @param key type
* @param value type
* @return index build
*/
public static IndexDefinition withKeyMappings(final KeyMapper, V> mapper)
{
return new IndexDefinition<>(mapper);
}
KeyMapper, V> getKeyMapper()
{
return keyMapper;
}
ComparisonPolicy getComparisonPolicy()
{
return comparisonPolicy;
}
Reducer getReducer()
{
return reducer;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy