View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.io.file;
19  
20  import java.io.File;
21  import java.io.IOException;
22  import java.net.URI;
23  import java.nio.file.FileSystem;
24  import java.nio.file.Files;
25  import java.nio.file.LinkOption;
26  import java.nio.file.Path;
27  import java.nio.file.WatchEvent.Kind;
28  import java.nio.file.WatchEvent.Modifier;
29  import java.nio.file.WatchKey;
30  import java.nio.file.WatchService;
31  import java.util.Iterator;
32  import java.util.Objects;
33  import java.util.Spliterator;
34  import java.util.function.Consumer;
35  
36  /**
37   * Wraps and delegates to a Path for subclasses.
38   *
39   * @since 2.12.0
40   */
41  public abstract class AbstractPathWrapper implements Path {
42  
43      /**
44       * The path delegate.
45       */
46      private final Path path;
47  
48      /**
49       * Constructs a new instance.
50       *
51       * @param path The path to wrap.
52       */
53      protected AbstractPathWrapper(final Path path) {
54          this.path = Objects.requireNonNull(path, "path");
55      }
56  
57      @Override
58      public int compareTo(final Path other) {
59          return path.compareTo(other);
60      }
61  
62      @Override
63      public boolean endsWith(final Path other) {
64          return path.endsWith(other);
65      }
66  
67      @Override
68      public boolean endsWith(final String other) {
69          return path.endsWith(other);
70      }
71  
72      @Override
73      public boolean equals(final Object obj) {
74          if (this == obj) {
75              return true;
76          }
77          if (!(obj instanceof AbstractPathWrapper)) {
78              return false;
79          }
80          final AbstractPathWrapper other = (AbstractPathWrapper) obj;
81          return Objects.equals(path, other.path);
82      }
83  
84      /**
85       * Delegates to {@link Files#exists(Path, LinkOption...)}.
86       *
87       * @param options See {@link Files#exists(Path, LinkOption...)}.
88       * @return See {@link Files#exists(Path, LinkOption...)}.
89       */
90      public boolean exists(final LinkOption... options) {
91          return Files.exists(path, options);
92      }
93  
94      @Override
95      public void forEach(final Consumer<? super Path> action) {
96          path.forEach(action);
97      }
98  
99      /**
100      * Gets the delegate Path.
101      *
102      * @return the delegate Path.
103      */
104     public Path get() {
105         return path;
106     }
107 
108     @Override
109     public Path getFileName() {
110         return path.getFileName();
111     }
112 
113     @Override
114     public FileSystem getFileSystem() {
115         return path.getFileSystem();
116     }
117 
118     @Override
119     public Path getName(final int index) {
120         return path.getName(index);
121     }
122 
123     @Override
124     public int getNameCount() {
125         return path.getNameCount();
126     }
127 
128     @Override
129     public Path getParent() {
130         return path.getParent();
131     }
132 
133     @Override
134     public Path getRoot() {
135         return path.getRoot();
136     }
137 
138     @Override
139     public int hashCode() {
140         return Objects.hash(path);
141     }
142 
143     @Override
144     public boolean isAbsolute() {
145         return path.isAbsolute();
146     }
147 
148     @Override
149     public Iterator<Path> iterator() {
150         return path.iterator();
151     }
152 
153     @Override
154     public Path normalize() {
155         return path.normalize();
156     }
157 
158     @Override
159     public WatchKey register(final WatchService watcher, final Kind<?>... events) throws IOException {
160         return path.register(watcher, events);
161     }
162 
163     @Override
164     public WatchKey register(final WatchService watcher, final Kind<?>[] events, final Modifier... modifiers) throws IOException {
165         return path.register(watcher, events, modifiers);
166     }
167 
168     @Override
169     public Path relativize(final Path other) {
170         return path.relativize(other);
171     }
172 
173     @Override
174     public Path resolve(final Path other) {
175         return path.resolve(other);
176     }
177 
178     @Override
179     public Path resolve(final String other) {
180         return path.resolve(other);
181     }
182 
183     @Override
184     public Path resolveSibling(final Path other) {
185         return path.resolveSibling(other);
186     }
187 
188     @Override
189     public Path resolveSibling(final String other) {
190         return path.resolveSibling(other);
191     }
192 
193     @Override
194     public Spliterator<Path> spliterator() {
195         return path.spliterator();
196     }
197 
198     @Override
199     public boolean startsWith(final Path other) {
200         return path.startsWith(other);
201     }
202 
203     @Override
204     public boolean startsWith(final String other) {
205         return path.startsWith(other);
206     }
207 
208     @Override
209     public Path subpath(final int beginIndex, final int endIndex) {
210         return path.subpath(beginIndex, endIndex);
211     }
212 
213     @Override
214     public Path toAbsolutePath() {
215         return path.toAbsolutePath();
216     }
217 
218     @Override
219     public File toFile() {
220         return path.toFile();
221     }
222 
223     @Override
224     public Path toRealPath(final LinkOption... options) throws IOException {
225         return path.toRealPath(options);
226     }
227 
228     @Override
229     public String toString() {
230         return path.toString();
231     }
232 
233     @Override
234     public URI toUri() {
235         return path.toUri();
236     }
237 
238 }