<commit_before>/*
 * Copyright (C) 2015 SoftIndex LLC.
 *
 * 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 io.datakernel.guice.servicegraph;

import io.datakernel.async.CompletionCallback;
import io.datakernel.eventloop.NioEventloop;
import io.datakernel.eventloop.NioServer;
import io.datakernel.eventloop.NioService;
import io.datakernel.service.AsyncService;
import io.datakernel.service.AsyncServiceCallback;
import io.datakernel.service.Service;
import org.slf4j.Logger;

import javax.sql.DataSource;
import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * Static utility methods pertaining to ConcurrentService. Creates ConcurrentService from some other type of instances.
 */
public final class AsyncServiceAdapters {
	private static final Logger logger = getLogger(AsyncServiceAdapters.class);

	private AsyncServiceAdapters() {
	}

	public static AsyncServiceAdapter<AsyncService> forAsyncService() {
		return new AsyncServiceAdapter<AsyncService>() {
			@Override
			public AsyncService toService(AsyncService service, Executor executor) {
				return service;
			}
		};
	}

	private static CompletionCallback toCompletionCallback(final AsyncServiceCallback callback) {
		return new CompletionCallback() {
			@Override
			public void onComplete() {
				callback.onComplete();
			}

			@Override
			public void onException(Exception exception) {
				callback.onException(exception);
			}
		};
	}

	public static AsyncServiceAdapter<NioService> forNioService() {
		return new AsyncServiceAdapter<NioService>() {

			@Override
			public AsyncService toService(final NioService node, Executor executor) {
				return new AsyncService() {
					@Override
					public void start(final AsyncServiceCallback callback) {
						node.getNioEventloop().postConcurrently(new Runnable() {
							@Override
							public void run() {
								node.start(toCompletionCallback(callback));
								callback.onComplete();
							}
						});
					}

					@Override
					public void stop(final AsyncServiceCallback callback) {
						node.getNioEventloop().postConcurrently(new Runnable() {
							@Override
							public void run() {
								node.stop(toCompletionCallback(callback));
								callback.onComplete();
							}
						});
					}
				};
			}
		};
	}<commit_msg>Fix adapter for NioService, make toCompletionCallback(AsyncServiceCallback) public<commit_after>/*
 * Copyright (C) 2015 SoftIndex LLC.
 *
 * 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 io.datakernel.guice.servicegraph;

import io.datakernel.async.CompletionCallback;
import io.datakernel.eventloop.NioEventloop;
import io.datakernel.eventloop.NioServer;
import io.datakernel.eventloop.NioService;
import io.datakernel.service.AsyncService;
import io.datakernel.service.AsyncServiceCallback;
import io.datakernel.service.Service;
import org.slf4j.Logger;

import javax.sql.DataSource;
import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.util.List;
import java.util.Timer;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * Static utility methods pertaining to ConcurrentService. Creates ConcurrentService from some other type of instances.
 */
public final class AsyncServiceAdapters {
	private static final Logger logger = getLogger(AsyncServiceAdapters.class);

	private AsyncServiceAdapters() {
	}

	public static AsyncServiceAdapter<AsyncService> forAsyncService() {
		return new AsyncServiceAdapter<AsyncService>() {
			@Override
			public AsyncService toService(AsyncService service, Executor executor) {
				return service;
			}
		};
	}

	public static CompletionCallback toCompletionCallback(final AsyncServiceCallback callback) {
		return new CompletionCallback() {
			@Override
			public void onComplete() {
				callback.onComplete();
			}

			@Override
			public void onException(Exception exception) {
				callback.onException(exception);
			}
		};
	}

	public static AsyncServiceAdapter<NioService> forNioService() {
		return new AsyncServiceAdapter<NioService>() {

			@Override
			public AsyncService toService(final NioService node, Executor executor) {
				return new AsyncService() {
					@Override
					public void start(final AsyncServiceCallback callback) {
						node.getNioEventloop().postConcurrently(new Runnable() {
							@Override
							public void run() {
								node.start(toCompletionCallback(callback));
							}
						});
					}

					@Override
					public void stop(final AsyncServiceCallback callback) {
						node.getNioEventloop().postConcurrently(new Runnable() {
							@Override
							public void run() {
								node.stop(toCompletionCallback(callback));
							}
						});
					}
				};
			}
		};
	}
{'fraction_non_alphanumeric': 0.08740234375, 'fraction_numerical': 0.0032552083333333335, 'mean_word_length': 4.150880134115675, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '4943098', 'n_tokens_mistral': 1985, 'n_tokens_neox': 1649, 'n_words': 470}