<commit_before>/*
 * Copyright 2015-2016 Canoo Engineering AG.
 *
 * 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.canoo.dolphin.client.impl;

import com.canoo.dolphin.client.ClientBeanManager;
import com.canoo.dolphin.client.ClientConfiguration;
import com.canoo.dolphin.client.ClientContext;
import com.canoo.dolphin.client.ClientInitializationException;
import com.canoo.dolphin.client.ControllerInitalizationException;
import com.canoo.dolphin.client.ControllerProxy;
import com.canoo.dolphin.client.State;
import com.canoo.dolphin.impl.PlatformConstants;
import com.canoo.dolphin.util.Assert;
import com.canoo.dolphin.util.DolphinRemotingException;
import org.opendolphin.core.client.ClientDolphin;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ClientContextImpl implements ClientContext {

    private final ClientDolphin clientDolphin;

    private final ClientBeanManagerImpl clientBeanManager;

    private final ClientPlatformBeanRepository platformBeanRepository;

    private State state = State.CREATED;

    private final ControllerProxyFactory controllerProxyFactory;

    private final DolphinCommandHandler dolphinCommandHandler;

    public ClientContextImpl(ClientConfiguration clientConfiguration, ClientDolphin clientDolphin, ControllerProxyFactory controllerProxyFactory, DolphinCommandHandler dolphinCommandHandler, ClientPlatformBeanRepository platformBeanRepository, ClientBeanManagerImpl clientBeanManager) throws ExecutionException, InterruptedException {
        Assert.requireNonNull(clientDolphin, "clientDolphin");
        Assert.requireNonNull(controllerProxyFactory, "controllerProxyFactory");
        Assert.requireNonNull(dolphinCommandHandler, "dolphinCommandHandler");
        Assert.requireNonNull(platformBeanRepository, "platformBeanRepository");
        Assert.requireNonNull(clientBeanManager, "clientBeanManager");
        this.clientDolphin = clientDolphin;
        this.controllerProxyFactory = controllerProxyFactory;
        this.dolphinCommandHandler = dolphinCommandHandler;
        this.platformBeanRepository = platformBeanRepository;
        this.clientBeanManager = clientBeanManager;
        try {
            dolphinCommandHandler.invokeDolphinCommand(PlatformConstants.INIT_CONTEXT_COMMAND_NAME).handle((v, e) -> {
                if (e != null) {
                    state = State.DESTROYED;
                    throw new ClientInitializationException("Can't call init action!");
                } else {
                    state = State.INITIALIZED;
                }
                return null;
            }).get(clientConfiguration.getConnectionTimeout(), TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            throw new ClientInitializationException("Can not connect to server!", e);
        }
    }

    @Override
    public synchronized <T> CompletableFuture<ControllerProxy<T>> createController(String name) {
        Assert.requireNonBlank(name, "name");
        checkForInitializedState();

        CompletableFuture<ControllerProxy<T>> factoryResult = controllerProxyFactory.create(name);

        return factoryResult.handle((c, e) -> {
            if (e != null) {
                throw new ControllerInitalizationException(e);
            }
            return c;
        });
    }

    @Override
    public synchronized ClientBeanManager getBeanManager() {
        checkForInitializedState();
        return clientBeanManager;
    }

    @Override
    public synchronized CompletableFuture<Void> disconnect() {
        checkForInitializedState();
        state = State.DESTROYING;
        clientDolphin.stopPushListening();<commit_msg>Based on review<commit_after>/*
 * Copyright 2015-2016 Canoo Engineering AG.
 *
 * 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.canoo.dolphin.client.impl;

import com.canoo.dolphin.client.ClientBeanManager;
import com.canoo.dolphin.client.ClientConfiguration;
import com.canoo.dolphin.client.ClientContext;
import com.canoo.dolphin.client.ClientInitializationException;
import com.canoo.dolphin.client.ControllerInitalizationException;
import com.canoo.dolphin.client.ControllerProxy;
import com.canoo.dolphin.client.State;
import com.canoo.dolphin.impl.PlatformConstants;
import com.canoo.dolphin.util.Assert;
import com.canoo.dolphin.util.DolphinRemotingException;
import org.opendolphin.core.client.ClientDolphin;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class ClientContextImpl implements ClientContext {

    private final ClientDolphin clientDolphin;

    private final ClientBeanManagerImpl clientBeanManager;

    private final ClientPlatformBeanRepository platformBeanRepository;

    private State state = State.CREATED;

    private final ControllerProxyFactory controllerProxyFactory;

    private final DolphinCommandHandler dolphinCommandHandler;

    public ClientContextImpl(ClientConfiguration clientConfiguration, ClientDolphin clientDolphin, ControllerProxyFactory controllerProxyFactory, DolphinCommandHandler dolphinCommandHandler, ClientPlatformBeanRepository platformBeanRepository, ClientBeanManagerImpl clientBeanManager) throws ExecutionException, InterruptedException {
        Assert.requireNonNull(clientDolphin, "clientDolphin");
        Assert.requireNonNull(controllerProxyFactory, "controllerProxyFactory");
        Assert.requireNonNull(dolphinCommandHandler, "dolphinCommandHandler");
        Assert.requireNonNull(platformBeanRepository, "platformBeanRepository");
        Assert.requireNonNull(clientBeanManager, "clientBeanManager");
        this.clientDolphin = clientDolphin;
        this.controllerProxyFactory = controllerProxyFactory;
        this.dolphinCommandHandler = dolphinCommandHandler;
        this.platformBeanRepository = platformBeanRepository;
        this.clientBeanManager = clientBeanManager;
        try {
            dolphinCommandHandler.invokeDolphinCommand(PlatformConstants.INIT_CONTEXT_COMMAND_NAME).handle((v, e) -> {
                if (e != null) {
                    state = State.DESTROYED;
                    throw new ClientInitializationException("Can't call init action!");
                } else {
                    state = State.INITIALIZED;
                }
                return null;
            }).get(clientConfiguration.getConnectionTimeout(), TimeUnit.MILLISECONDS);
        } catch (TimeoutException e) {
            throw new ClientInitializationException("Can not connect to server!", e);
        }
    }

    @Override
    public synchronized <T> CompletableFuture<ControllerProxy<T>> createController(String name) {
        Assert.requireNonBlank(name, "name");
        checkForInitializedState();

        return controllerProxyFactory.<T>create(name).handle((c, e) -> {
            if (e != null) {
                throw new ControllerInitalizationException(e);
            }
            return c;
        });
    }

    @Override
    public synchronized ClientBeanManager getBeanManager() {
        checkForInitializedState();
        return clientBeanManager;
    }

    @Override
    public synchronized CompletableFuture<Void> disconnect() {
        checkForInitializedState();
        state = State.DESTROYING;
        clientDolphin.stopPushListening();
{'fraction_non_alphanumeric': 0.07603767003836763, 'fraction_numerical': 0.0027903732124171608, 'mean_word_length': 3.7948717948717947, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 18, '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': '4675209', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2078, 'n_words': 510}