Exemplo n.º 1
0
  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
Exemplo n.º 2
0
  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