@Override
	public void add(int index, ST segment) {
		if (index < 0 || index > this.segmentList.size()) {
			throw new IndexOutOfBoundsException();
		}
		// The path is empty, so that the segment is the first one.
		if (this.segmentList.isEmpty()) {
			if (this.segmentList.add(segment)) {
				this.startingPoint = segment.getBeginPoint();
				this.endingPoint = segment.getEndPoint();
				this.length += segment.getLength();
				return;
			}
			throw new IllegalArgumentException();
		}

		// Detect the candidates to the connection
		final PT candidate;

		this.isReversable = false;

		if (index < this.segmentList.size()) {
			candidate = getStartingPointFor(index);
		} else {
			candidate = this.endingPoint;
		}

		// Check the connection validity
		if (candidate != null) {
			final PT first = segment.getBeginPoint();
			final PT last = segment.getEndPoint();
			if (index == 0 && (candidate.equals(first) || candidate.equals(last))) {
				this.segmentList.add(0, segment);
				this.startingPoint = segment.getOtherSidePoint(this.startingPoint);
				this.length += segment.getLength();
				return;
			} else if (index == this.segmentList.size() && (candidate.equals(first) || candidate.equals(last))) {
				this.segmentList.add(segment);
				this.endingPoint = segment.getOtherSidePoint(this.endingPoint);
				this.length += segment.getLength();
				return;
			} else if (candidate.equals(first) || candidate.equals(last)) {
				this.segmentList.add(index, segment);
				this.length += segment.getLength();
				return;
			}
		}

		throw new IllegalArgumentException();
	}
@ Override public void add ( int index , ST segment ) { if ( index < 0 || index > this . segmentList . size ( ) ) { throw new IndexOutOfBoundsException ( ) ; } if ( this . segmentList . isEmpty ( ) ) { if ( this . segmentList . add ( segment ) ) { this . startingPoint = segment . getBeginPoint ( ) ; this . endingPoint = segment . getEndPoint ( ) ; this . length += segment . getLength ( ) ; return ; } throw new IllegalArgumentException ( ) ; } final PT candidate ; this . isReversable = false ; if ( index < this . segmentList . size ( ) ) { candidate = getStartingPointFor ( index ) ; } else { candidate = this . endingPoint ; } if ( candidate != null ) { final PT first = segment . getBeginPoint ( ) ; final PT last = segment . getEndPoint ( ) ; if ( index == 0 && ( candidate . equals ( first ) || candidate . equals ( last ) ) ) { this . segmentList . add ( 0 , segment ) ; this . startingPoint = segment . getOtherSidePoint ( this . startingPoint ) ; this . length += segment . getLength ( ) ; return ; } else if ( index == this . segmentList . size ( ) && ( candidate . equals ( first ) || candidate . equals ( last ) ) ) { this . segmentList . add ( segment ) ; this . endingPoint = segment . getOtherSidePoint ( this . endingPoint ) ; this . length += segment . getLength ( ) ; return ; } else if ( candidate . equals ( first ) || candidate . equals ( last ) ) { this . segmentList . add ( index , segment ) ; this . length += segment . getLength ( ) ; return ; } } throw new IllegalArgumentException ( ) ; }
{@inheritDoc}

@throws IndexOutOfBoundsException if the index is invalid.
@throws IllegalArgumentException is the given segment cannot be connected.