Looking for complete code snippets to read through and try to understand. Post them below and I'll post back with what I think it does.

Do you mean stuff like this (class name changed, comments deleted) to make it less obvious)

public class QQQ<E> extends Vector<E> {

    private static final long serialVersionUID = -1859832140413652455L;

    private Comparator<E> c;

    public QQQ(Comparator<E> c) {
        this.c = c;
    }

    public QQQ() { // no comparator: behaves like Vector
        c = null;
    } 

    @Override
    public boolean add(E o) {
        if  (c == null) {
            super.add(o);
            return true;
        }
        int i;
        for (i = 0; i<size(); i++ ) {
            if (c.compare(o, elementAt(i)) <= 0) break;
        }
        insertElementAt(o, i);
        // System.out.println("Inserting " + o + " at " + i);
        return true;
    }

    public void reSort() {
        if  (c == null) return;  
        Vector<E> temp = new Vector<E>(this);
        this.clear();
        for (E o : temp) add(o);
    }
}

As far as I can tell, it's a sortable List of Vectors of type E.

The constructor can take in a Comparable object which is used in the add method.

The add method first checks to see if the Comparable object is null, in which case it calls Vector's add method and passes this add's parameter to the Vector's add parameter.

Otherwise if the Comparable object is not null, you iterate over the Vector's List using the Comparable object to check for the first index where the value of Object o and the Element at Index i is less than or equal to 0.

You then use that Index to insert Object o into the Vector's List.

The reSort method passes the Vector's List into a temporary List, then it gets cleared.
Then you iterate over each Object in the Vector's List using a for each loop, adding each Object to the Vector's List, therefore ensuring the List is sorted.

This code is extremely neat and the use of Comparable, from what I understand of it, can sort the List in pretty much any way. All in all, very nice code. If you'd like to post the code with the comments and full names, I would really like to know how accurate I was!

public class SortedVector<E> extends Vector<E> {

    // Quickie convenience class for a Vector in a defined sort order.
    // Performance should scale linearly with size.
    //
    // The constructor for this class requires a Comparator class to
    // define the sort order.  
    // Without a Comparator this works like a normal Vector.
    //
    // The overridden add(object) method inserts the new object at the 
    // right place in the Vector according to the sort order defined in 
    //  the constructor. Without a Comparator it adds like Vector does.
    //
    // Using other methods from Vector to add or insert Objects is not
    // prohibited (lazyness on my part), but the results are undefined.
    //
    // Using methods from Vector to remove Objects is OK, as are all
    // the iterator and all other methods that do not change the
    // contents of the Vector.
    //
    // The new methods are NOT Thread safe.


    private static final long serialVersionUID = -1859832140413652455L;

    private Comparator<E> c;

    public SortedVector(Comparator<E> c) {
        this.c = c;
    }

    public SortedVector() { // no comparator: behaves like Vector
        c = null;
    } 

    @Override
    public boolean add(E o) {
        if  (c == null) {
            super.add(o);
            return true;
        }
        int i;
        for (i = 0; i<size(); i++ ) {
            if (c.compare(o, elementAt(i)) <= 0) break;
        }
        insertElementAt(o, i);
        // System.out.println("Inserting " + o + " at " + i);
        return true;
    }

    public void reSort() {
        if  (c == null) return;  // no sort order,so this is a no-op
        // force back into correct sequence after something changed...
        Vector<E> temp = new Vector<E>(this);
        this.clear();
        for (E o : temp) add(o);
    }
}

Here's a Swing-based one...

public class QQQ
    implements java.awt.event.ActionListener {

  private Preferences prefs;

  private JComponent parent;
  private String prefsItemName;
  private Runnable changeCallback = null;
  private ButtonGroup group = new ButtonGroup();
  private HashMap<JRadioButton, String> map = new HashMap<JRadioButton, String>();

  public QQQ(JComponent parent, Preferences prefs, String prefsItemName) {
    this.parent = parent;
    this.prefs = prefs;
    this.prefsItemName = prefsItemName;
  }

  public QQQ(JComponent parent,  Preferences prefs, String prefsItemName,
      Runnable changeCallback) {
    this.parent = parent;
    this.prefs = prefs;
    this.prefsItemName = prefsItemName;
    this.changeCallback = changeCallback;
  }

  public JRadioButton add(String text, String prefsItemValue) {
    // returns button in case you want to set a mnemonic or whatever
    JRadioButton b = new JRadioButton(text);
    b.addActionListener(this);
    map.put(b, prefsItemValue);
    group.add(b);
    parent.add(b);
    if (prefs.get(prefsItemName, "").equals(prefsItemValue)) {
      b.setSelected(true);
    }
    return b;
  }

  public void actionPerformed(ActionEvent e) {
    String prefsItemValue = map.get(e.getSource());
    // System.out.println("Selected " + prefsItemName + " = " + prefsItemValue);
    prefs.put(prefsItemName, prefsItemValue);
    if (changeCallback != null) {
      changeCallback.run();
    }
  }



}

Edited 1 Year Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.