<commit_before>/*
 *  Copyright 2017 <NAME>.
 *
 *  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 org.teavm.idea;

import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.components.ServiceManager;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.teavm.idea.daemon.TeaVMBuildDaemon;
import org.teavm.idea.daemon.TeaVMDaemonInfo;
import org.teavm.idea.jps.model.TeaVMJpsWorkspaceConfiguration;

public class TeaVMDaemonComponent implements ApplicationComponent {
    private TeaVMDaemonInfo daemonInfo;
    private boolean incremental;
    private int daemonMemory;

    @Override
    public void initComponent() {
        TeaVMWorkspaceConfigurationStorage configurationStorage = ServiceManager.getService(
                TeaVMWorkspaceConfigurationStorage.class);
        if (configurationStorage != null) {
            TeaVMJpsWorkspaceConfiguration configuration = configurationStorage.getState();
            incremental = configuration.isIncremental();
            daemonMemory = configuration.getDaemonMemory();
            if (configuration.isDaemonEnabled()) {
                startDaemon();
            }
        }
    }

    private TeaVMWorkspaceConfigurationStorage getConfigurationStorage() {
        return ServiceManager.getService(TeaVMWorkspaceConfigurationStorage.class);
    }

    @Override
    public void disposeComponent() {
        if (daemonInfo != null) {
            daemonInfo.getProcess().destroy();
        }
    }

    public boolean isDaemonRunning() {
        return daemonInfo != null;
    }

    public int getDaemonPort() {
        return daemonInfo.getPort();
    }

    public void startDaemon() {
        if (daemonInfo == null) {
            try {
                daemonInfo = TeaVMBuildDaemon.start(incremental, daemonMemory);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            updateConfiguration(true, daemonMemory);
        }
    }

    public void stopDaemon() {
        if (daemonInfo != null) {
            daemonInfo.getProcess().destroy();
            daemonInfo = null;
            updateConfiguration(false, daemonMemory);
        }
    }

    public boolean isIncremental() {
        return incremental;
    }

    public void setIncremental(boolean incremental) {
        this.incremental = incremental;
    }

    public int getDaemonMemory() {
        return daemonMemory;
    }

    public void setDaemonMemory(int daemonMemory) {
        this.daemonMemory = daemonMemory;
    }

    public void applyChanges() {
        TeaVMWorkspaceConfigurationStorage configurationStorage = getConfigurationStorage();
        TeaVMJpsWorkspaceConfiguration configuration = configurationStorage.getState();
        configuration.setIncremental(incremental);
        configuration.setDaemonMemory(daemonMemory);
        configurationStorage.loadState(configuration);
    }

    private void updateConfiguration(boolean daemonEnabled, int daemonMemory) {
        TeaVMWorkspaceConfigurationStorage configurationStorage = getConfigurationStorage();
        TeaVMJpsWorkspaceConfiguration configuration = configurationStorage.getState();
        configuration.setDaemonEnabled(daemonEnabled);
        configuration.setDaemonMemory(daemonMemory);
        configurationStorage.loadState(configuration);
    }

    @NotNull
    @Override
    public String getComponentName() {
        return "TeaVM daemon";
    }
}<commit_msg>IDEA: fix starting daemon with specified amount of memory<commit_after>/*
 *  Copyright 2017 <NAME>.
 *
 *  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 org.teavm.idea;

import com.intellij.openapi.components.ApplicationComponent;
import com.intellij.openapi.components.ServiceManager;
import java.io.IOException;
import org.jetbrains.annotations.NotNull;
import org.teavm.idea.daemon.TeaVMBuildDaemon;
import org.teavm.idea.daemon.TeaVMDaemonInfo;
import org.teavm.idea.jps.model.TeaVMJpsWorkspaceConfiguration;

public class TeaVMDaemonComponent implements ApplicationComponent {
    private TeaVMDaemonInfo daemonInfo;
    private boolean incremental;
    private int daemonMemory;

    @Override
    public void initComponent() {
        TeaVMWorkspaceConfigurationStorage configurationStorage = ServiceManager.getService(
                TeaVMWorkspaceConfigurationStorage.class);
        if (configurationStorage != null) {
            TeaVMJpsWorkspaceConfiguration configuration = configurationStorage.getState();
            incremental = configuration.isIncremental();
            daemonMemory = configuration.getDaemonMemory();
            if (configuration.isDaemonEnabled()) {
                startDaemon();
            }
        }
    }

    private TeaVMWorkspaceConfigurationStorage getConfigurationStorage() {
        return ServiceManager.getService(TeaVMWorkspaceConfigurationStorage.class);
    }

    @Override
    public void disposeComponent() {
        if (daemonInfo != null) {
            daemonInfo.getProcess().destroy();
        }
    }

    public boolean isDaemonRunning() {
        return daemonInfo != null;
    }

    public int getDaemonPort() {
        return daemonInfo.getPort();
    }

    public void startDaemon() {
        if (daemonInfo == null) {
            try {
                daemonInfo = TeaVMBuildDaemon.start(incremental, daemonMemory);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            updateConfiguration(true);
        }
    }

    public void stopDaemon() {
        if (daemonInfo != null) {
            daemonInfo.getProcess().destroy();
            daemonInfo = null;
            updateConfiguration(false);
        }
    }

    public boolean isIncremental() {
        return incremental;
    }

    public void setIncremental(boolean incremental) {
        this.incremental = incremental;
    }

    public int getDaemonMemory() {
        return daemonMemory;
    }

    public void setDaemonMemory(int daemonMemory) {
        this.daemonMemory = daemonMemory;
    }

    public void applyChanges() {
        TeaVMWorkspaceConfigurationStorage configurationStorage = getConfigurationStorage();
        TeaVMJpsWorkspaceConfiguration configuration = configurationStorage.getState();
        configuration.setIncremental(incremental);
        configuration.setDaemonMemory(daemonMemory);
        configurationStorage.loadState(configuration);
    }

    private void updateConfiguration(boolean daemonEnabled) {
        TeaVMWorkspaceConfigurationStorage configurationStorage = getConfigurationStorage();
        TeaVMJpsWorkspaceConfiguration configuration = configurationStorage.getState();
        configuration.setDaemonEnabled(daemonEnabled);
        configurationStorage.loadState(configuration);
    }

    @NotNull
    @Override
    public String getComponentName() {
        return "TeaVM daemon";
    }
}
{'fraction_non_alphanumeric': 0.0764698508211107, 'fraction_numerical': 0.0020057665789143786, 'mean_word_length': 2.9790523690773068, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3111407', 'n_tokens_mistral': 2050, 'n_tokens_neox': 1994, 'n_words': 489}