<commit_before>/*
 * Copyright 2011-2022 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.core.request;

import java.time.LocalDate;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.b2international.commons.http.ExtendedLocale;
import com.b2international.commons.options.Options;
import com.b2international.index.Hits;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.date.EffectiveTimes;
import com.b2international.snowowl.core.domain.CollectionResource;
import com.b2international.snowowl.core.plugin.ClassPathScanner;
import com.google.common.collect.Iterables;

/**
 * @since 4.0
 * @param <T> - document type
 * @param <R> - domain type
 * @param <CR> - collection resource type
 */
public abstract class BaseResourceConverter<T, R, CR extends CollectionResource<R>> extends BaseResourceExpander<R> {

	protected BaseResourceConverter(ServiceProvider context, Options expand, List<ExtendedLocale> locales) {
		super(context, expand, locales);
	}

	public final CR convert(Hits<T> hits) {
		return convert(hits.getHits(), hits.getSearchAfter(), hits.getLimit(), hits.getTotal());
	}
	
	/**
	 * Convert a single internal index based entity to a resource based representation.
	 * 
	 * @param component
	 * @return
	 */
	public final R convert(T component) {
		return Iterables.getOnlyElement(convert(Collections.singleton(component), null, 1, 1));
	}

	/**
	 * Convert multiple internal index based entities to resource based representations.
	 * 
	 * @param components
	 * @param searchAfter
	 * @param limit
	 * @param total
	 * @return
	 */
	public final CR convert(Collection<T> components, String searchAfter, int limit, int total) {
		final List<R> results = components
				.stream()
				.map(this::toResource)
				.collect(Collectors.toList());
		
		if (!results.isEmpty()) {
			// expand using the current converter
			expand(results);
			// expand via plugins 
			expandViaPlugins(results);
		}
		
		return createCollectionResource(results, searchAfter, limit, total);
	}
	
	@Override
	public void expand(List<R> results) {
		// by default no expansions are performed
	}

	private final void expandViaPlugins(List<R> results) {
		context().service(ClassPathScanner.class).getComponentsByInterface(ResourceExpander.class)
			.stream()
			.filter(expander -> getType().equals(expander.getType()))
			.forEach(expander -> ((ResourceExpander<R>) expander).expand(results));
	}

	protected abstract CR createCollectionResource(List<R> results, String searchAfter, int limit, int total);<commit_msg>fix(core): certain tests do not have ClassPathScanner available...<commit_after>/*
 * Copyright 2011-2022 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.core.request;

import java.time.LocalDate;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import com.b2international.commons.http.ExtendedLocale;
import com.b2international.commons.options.Options;
import com.b2international.index.Hits;
import com.b2international.snowowl.core.ServiceProvider;
import com.b2international.snowowl.core.date.EffectiveTimes;
import com.b2international.snowowl.core.domain.CollectionResource;
import com.b2international.snowowl.core.plugin.ClassPathScanner;
import com.google.common.collect.Iterables;

/**
 * @since 4.0
 * @param <T> - document type
 * @param <R> - domain type
 * @param <CR> - collection resource type
 */
public abstract class BaseResourceConverter<T, R, CR extends CollectionResource<R>> extends BaseResourceExpander<R> {

	protected BaseResourceConverter(ServiceProvider context, Options expand, List<ExtendedLocale> locales) {
		super(context, expand, locales);
	}

	public final CR convert(Hits<T> hits) {
		return convert(hits.getHits(), hits.getSearchAfter(), hits.getLimit(), hits.getTotal());
	}
	
	/**
	 * Convert a single internal index based entity to a resource based representation.
	 * 
	 * @param component
	 * @return
	 */
	public final R convert(T component) {
		return Iterables.getOnlyElement(convert(Collections.singleton(component), null, 1, 1));
	}

	/**
	 * Convert multiple internal index based entities to resource based representations.
	 * 
	 * @param components
	 * @param searchAfter
	 * @param limit
	 * @param total
	 * @return
	 */
	public final CR convert(Collection<T> components, String searchAfter, int limit, int total) {
		final List<R> results = components
				.stream()
				.map(this::toResource)
				.collect(Collectors.toList());
		
		if (!results.isEmpty()) {
			// expand using the current converter
			expand(results);
			// expand via plugins 
			expandViaPlugins(results);
		}
		
		return createCollectionResource(results, searchAfter, limit, total);
	}
	
	@Override
	public void expand(List<R> results) {
		// by default no expansions are performed
	}

	private final void expandViaPlugins(List<R> results) {
		context().optionalService(ClassPathScanner.class).ifPresent(scanner -> {
			scanner.getComponentsByInterface(ResourceExpander.class)
				.stream()
				.filter(expander -> getType().equals(expander.getType()))
				.forEach(expander -> ((ResourceExpander<R>) expander).expand(results));
		});
	}

	protected abstract CR createCollectionResource(List<R> results, String searchAfter, int limit, int total);
{'fraction_non_alphanumeric': 0.10690287110568113, 'fraction_numerical': 0.00794135613927917, 'mean_word_length': 4.55470737913486, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 36, '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': '2776040', 'n_tokens_mistral': 2005, 'n_tokens_neox': 1818, 'n_words': 551}