// This routine modifies the periods list that it has been passed so this list should not be used // again. public List<Period> coalesce(List<Period> periods, int granularity) throws TemporalException { Iterator<Period> iterator; Period p1, p2; boolean periodMerged; List<Period> resultList = new ArrayList<Period>(); // Loop through each period in the list trying to merge with other periods. while (!periods.isEmpty()) { p1 = (Period) periods.get(0); periods.remove(0); // Remove each period as we deal with it. // See if we can merge this period with the remaining periods in the list. If we merge this // period with an existing period we must // then scan through the remainder of the period list again from the beginning to see if this // new merged period can be merged with // other periods and so on.String do { periodMerged = false; iterator = periods.iterator(); while (!periodMerged && iterator.hasNext()) { p2 = (Period) iterator.next(); // Merge contiguous or overlapping periods. if ((p1.intersects(p2, granularity)) || (p1.adjacent(p2, granularity))) { p1 = p1.merge(p2, granularity); iterator.remove(); // We have merged with period p2 - remove it. periodMerged = true; } // if } // while } while (periodMerged); resultList.add(p1); } // while return resultList; } // coalesce