<commit_before>package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
import java.util.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.*;
import javax.swing.tree.*;

public final class MainPanel extends JPanel {
    private final DefaultMutableTreeNode root = TreeUtil.makeTreeRoot();
    private final JTree tree = new JTree(new DefaultTreeModel(TreeUtil.makeTreeRoot()));
    private final JCheckBox sort0 = new JCheckBox("0: bubble sort");
    private final JCheckBox sort1 = new JCheckBox("1: bubble sort");
    private final JCheckBox sort2 = new JCheckBox("2: selection sort");
    private final JCheckBox sort3 = new JCheckBox("3: iterative merge sort");

    public MainPanel() {
        super(new BorderLayout());
        JPanel box = new JPanel(new GridLayout(2, 2));
        ActionListener sortActionListener = new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                JCheckBox check = (JCheckBox) e.getSource();
                if (check.isSelected()) {
                    TreeUtil.compareCount.set(0);
                    TreeUtil.swapCount.set(0);
                    DefaultMutableTreeNode r = TreeUtil.deepCopyTree(root, (DefaultMutableTreeNode) root.clone());
                    if (check.equals(sort0)) {
                        TreeUtil.sortTree0(r);
                    } else if (check.equals(sort1)) {
                        TreeUtil.sortTree1(r);
                    } else if (check.equals(sort2)) {
                        TreeUtil.sortTree2(r);
                    } else {
                        TreeUtil.sortTree3(r);
                    }
                    log(check.getText());
                    tree.setModel(new DefaultTreeModel(r));
                } else {
                    tree.setModel(new DefaultTreeModel(root));
                }
                TreeUtil.expandAll(tree);
            }
        };
        for (JCheckBox check: Arrays.asList(sort0, sort1, sort2, sort3)) {
            box.add(check);
            check.addActionListener(sortActionListener);
        }
        add(box, BorderLayout.SOUTH);

        JPanel p = new JPanel(new BorderLayout());
        p.setBorder(BorderFactory.createTitledBorder("Sort JTree"));
        p.add(new JScrollPane(tree));
        add(p);
        TreeUtil.expandAll(tree);
        setPreferredSize(new Dimension(320, 240));
    }

    private static void log(String title) {
        if (TreeUtil.swapCount.get() == 0) {
            System.out.format("%-24s - compare: %3d, swap: ---%n",
                              title, TreeUtil.compareCount.get());
        } else {
            System.out.format("%-24s - compare: %3d, swap: %3d%n",
                              title, TreeUtil.compareCount.get(), TreeUtil.swapCount.get());
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException |
                 IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

final class TreeUtil {
    private static final boolean DEBUG = true;
    public static AtomicInteger compareCount = new AtomicInteger();
    public static AtomicInteger swapCount = new AtomicInteger();

    //JDK 1.7.0
    private static TreeNodeComparator tnc = new TreeNodeComparator();
    private static class TreeNodeComparator implements Comparator<DefaultMutableTreeNode>, Serializable {
        private static final long serialVersionUID = 1L;
        @Override public int compare(DefaultMutableTreeNode a, DefaultMutableTreeNode b) {
            compareCount.getAndIncrement();
            if (a.isLeaf() && !b.isLeaf()) {
                return 1;
            } else if (!a.isLeaf() && b.isLeaf()) {
                return -1;
            } else {
                String sa = a.getUserObject().toString();
                String sb = b.getUserObject().toString();
                return sa.compareToIgnoreCase(sb);
            }
        }
    }
//     //JDK 1.8.0
//     private static TreeNodeComparator tnc = Comparator.comparing(DefaultMutableTreeNode::isLeaf)
//                                                       .thenComparing(DefaultMutableTreeNode::getUserObject().toString());

    private TreeUtil() { /* Singleton */ }

    // https://forums.oracle.com/thread/1355435 How to sort jTree Nodes
    public static void sortTree0(DefaultMutableTreeNode root) {
        for (int i = 0; i < root.getChildCount(); i++) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
            for (int j = 0; j < i; j++) {
                DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j);
                compareCount.getAndIncrement();
                if (tnc.compare(node, prevNode) < 0) {
                    root.insert(node, j);
                    root.insert(prevNode, i);
                    swapCount.getAndIncrement();
                    if (DEBUG) {
                        i--;
                        break;<commit_msg>comment: a lambda expression Comparator<commit_after>package example;
//-*- mode:java; encoding:utf-8 -*-
// vim:set fileencoding=utf-8:
//@homepage@
import java.awt.*;
import java.awt.event.*;
import java.io.Serializable;
import java.util.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import javax.swing.*;
import javax.swing.tree.*;

public final class MainPanel extends JPanel {
    private final DefaultMutableTreeNode root = TreeUtil.makeTreeRoot();
    private final JTree tree = new JTree(new DefaultTreeModel(TreeUtil.makeTreeRoot()));
    private final JCheckBox sort0 = new JCheckBox("0: bubble sort");
    private final JCheckBox sort1 = new JCheckBox("1: bubble sort");
    private final JCheckBox sort2 = new JCheckBox("2: selection sort");
    private final JCheckBox sort3 = new JCheckBox("3: iterative merge sort");

    public MainPanel() {
        super(new BorderLayout());
        JPanel box = new JPanel(new GridLayout(2, 2));
        ActionListener sortActionListener = new ActionListener() {
            @Override public void actionPerformed(ActionEvent e) {
                JCheckBox check = (JCheckBox) e.getSource();
                if (check.isSelected()) {
                    TreeUtil.compareCount.set(0);
                    TreeUtil.swapCount.set(0);
                    DefaultMutableTreeNode r = TreeUtil.deepCopyTree(root, (DefaultMutableTreeNode) root.clone());
                    if (check.equals(sort0)) {
                        TreeUtil.sortTree0(r);
                    } else if (check.equals(sort1)) {
                        TreeUtil.sortTree1(r);
                    } else if (check.equals(sort2)) {
                        TreeUtil.sortTree2(r);
                    } else {
                        TreeUtil.sortTree3(r);
                    }
                    log(check.getText());
                    tree.setModel(new DefaultTreeModel(r));
                } else {
                    tree.setModel(new DefaultTreeModel(root));
                }
                TreeUtil.expandAll(tree);
            }
        };
        for (JCheckBox check: Arrays.asList(sort0, sort1, sort2, sort3)) {
            box.add(check);
            check.addActionListener(sortActionListener);
        }
        add(box, BorderLayout.SOUTH);

        JPanel p = new JPanel(new BorderLayout());
        p.setBorder(BorderFactory.createTitledBorder("Sort JTree"));
        p.add(new JScrollPane(tree));
        add(p);
        TreeUtil.expandAll(tree);
        setPreferredSize(new Dimension(320, 240));
    }

    private static void log(String title) {
        if (TreeUtil.swapCount.get() == 0) {
            System.out.format("%-24s - compare: %3d, swap: ---%n",
                              title, TreeUtil.compareCount.get());
        } else {
            System.out.format("%-24s - compare: %3d, swap: %3d%n",
                              title, TreeUtil.compareCount.get(), TreeUtil.swapCount.get());
        }
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override public void run() {
                createAndShowGUI();
            }
        });
    }
    public static void createAndShowGUI() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException | InstantiationException |
                 IllegalAccessException | UnsupportedLookAndFeelException ex) {
            ex.printStackTrace();
        }
        JFrame frame = new JFrame("@title@");
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.getContentPane().add(new MainPanel());
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}

final class TreeUtil {
    private static final boolean DEBUG = true;
    public static AtomicInteger compareCount = new AtomicInteger();
    public static AtomicInteger swapCount = new AtomicInteger();

//* //JDK 1.7.0
    private static TreeNodeComparator tnc = new TreeNodeComparator();
    private static class TreeNodeComparator implements Comparator<DefaultMutableTreeNode>, Serializable {
        private static final long serialVersionUID = 1L;
        @Override public int compare(DefaultMutableTreeNode a, DefaultMutableTreeNode b) {
            compareCount.getAndIncrement();
            if (a.isLeaf() && !b.isLeaf()) {
                return 1;
            } else if (!a.isLeaf() && b.isLeaf()) {
                return -1;
            } else {
                String sa = a.getUserObject().toString();
                String sb = b.getUserObject().toString();
                return sa.compareToIgnoreCase(sb);
            }
        }
    }
/*/ //JDK 1.8.0
    private static Comparator<DefaultMutableTreeNode> tnc = Comparator.comparing(DefaultMutableTreeNode::isLeaf)
                                                                      .thenComparing(n -> n.getUserObject().toString());
//*/

    private TreeUtil() { /* Singleton */ }

    // https://forums.oracle.com/thread/1355435 How to sort jTree Nodes
    public static void sortTree0(DefaultMutableTreeNode root) {
        for (int i = 0; i < root.getChildCount(); i++) {
            DefaultMutableTreeNode node = (DefaultMutableTreeNode) root.getChildAt(i);
            for (int j = 0; j < i; j++) {
                DefaultMutableTreeNode prevNode = (DefaultMutableTreeNode) root.getChildAt(j);
                compareCount.getAndIncrement();
                if (tnc.compare(node, prevNode) < 0) {
                    root.insert(node, j);
                    root.insert(prevNode, i);
                    swapCount.getAndIncrement();
                    if (DEBUG) {
                        i--;
                        break;
{'fraction_non_alphanumeric': 0.11048672948906371, 'fraction_numerical': 0.010201435117143599, 'mean_word_length': 1.964633521271143, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2752129', 'n_tokens_mistral': 3187, 'n_tokens_neox': 3027, 'n_words': 680}