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

com.threewks.thundr.search.gae.mediator.FieldMediatorSet Maven / Gradle / Ivy

There is a newer version: 3.0.0
Show newest version
/*
 * This file is a component of thundr, a software library from 3wks.
 * Read more: http://3wks.github.io/thundr/
 * Copyright (C) 2015 3wks, 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.threewks.thundr.search.gae.mediator;

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

import com.atomicleopard.expressive.ETransformer;
import com.threewks.thundr.search.SearchException;
import com.threewks.thundr.search.gae.meta.IndexType;

/**
 * A {@link FieldMediator} is used to mediate between a java type and a low level search api type.
 * The {@link FieldMediatorSet} groups the full set of {@link FieldMediator} implementations and matches
 * them to the appropriate {@link IndexType}.
 */
public class FieldMediatorSet {
	private Map> fieldMediators = initIndexFieldBuilders();

	@SuppressWarnings("unchecked")
	public  FieldMediator get(IndexType indexType) {
		FieldMediator result = (FieldMediator) fieldMediators.get(indexType);
		if (result == null) {
			throw new SearchException("No %s present for %s '%s'", FieldMediator.class.getSimpleName(), IndexType.class.getSimpleName(), indexType);
		}
		return result;
	}

	public  void put(IndexType indexType, FieldMediator mediator) {
		this.fieldMediators.put(indexType, mediator);
	}

	protected static Map> initIndexFieldBuilders() {
		Map> map = new HashMap<>();
		map.put(IndexType.Automatic, new TextFieldMediator());
		map.put(IndexType.BigDecimal, new BigDecimalFieldMediator());
		map.put(IndexType.Date, new DateFieldMediator());
		map.put(IndexType.GeoPoint, new GeoPointFieldMediator());
		map.put(IndexType.Html, new HtmlFieldMediator());
		map.put(IndexType.Identifier, new AtomFieldMediator());
		map.put(IndexType.SmallDecimal, new SmallDecimalFieldMediator());
		map.put(IndexType.Text, new TextFieldMediator());
		return map;
	}

	public static final ETransformer quote = new ETransformer() {
		@Override
		public String from(String from) {
			from = from.replaceAll("\"", "\\\\\"");
			return String.format("\"%s\"", from);
		}
	};
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy