private static boolean compare(GeoElement geo1, GeoElement geo2, SortMode mode) { switch (mode) { case ORDER: return geo1.getConstructionIndex() > geo2.getConstructionIndex(); default: // alphabetical return GeoElement.compareLabels(geo1.getLabel(), geo2.getLabel()) > 0; } }
/** * Performs a binary search for geo among the children of parent. All children of parent have to * be instances of GeoElement sorted alphabetically by their names. * * @return -1 when not found */ public static final int binarySearchGeo(DefaultMutableTreeNode parent, String geoLabel) { int left = 0; int right = parent.getChildCount() - 1; if (right == -1) return -1; // binary search for geo's label while (left <= right) { int middle = (left + right) / 2; DefaultMutableTreeNode node = (DefaultMutableTreeNode) parent.getChildAt(middle); String nodeLabel = ((GeoElement) node.getUserObject()).getLabel(); int compare = GeoElement.compareLabels(geoLabel, nodeLabel); if (compare < 0) right = middle - 1; else if (compare > 0) left = middle + 1; else return middle; } return -1; }