/**
 * LOGICAL-PARADOX.ORG
 * Copyright (C)2004 satoshi akabane(akabane@logical-paradox.org)
 *
 */
package org.logical_paradox.phalanx;

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

import org.logical_paradox.phalanx.binding.condition.Condition;
import org.logical_paradox.phalanx.impl.NgramFTSSearchLogic;

/**
 * 検索処理のロジックを生成するファクトリ
 * @author satoshi akabane@logical-paradox.org
 * @version $Revision: 1.3 $
 */
public class SearchLogicFactory {
	/** 検索ロジックのマップ */
	private static final Map<String, SearchLogic> map;
	protected final static String NGRAM_FTS_SEARCH_LOGIC = "ngramFtsSearchLogic";

	static {
		HashMap<String, SearchLogic> m = new HashMap<String, SearchLogic>();
		// N-gram索引を使用する検索ロジック
		m.put(NGRAM_FTS_SEARCH_LOGIC, new NgramFTSSearchLogic());

		// read onlyなマップの作成
		map = Collections.unmodifiableMap(m);
	}

	/**
	 * 検索条件にて指定されたタイプの検索ロジックを生成する
	 * @param condition 検索条件
	 * @return 検索処理を実際に担当するロジック
	 */
	public static final SearchLogic getSearchLogic(Condition condition) {
		String searchMethod = condition.getMethod();
		if(searchMethod == null || searchMethod.trim().length() == 0) {
			searchMethod = NGRAM_FTS_SEARCH_LOGIC;
		}

		SearchLogic logic = map.get(searchMethod);
		if(logic == null) {
			throw new IllegalArgumentException("undefined search method is specified [" + searchMethod + "]");
		}

		return logic;
	}
}
