<commit_before>/*
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.b2international.snowowl.datastore.index.mapping;

import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.TermsFilter;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortField.Type;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.BytesRef;

import com.b2international.commons.CompareUtils;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;

/**
 * @since 4.3
 */
public abstract class IndexFieldBase<T> implements IndexField<T> {

	private String fieldName;
	private Store store;

	public IndexFieldBase(String fieldName, boolean store) {
		this.fieldName = fieldName;
		this.store = store ? Store.YES : Store.NO;
	}
	
	public final Store isStored() {
		return store;
	}
	
	@Override
	public void copyTo(Document source, Document target) {
		for (T t : getValues(source)) {
			addTo(target, t);
		}
	}
	
	protected abstract T getValue(IndexableField field);
	
	protected abstract BytesRef toBytesRef(T value);
	
	protected abstract IndexableField toField(T value);
	
	protected abstract Type getSortFieldType();
	
	@Override
	public final String fieldName() {
		return fieldName;
	}
	
	@Override
	public final T getValue(Document doc) {
		return getValue(getField(doc));
	}
	
	@Override
	public final String getValueAsString(Document doc) {
		return getValue(doc).toString();
	}
	<commit_msg>SO-1352: throw NPE if field is missing from doc when getting value<commit_after>/*
 * Copyright 2011-2015 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * 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.b2international.snowowl.datastore.index.mapping;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.List;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.Term;
import org.apache.lucene.queries.TermsFilter;
import org.apache.lucene.search.Filter;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.SortField.Type;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.util.BytesRef;

import com.b2international.commons.CompareUtils;
import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;

/**
 * @since 4.3
 */
public abstract class IndexFieldBase<T> implements IndexField<T> {

	private String fieldName;
	private Store store;

	public IndexFieldBase(String fieldName, boolean store) {
		this.fieldName = fieldName;
		this.store = store ? Store.YES : Store.NO;
	}
	
	public final Store isStored() {
		return store;
	}
	
	@Override
	public void copyTo(Document source, Document target) {
		for (T t : getValues(source)) {
			addTo(target, t);
		}
	}
	
	protected abstract T getValue(IndexableField field);
	
	protected abstract BytesRef toBytesRef(T value);
	
	protected abstract IndexableField toField(T value);
	
	protected abstract Type getSortFieldType();
	
	@Override
	public final String fieldName() {
		return fieldName;
	}
	
	@Override
	public final T getValue(Document doc) {
		return getValue(checkNotNull(getField(doc), "Missing field %s from doc [%s]", fieldName(), doc));
	}
	
	@Override
	public final String getValueAsString(Document doc) {
		return getValue(doc).toString();
	}
	
{'fraction_non_alphanumeric': 0.09848919487473705, 'fraction_numerical': 0.007649646203863071, 'mean_word_length': 4.623655913978495, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1925326', 'n_tokens_mistral': 1660, 'n_tokens_neox': 1540, 'n_words': 450}