<commit_before>/*
 * Copyright 2011-2017 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.snomed.datastore.request;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

import com.b2international.commons.ExplicitFirstOrdering;
import com.b2international.commons.http.ExtendedLocale;
import com.b2international.snowowl.core.domain.IComponentRef;
import com.b2international.snowowl.snomed.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.SnomedConstants.LanguageCodeReferenceSetIdentifierMapping;
import com.b2international.snowowl.snomed.core.domain.Acceptability;
import com.b2international.snowowl.snomed.core.domain.SnomedDescription;
import com.b2international.snowowl.snomed.core.domain.SnomedDescriptions;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;

/**
 * @since 4.5
 */
public abstract class DescriptionRequestHelper {

	private static final class ExtractFirstFunction implements Function<Collection<SnomedDescription>, SnomedDescription> {
		private static final ExtractFirstFunction INSTANCE = new ExtractFirstFunction();

		@Override
		public SnomedDescription apply(Collection<SnomedDescription> input) {
			return Iterables.getFirst(input, null);
		}
	}

	private static class ExtractBestFunction<T> implements Function<Collection<SnomedDescription>, SnomedDescription> {
		
		private final List<T> orderedValues;
		private final Function<SnomedDescription, T> valuesExtractor;
		private final Ordering<SnomedDescription> ordering;

		private ExtractBestFunction(List<T> orderedValues, Function<SnomedDescription, T> valuesExtractor) {
			this.orderedValues = orderedValues;
			this.valuesExtractor = valuesExtractor;
			this.ordering = ExplicitFirstOrdering.create(orderedValues).onResultOf(valuesExtractor);
		}

		@Override 
		public SnomedDescription apply(Collection<SnomedDescription> descriptions) {
			try {
				
				SnomedDescription candidate = ordering.max(descriptions);
				
				/*<commit_msg>TCC-12 removed unused code from DescriptionRequestHelper<commit_after>/*
 * Copyright 2011-2017 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.snomed.datastore.request;

import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newHashMap;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;

import com.b2international.commons.ExplicitFirstOrdering;
import com.b2international.commons.http.ExtendedLocale;
import com.b2international.snowowl.core.domain.IComponentRef;
import com.b2international.snowowl.snomed.SnomedConstants.Concepts;
import com.b2international.snowowl.snomed.SnomedConstants.LanguageCodeReferenceSetIdentifierMapping;
import com.b2international.snowowl.snomed.core.domain.Acceptability;
import com.b2international.snowowl.snomed.core.domain.SnomedDescription;
import com.b2international.snowowl.snomed.core.domain.SnomedDescriptions;
import com.google.common.base.Function;
import com.google.common.base.Predicates;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;

/**
 * @since 4.5
 */
public abstract class DescriptionRequestHelper {

	private static class ExtractBestFunction<T> implements Function<Collection<SnomedDescription>, SnomedDescription> {
		
		private final List<T> orderedValues;
		private final Function<SnomedDescription, T> valuesExtractor;
		private final Ordering<SnomedDescription> ordering;

		private ExtractBestFunction(List<T> orderedValues, Function<SnomedDescription, T> valuesExtractor) {
			this.orderedValues = orderedValues;
			this.valuesExtractor = valuesExtractor;
			this.ordering = ExplicitFirstOrdering.create(orderedValues).onResultOf(valuesExtractor);
		}

		@Override 
		public SnomedDescription apply(Collection<SnomedDescription> descriptions) {
			try {
				
				SnomedDescription candidate = ordering.max(descriptions);
				
				/*
{'fraction_non_alphanumeric': 0.08782565951171555, 'fraction_numerical': 0.008520399803375388, 'mean_word_length': 5.598918918918919, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, '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': '2411560', 'n_tokens_mistral': 1829, 'n_tokens_neox': 1684, 'n_words': 414}