<commit_before>package paidia;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ApplyView extends JPanel implements ValueView, ValueViewContainer {
    private JComponent functionView;
    private List<Argument> arguments;
    private ValueViewObserver observer;

    public ApplyView(JComponent functionView, List<JComponent> arguments) {
        addContainerListener(new ContainerAdapter() {
            ComponentAdapter componentAdapter;

            @Override
            public void componentAdded(ContainerEvent e) {
                componentAdapter = new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent e) {
                        setSize(getPreferredSize());
                    }
                };

                e.getChild().addComponentListener(componentAdapter);
            }

            @Override
            public void componentRemoved(ContainerEvent e) {
                e.getChild().removeComponentListener(componentAdapter);
            }
        });

        this.functionView = functionView;
        this.arguments = IntStream.range(0, arguments.size()).mapToObj(i -> {
            String name = ((ValueView)this.functionView).getIdentifiers().get(i);
            JComponent view = arguments.get(i);
            return createArgument(name, view);
        }).collect(Collectors.toList());

        this.arguments.forEach(x -> add(x));

        setSize(getPreferredSize());

        setBorder(BorderFactory.createDashedBorder(Color.GRAY, 4.0f, 4.0f));
    }

    private Argument createArgument(String name, JComponent view) {
        Argument argument = new Argument(name, view);

        argument.observer = new ValueViewObserver() {
            @Override
            public void updated() {
                observers.forEach(x -> x.updated());
            }
        };

        return argument;
    }

    private void update(PlaygroundView playgroundView) {
        List<String> addedParameters = ((ValueView)ApplyView.this.functionView).getIdentifiers().stream()
            .filter(x -> !ApplyView.this.arguments.stream().anyMatch(y -> y.name.equals(x)))
            .collect(Collectors.toList());

        List<String> removedParameters = ApplyView.this.arguments.stream().map(x -> x.name)
            .filter(x -> !((ValueView)ApplyView.this.functionView).getIdentifiers().contains(x))
            .collect(Collectors.toList());

        removedParameters.forEach(x -> {
            int index = IntStream.range(0, arguments.size()).filter(i -> arguments.get(i).name.equals(x)).findFirst().getAsInt();
            Argument argument = arguments.get(index);
            releaseArgument(argument);
            arguments.remove(index);
            remove(argument);

            setSize(getPreferredSize());
            revalidate();
            repaint();
        });

        addedParameters.forEach(x -> {
            int indexOfArgument = ((ValueView)ApplyView.this.functionView).getIdentifiers().indexOf(x);
            Argument argument = createArgument(x, playgroundView.createDefaultValueView());
            arguments.add(indexOfArgument, argument);
            add(argument, indexOfArgument);
            setupArgument(playgroundView, argument);

            setSize(getPreferredSize());
            revalidate();
            repaint();
        });

        observers.forEach(x -> x.updated());
    }

    @Override
    public String getText(TextContext textContext) {
        return IntStream.range(0, ((ValueView)this.functionView).getIdentifiers().size()).mapToObj(i -> {
            String n = ((ValueView)this.functionView).getIdentifiers().get(i);
            return n + " = " + ((ValueView)arguments.get(i).valueView).getText(textContext);
        }).collect(Collectors.joining(", ", "(", ")"));
    }

    @Override
    public void setText(String text) {

    }

    @Override
    public void setup(PlaygroundView playgroundView) {
        // playgroundView could be derived looking into the ancestry
        observer = new ValueViewObserver() {
            @Override
            public void updated() {
                update(playgroundView);
            }
        };

        ((ValueView)functionView).addObserver(observer);

        IntStream.range(0, arguments.size()).forEach(index -> {
            Argument x = arguments.get(index);
            setupArgument(playgroundView, x);
        });
    }

    private void setupArgument(PlaygroundView playgroundView, Argument x) {
        ((ValueView)x.valueView).addObserver(x.observer);
        x.editableView = playgroundView.createEditableView(new Editor() {
            @Override
            public String getText() {
                return ((ValueView)x.valueView).getText(new DefaultTextContext());
            }

            @Override
            public void beginEdit(JComponent editorComponent) {
                editorComponent.setPreferredSize(x.valueView.getPreferredSize());
                x.beginEdit(editorComponent);
                setSize(getPreferredSize());

                repaint();
                revalidate();
            }

            @Override
            public void endEdit(JComponent parsedComponent) {
                changeArgument(playgroundView, x, parsedComponent);
            }

            @Override
            public void cancelEdit() {
                x.cancelEdit();
                setSize(getPreferredSize());

                repaint();
                revalidate();
            }
        });
        playgroundView.makeEditableByMouse(x.valueView);
    }

    private void releaseArgument(Argument argument) {
        ((ValueView)argument.valueView).removeObserver(argument.observer);
        ((ValueView)argument.valueView).release();
    }

    private void changeArgument(PlaygroundView playgroundView, Argument argument, JComponent valueView) {
        argument.changeValue(valueView);
        releaseArgument(argument);
        argument.valueView = valueView;
        ((ValueView)argument.valueView).addObserver(argument.observer);

        playgroundView.makeEditableByMouse(argument.valueView);
        ((ValueView)argument.valueView).setup(playgroundView);

        setSize(getPreferredSize());

        repaint();
        revalidate();

        observers.forEach(x -> x.updated());
    }

    @Override
    public ValueView evaluate(Map<String, ValueView> environment) {
        // Ignore given arguments;
        // - Or, could see arguments as a dynamic scope?
        Map<String, ValueView> applyEnvironment = IntStream.range(0, this.arguments.size()).mapToObj(i -> i)
            .collect(Collectors.toMap(
                i -> ((ValueView)this.functionView).getIdentifiers().get(i.intValue()),
                i -> ((ValueView)this.arguments.get(i.intValue()).valueView).evaluate(environment)));

        return ((ValueView)this.functionView).evaluate(applyEnvironment);
    }

    private ArrayList<ValueViewObserver> observers = new ArrayList<>();

    @Override
    public void addObserver(ValueViewObserver observer) {
        observers.add(observer);
    }<commit_msg>Removed redundant comments.<commit_after>package paidia;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class ApplyView extends JPanel implements ValueView, ValueViewContainer {
    private JComponent functionView;
    private List<Argument> arguments;
    private ValueViewObserver observer;

    public ApplyView(JComponent functionView, List<JComponent> arguments) {
        addContainerListener(new ContainerAdapter() {
            ComponentAdapter componentAdapter;

            @Override
            public void componentAdded(ContainerEvent e) {
                componentAdapter = new ComponentAdapter() {
                    @Override
                    public void componentResized(ComponentEvent e) {
                        setSize(getPreferredSize());
                    }
                };

                e.getChild().addComponentListener(componentAdapter);
            }

            @Override
            public void componentRemoved(ContainerEvent e) {
                e.getChild().removeComponentListener(componentAdapter);
            }
        });

        this.functionView = functionView;
        this.arguments = IntStream.range(0, arguments.size()).mapToObj(i -> {
            String name = ((ValueView)this.functionView).getIdentifiers().get(i);
            JComponent view = arguments.get(i);
            return createArgument(name, view);
        }).collect(Collectors.toList());

        this.arguments.forEach(x -> add(x));

        setSize(getPreferredSize());

        setBorder(BorderFactory.createDashedBorder(Color.GRAY, 4.0f, 4.0f));
    }

    private Argument createArgument(String name, JComponent view) {
        Argument argument = new Argument(name, view);

        argument.observer = new ValueViewObserver() {
            @Override
            public void updated() {
                observers.forEach(x -> x.updated());
            }
        };

        return argument;
    }

    private void update(PlaygroundView playgroundView) {
        List<String> addedParameters = ((ValueView)ApplyView.this.functionView).getIdentifiers().stream()
            .filter(x -> !ApplyView.this.arguments.stream().anyMatch(y -> y.name.equals(x)))
            .collect(Collectors.toList());

        List<String> removedParameters = ApplyView.this.arguments.stream().map(x -> x.name)
            .filter(x -> !((ValueView)ApplyView.this.functionView).getIdentifiers().contains(x))
            .collect(Collectors.toList());

        removedParameters.forEach(x -> {
            int index = IntStream.range(0, arguments.size()).filter(i -> arguments.get(i).name.equals(x)).findFirst().getAsInt();
            Argument argument = arguments.get(index);
            releaseArgument(argument);
            arguments.remove(index);
            remove(argument);

            setSize(getPreferredSize());
            revalidate();
            repaint();
        });

        addedParameters.forEach(x -> {
            int indexOfArgument = ((ValueView)ApplyView.this.functionView).getIdentifiers().indexOf(x);
            Argument argument = createArgument(x, playgroundView.createDefaultValueView());
            arguments.add(indexOfArgument, argument);
            add(argument, indexOfArgument);
            setupArgument(playgroundView, argument);

            setSize(getPreferredSize());
            revalidate();
            repaint();
        });

        observers.forEach(x -> x.updated());
    }

    @Override
    public String getText(TextContext textContext) {
        return IntStream.range(0, ((ValueView)this.functionView).getIdentifiers().size()).mapToObj(i -> {
            String n = ((ValueView)this.functionView).getIdentifiers().get(i);
            return n + " = " + ((ValueView)arguments.get(i).valueView).getText(textContext);
        }).collect(Collectors.joining(", ", "(", ")"));
    }

    @Override
    public void setText(String text) {

    }

    @Override
    public void setup(PlaygroundView playgroundView) {
        // playgroundView could be derived looking into the ancestry
        observer = new ValueViewObserver() {
            @Override
            public void updated() {
                update(playgroundView);
            }
        };

        ((ValueView)functionView).addObserver(observer);

        IntStream.range(0, arguments.size()).forEach(index -> {
            Argument x = arguments.get(index);
            setupArgument(playgroundView, x);
        });
    }

    private void setupArgument(PlaygroundView playgroundView, Argument x) {
        ((ValueView)x.valueView).addObserver(x.observer);
        x.editableView = playgroundView.createEditableView(new Editor() {
            @Override
            public String getText() {
                return ((ValueView)x.valueView).getText(new DefaultTextContext());
            }

            @Override
            public void beginEdit(JComponent editorComponent) {
                editorComponent.setPreferredSize(x.valueView.getPreferredSize());
                x.beginEdit(editorComponent);
                setSize(getPreferredSize());

                repaint();
                revalidate();
            }

            @Override
            public void endEdit(JComponent parsedComponent) {
                changeArgument(playgroundView, x, parsedComponent);
            }

            @Override
            public void cancelEdit() {
                x.cancelEdit();
                setSize(getPreferredSize());

                repaint();
                revalidate();
            }
        });
        playgroundView.makeEditableByMouse(x.valueView);
    }

    private void releaseArgument(Argument argument) {
        ((ValueView)argument.valueView).removeObserver(argument.observer);
        ((ValueView)argument.valueView).release();
    }

    private void changeArgument(PlaygroundView playgroundView, Argument argument, JComponent valueView) {
        argument.changeValue(valueView);
        releaseArgument(argument);
        argument.valueView = valueView;
        ((ValueView)argument.valueView).addObserver(argument.observer);

        playgroundView.makeEditableByMouse(argument.valueView);
        ((ValueView)argument.valueView).setup(playgroundView);

        setSize(getPreferredSize());

        repaint();
        revalidate();

        observers.forEach(x -> x.updated());
    }

    @Override
    public ValueView evaluate(Map<String, ValueView> environment) {
        Map<String, ValueView> applyEnvironment = IntStream.range(0, this.arguments.size()).mapToObj(i -> i)
            .collect(Collectors.toMap(
                i -> ((ValueView)this.functionView).getIdentifiers().get(i.intValue()),
                i -> ((ValueView)this.arguments.get(i.intValue()).valueView).evaluate(environment)));

        return ((ValueView)this.functionView).evaluate(applyEnvironment);
    }

    private ArrayList<ValueViewObserver> observers = new ArrayList<>();

    @Override
    public void addObserver(ValueViewObserver observer) {
        observers.add(observer);
    }
{'fraction_non_alphanumeric': 0.11369807222373066, 'fraction_numerical': 0.0012218300298669563, 'mean_word_length': 2.381455129676383, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 53, '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': '800917', 'n_tokens_mistral': 3754, 'n_tokens_neox': 3598, 'n_words': 643}