<commit_before>package org.flexiblepower.runtime.ui.connectionspage;

import org.flexiblepower.messaging.ConnectionManager;
import org.flexiblepower.messaging.ConnectionManager.EndpointPort;
import org.flexiblepower.messaging.ConnectionManager.ManagedEndpoint;
import org.flexiblepower.messaging.ConnectionManager.PotentialConnection;
import org.osgi.framework.BundleContext;

public class BaseWidget {

    private final ConnectionManager connectionManager;
    private final BundleContext bunleContext;

    public BaseWidget(ConnectionManager connectionManager, BundleContext bunleContext) {
        this.connectionManager = connectionManager;
        this.bunleContext = bunleContext;
    }

    public ConnectionManagerState currentState() {
        ConnectionManagerState state = new ConnectionManagerState(connectionManager, bunleContext);
        state.performLayout();
        return state;
    }

    public void autoconnect() {
        connectionManager.autoConnect();
    }

    public boolean connect(ConnectionInfo info) {
        if (info.getSourceEndpoint() == null || info.getTargetEndpoint() == null) {
            return false;
        }

        ManagedEndpoint sourceEndpoint = connectionManager.getEndpoint(info.getSourceEndpoint());
        ManagedEndpoint targetEndpoint = connectionManager.getEndpoint(info.getTargetEndpoint());

        if (sourceEndpoint != null && targetEndpoint != null) {
            EndpointPort sourcePort = sourceEndpoint.getPort(info.getSourcePort());
            EndpointPort targetPort = targetEndpoint.getPort(info.getTargetPort());

            if (sourcePort != null && targetPort != null) {
                PotentialConnection connection = sourcePort.getPotentialConnection(targetPort);
                if (connection != null && connection.isConnectable()) {
                    connection.connect();
                    return true;
                }
            }
        }

        return false;
    }

    public boolean disconnect(ConnectionInfo info) {
        if (info.getSourceEndpoint() == null || info.getTargetEndpoint() == null) {
            return false;
        }

        ManagedEndpoint sourceEndpoint = connectionManager.getEndpoint(info.getSourceEndpoint());
        ManagedEndpoint targetEndpoint = connectionManager.getEndpoint(info.getTargetEndpoint());

        if (sourceEndpoint != null && targetEndpoint != null) {
            EndpointPort sourcePort = sourceEndpoint.getPort(info.getSourcePort());
            EndpointPort targetPort = targetEndpoint.getPort(info.getTargetPort());

            if (sourcePort != null && targetPort != null) {
                PotentialConnection connection = sourcePort.getPotentialConnection(targetPort);
                if (connection != null && connection.isConnected()) {
                    connection.disconnect();
                    return true;
                }
            }
        }

        return false;
    }<commit_msg>Added more checks to see if it is a valid connection<commit_after>package org.flexiblepower.runtime.ui.connectionspage;

import org.flexiblepower.messaging.ConnectionManager;
import org.flexiblepower.messaging.ConnectionManager.EndpointPort;
import org.flexiblepower.messaging.ConnectionManager.ManagedEndpoint;
import org.flexiblepower.messaging.ConnectionManager.PotentialConnection;
import org.osgi.framework.BundleContext;

public class BaseWidget {

    private final ConnectionManager connectionManager;
    private final BundleContext bunleContext;

    public BaseWidget(ConnectionManager connectionManager, BundleContext bunleContext) {
        this.connectionManager = connectionManager;
        this.bunleContext = bunleContext;
    }

    public ConnectionManagerState currentState() {
        ConnectionManagerState state = new ConnectionManagerState(connectionManager, bunleContext);
        state.performLayout();
        return state;
    }

    public void autoconnect() {
        connectionManager.autoConnect();
    }

    public boolean connect(ConnectionInfo info) {
        if (info == null || info.getSourceEndpoint() == null || info.getTargetEndpoint() == null) {
            return false;
        }

        ManagedEndpoint sourceEndpoint = connectionManager.getEndpoint(info.getSourceEndpoint());
        ManagedEndpoint targetEndpoint = connectionManager.getEndpoint(info.getTargetEndpoint());

        if (sourceEndpoint != null && targetEndpoint != null) {
            EndpointPort sourcePort = sourceEndpoint.getPort(info.getSourcePort());
            EndpointPort targetPort = targetEndpoint.getPort(info.getTargetPort());

            if (sourcePort != null && targetPort != null) {
                PotentialConnection connection = sourcePort.getPotentialConnection(targetPort);
                if (connection != null && connection.isConnectable()) {
                    connection.connect();
                    return true;
                }
            }
        }

        return false;
    }

    public boolean disconnect(ConnectionInfo info) {
        if (info == null || info.getSourceEndpoint() == null || info.getTargetEndpoint() == null) {
            return false;
        }

        ManagedEndpoint sourceEndpoint = connectionManager.getEndpoint(info.getSourceEndpoint());
        ManagedEndpoint targetEndpoint = connectionManager.getEndpoint(info.getTargetEndpoint());

        if (sourceEndpoint != null && targetEndpoint != null) {
            EndpointPort sourcePort = sourceEndpoint.getPort(info.getSourcePort());
            EndpointPort targetPort = targetEndpoint.getPort(info.getTargetPort());

            if (sourcePort != null && targetPort != null) {
                PotentialConnection connection = sourcePort.getPotentialConnection(targetPort);
                if (connection != null && connection.isConnected()) {
                    connection.disconnect();
                    return true;
                }
            }
        }

        return false;
    }
{'fraction_non_alphanumeric': 0.0873347833361218, 'fraction_numerical': 0.0, 'mean_word_length': 2.8345093008338678, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5401210', 'n_tokens_mistral': 1423, 'n_tokens_neox': 1327, 'n_words': 288}