}
/*
- * traverse existing hidden ranges and insert / amend / append as
- * appropriate
+ * new range follows everything else; check first to avoid looping over whole hiddenColumns collection
*/
- for (int i = 0; i < hiddenColumns.size(); i++)
+ if (hiddenColumns.isEmpty()
+ || start > hiddenColumns.get(hiddenColumns.size() - 1)[1])
{
- int[] region = hiddenColumns.get(i);
-
- if (end < region[0] - 1)
- {
- /*
- * insert discontiguous preceding range
- */
- hiddenColumns.add(i, new int[] { start, end });
- return;
- }
-
- if (end <= region[1])
+ hiddenColumns.add(new int[] { start, end });
+ }
+ else
+ {
+ /*
+ * traverse existing hidden ranges and insert / amend / append as
+ * appropriate
+ */
+ boolean added = false;
+ for (int i = 0; !added && i < hiddenColumns.size(); i++)
{
- /*
- * new range overlaps existing, or is contiguous preceding it - adjust
- * start column
- */
- region[0] = Math.min(region[0], start);
- return;
- }
+ int[] region = hiddenColumns.get(i);
- if (start <= region[1] + 1)
- {
- /*
- * new range overlaps existing, or is contiguous following it - adjust
- * start and end columns
- */
- region[0] = Math.min(region[0], start);
- region[1] = Math.max(region[1], end);
-
- /*
- * also update or remove any subsequent ranges
- * that are overlapped
- */
- while (i < hiddenColumns.size() - 1)
+ if (end < region[0] - 1)
{
- int[] nextRegion = hiddenColumns.get(i + 1);
- if (nextRegion[0] > end + 1)
+ /*
+ * insert discontiguous preceding range
+ */
+ hiddenColumns.add(i, new int[] { start, end });
+ added = true;
+ }
+ else if (end <= region[1])
+ {
+ /*
+ * new range overlaps existing, or is contiguous preceding it - adjust
+ * start column
+ */
+ region[0] = Math.min(region[0], start);
+ added = true;
+ }
+ else if (start <= region[1] + 1)
+ {
+ /*
+ * new range overlaps existing, or is contiguous following it - adjust
+ * start and end columns
+ */
+ region[0] = Math.min(region[0], start);
+ region[1] = Math.max(region[1], end);
+
+ /*
+ * also update or remove any subsequent ranges
+ * that are overlapped
+ */
+ while (i < hiddenColumns.size() - 1)
{
- /*
- * gap to next hidden range - no more to update
- */
- break;
+ int[] nextRegion = hiddenColumns.get(i + 1);
+ if (nextRegion[0] > end + 1)
+ {
+ /*
+ * gap to next hidden range - no more to update
+ */
+ break;
+ }
+ region[1] = Math.max(nextRegion[1], end);
+ hiddenColumns.remove(i + 1);
}
- region[1] = Math.max(nextRegion[1], end);
- hiddenColumns.remove(i + 1);
+ added = true;
}
- return;
- }
+ } // for
}
-
- /*
- * remaining case is that the new range follows everything else
- */
- hiddenColumns.add(new int[] { start, end });
} finally
{
if (!wasAlreadyLocked)