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

org.hibernate.search.util.impl.ScopedAnalyzer Maven / Gradle / Ivy

/*
 * Hibernate Search, full-text search for your domain model
 *
 * License: GNU Lesser General Public License (LGPL), version 2.1 or later
 * See the lgpl.txt file in the root directory or .
 */
package org.hibernate.search.util.impl;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.AnalyzerWrapper;

/**
 * A {@code ScopedAnalyzer} is a wrapper class containing all analyzers for a given class.
 *
 * {@code ScopedAnalyzer} behaves similar to {@link org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper}
 * by delegating requests for {@code TokenStream}s to the underlying {@code Analyzer} depending on the requested field name.
 *
 * @author Emmanuel Bernard
 * @author Sanne Grinovero
 */
public final class ScopedAnalyzer extends AnalyzerWrapper {

	private Analyzer globalAnalyzer;
	private final Map scopedAnalyzers = new HashMap();

	public ScopedAnalyzer(Analyzer globalAnalyzer) {
		this( globalAnalyzer, Collections.emptyMap() );
	}

	private ScopedAnalyzer(Analyzer globalAnalyzer, Map scopedAnalyzers) {
		super( PER_FIELD_REUSE_STRATEGY );
		this.globalAnalyzer = globalAnalyzer;
		for ( Map.Entry entry : scopedAnalyzers.entrySet() ) {
			addScopedAnalyzer( entry.getKey(), entry.getValue() );
		}
	}

	public void setGlobalAnalyzer(Analyzer globalAnalyzer) {
		this.globalAnalyzer = globalAnalyzer;
	}

	public void addScopedAnalyzer(String scope, Analyzer scopedAnalyzer) {
		scopedAnalyzers.put( scope, scopedAnalyzer );
	}

	@Override
	protected Analyzer getWrappedAnalyzer(String fieldName) {
		final Analyzer analyzer = scopedAnalyzers.get( fieldName );
		if ( analyzer == null ) {
			return globalAnalyzer;
		}
		else {
			return analyzer;
		}
	}

	@Override
	public ScopedAnalyzer clone() {
		ScopedAnalyzer clone = new ScopedAnalyzer( globalAnalyzer, scopedAnalyzers );
		return clone;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy