784
785 /**
786 * Returns an iterator over the name elements of this path.
787 *
788 * <p> The first element returned by the iterator represents the name
789 * element that is closest to the root in the directory hierarchy, the
790 * second element is the next closest, and so on. The last element returned
791 * is the name of the file or directory denoted by this path. The {@link
792 * #getRoot root} component, if present, is not returned by the iterator.
793 *
794 * @implSpec
795 * The default implementation returns an {@code Iterator<Path>} which, for
796 * this path, traverses the {@code Path}s returned by
797 * {@code getName(index)}, where {@code index} ranges from zero to
798 * {@code getNameCount() - 1}, inclusive.
799 *
800 * @return an iterator over the name elements of this path.
801 */
802 @Override
803 default Iterator<Path> iterator() {
804 return new Iterator<Path>() {
805 private int i = 0;
806
807 @Override
808 public boolean hasNext() {
809 return (i < getNameCount());
810 }
811
812 @Override
813 public Path next() {
814 if (i < getNameCount()) {
815 Path result = getName(i);
816 i++;
817 return result;
818 } else {
819 throw new NoSuchElementException();
820 }
821 }
822 };
823 }
824
|
784
785 /**
786 * Returns an iterator over the name elements of this path.
787 *
788 * <p> The first element returned by the iterator represents the name
789 * element that is closest to the root in the directory hierarchy, the
790 * second element is the next closest, and so on. The last element returned
791 * is the name of the file or directory denoted by this path. The {@link
792 * #getRoot root} component, if present, is not returned by the iterator.
793 *
794 * @implSpec
795 * The default implementation returns an {@code Iterator<Path>} which, for
796 * this path, traverses the {@code Path}s returned by
797 * {@code getName(index)}, where {@code index} ranges from zero to
798 * {@code getNameCount() - 1}, inclusive.
799 *
800 * @return an iterator over the name elements of this path.
801 */
802 @Override
803 default Iterator<Path> iterator() {
804 return new Iterator<>() {
805 private int i = 0;
806
807 @Override
808 public boolean hasNext() {
809 return (i < getNameCount());
810 }
811
812 @Override
813 public Path next() {
814 if (i < getNameCount()) {
815 Path result = getName(i);
816 i++;
817 return result;
818 } else {
819 throw new NoSuchElementException();
820 }
821 }
822 };
823 }
824
|