// 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
public Period(Temporal temporal, Period period) throws TemporalException { this( temporal, period.getStartGranuleCount(period.getGranularity()), period.getFinishGranuleCount(period.getGranularity()), period.getGranularity()); } // Period
public Period merge(Period p2, int granularity) throws TemporalException { Instant resultStartInstant, resultFinishInstant; Period result; if ((intersection(p2, granularity) == null) && (!adjacent(p2, granularity))) throw new TemporalException( "start must be before or equal to the finish in a period: ('" + toString(granularity) + "'), ('" + p2.toString(granularity) + "')"); if (startInstant.before(p2.getStartInstant(), granularity)) resultStartInstant = startInstant; else resultStartInstant = p2.getStartInstant(); if (getFinishInstant().before(p2.getFinishInstant(), granularity)) resultFinishInstant = p2.getFinishInstant(); else resultFinishInstant = finishInstant; result = new Period(temporal, resultStartInstant, resultFinishInstant, granularity); return result; } // merge
public Period intersection(Period p2, int granularity) throws TemporalException { Period result; if (startInstant.equals(p2.getStartInstant(), granularity)) { // They must intersect if (finishInstant.after(p2.getFinishInstant(), granularity)) { result = new Period(temporal, startInstant, p2.getFinishInstant(), granularity); } else { result = new Period(temporal, startInstant, finishInstant, granularity); } // if } else if (startInstant.before( p2.getStartInstant(), granularity)) { // p2 starts after this Period if (finishInstant.after(p2.getStartInstant(), granularity)) { // They intersect if (finishInstant.before(p2.getFinishInstant(), granularity)) { result = new Period(temporal, p2.getStartInstant(), finishInstant, granularity); } else { result = new Period(temporal, p2.getStartInstant(), p2.getFinishInstant(), granularity); } // if } else { result = null; } // if } else { // p2 start before this Period if (p2.getFinishInstant().after(startInstant, granularity)) { // They intersect if (finishInstant.before(p2.getFinishInstant(), granularity)) { result = new Period(temporal, startInstant, finishInstant, granularity); } else { result = new Period(temporal, startInstant, p2.getFinishInstant(), granularity); } // if } else { result = null; } // if } // if return result; } // intersection
public boolean finished_by(Period p2, int granularity) throws TemporalException { return p2.finishes(this, granularity); } // finishes
public boolean finishes(Period p2, int granularity) throws TemporalException { return ((getStartGranuleCount(granularity) < p2.getStartGranuleCount(granularity)) && (getFinishGranuleCount(granularity) == p2.getFinishGranuleCount(granularity))); } // finishes
public boolean started_by(Period p2, int granularity) throws TemporalException { return p2.starts(this, granularity); } // started_by
public boolean contains(Period p2, int granularity) throws TemporalException { return (((p2.getStartGranuleCount(granularity) > getStartGranuleCount(granularity)) || (p2.getStartGranuleCount(granularity) == getStartGranuleCount(granularity))) && ((p2.getFinishGranuleCount(granularity) < getFinishGranuleCount(granularity)) || (p2.getFinishGranuleCount(granularity) == getFinishGranuleCount(granularity)))); } // contains
public boolean starts_before(Period p2, int granularity) throws TemporalException { return getStartGranuleCount(granularity) < p2.getStartGranuleCount(granularity); } // starts_before
public boolean overlapped_by(Period p2, int granularity) throws TemporalException { return p2.overlaps(this, granularity); } // overlapped_by
public boolean overlaps(Period p2, int granularity) throws TemporalException { return ((getStartGranuleCount(granularity) <= p2.getStartGranuleCount(granularity)) && (getFinishGranuleCount(granularity) <= p2.getFinishGranuleCount(granularity)) && (getFinishGranuleCount(granularity) >= p2.getStartGranuleCount(granularity))); } // overlaps
public boolean met_by(Period p2, int granularity) throws TemporalException { return p2.meets(this, granularity); } // met_by
public boolean meets(Period p2, int granularity) throws TemporalException { return ((getFinishGranuleCount(granularity) + 1) == p2.getStartGranuleCount(granularity)); } // meets
public boolean after(Period p2, int granularity) throws TemporalException { return getStartGranuleCount(granularity) > p2.getFinishGranuleCount(granularity); } // after
public boolean starts_after(Period p2) throws TemporalException { return getStartGranuleCount(Temporal.FINEST) > p2.getFinishGranuleCount(Temporal.FINEST); } // starts_after
public boolean during(Period p2, int granularity) throws TemporalException { return p2.contains(this, granularity) && !p2.equals(this, granularity); } // during
public boolean before(Period p2) throws TemporalException { return getFinishGranuleCount(Temporal.FINEST) < p2.getStartGranuleCount(Temporal.FINEST); } // before