boolean (final int index, final Collection<? extends Content> collection) { if ((collection == null)) { throw new NullPointerException( "Can not add a null collection to the ContentList"); } checkIndex(index, false); if (collection.isEmpty()) { // some collections are expensive to get the size of. // use isEmpty(). return false; } final int addcnt = collection.size(); if (addcnt == 1) { // quick check for single-add. add(index, collection.iterator().next()); return true; } ensureCapacity(size() + addcnt); final int tmpmodcount = getModCount(); final int tmpdmc = getDataModCount(); boolean ok = false; int count = 0; try { for (Content c : collection) { add(index + count, c); count++; } ok = true; } finally { if (!ok) { // something failed... remove all added content while (--count >= 0) { remove(index + count); } // restore the mod-counts. setModCount(tmpmodcount, tmpdmc); } } return true; }