1 /*
   2  * Copyright (c) 2007, 2011, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package sun.nio.fs;
  27 
  28 import java.nio.file.*;
  29 import java.io.File;
  30 import java.io.IOException;
  31 import java.util.Iterator;
  32 import java.util.NoSuchElementException;
  33 
  34 /**
  35  * Base implementation class of {@code Path}.
  36  */
  37 
  38 abstract class AbstractPath implements Path {
  39     protected AbstractPath() { }
  40 
  41     @Override
  42     public final boolean startsWith(String other) {
  43         return startsWith(getFileSystem().getPath(other));
  44     }
  45 
  46     @Override
  47     public final boolean endsWith(String other) {
  48         return endsWith(getFileSystem().getPath(other));
  49     }
  50 
  51     @Override
  52     public final Path resolve(String other) {
  53         return resolve(getFileSystem().getPath(other));
  54     }
  55 
  56     @Override
  57     public final Path resolveSibling(Path other) {
  58         if (other == null)
  59             throw new NullPointerException();
  60         Path parent = getParent();
  61         return (parent == null) ? other : parent.resolve(other);
  62     }
  63 
  64     @Override
  65     public final Path resolveSibling(String other) {
  66         return resolveSibling(getFileSystem().getPath(other));
  67     }
  68 
  69     @Override
  70     public final Iterator<Path> iterator() {
  71         return new Iterator<Path>() {
  72             private int i = 0;
  73             @Override
  74             public boolean hasNext() {
  75                 return (i < getNameCount());
  76             }
  77             @Override
  78             public Path next() {
  79                 if (i < getNameCount()) {
  80                     Path result = getName(i);
  81                     i++;
  82                     return result;
  83                 } else {
  84                     throw new NoSuchElementException();
  85                 }
  86             }
  87             @Override
  88             public void remove() {
  89                 throw new UnsupportedOperationException();
  90             }
  91         };
  92     }
  93 
  94     @Override
  95     public final File toFile() {
  96         return new File(toString());
  97     }
  98 
  99     @Override
 100     public final WatchKey register(WatchService watcher,
 101                                    WatchEvent.Kind<?>... events)
 102         throws IOException
 103     {
 104         return register(watcher, events, new WatchEvent.Modifier[0]);
 105     }
 106 }