View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.building;
20  
21  import java.io.File;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.util.Objects;
27  
28  /**
29   * Wraps an ordinary {@link File} as a source.
30   *
31   */
32  public class FileSource implements Source {
33      private final Path path;
34  
35      private final int hashCode;
36  
37      /**
38       * Creates a new source backed by the specified file.
39       *
40       * @param file The file, must not be {@code null}.
41       * @deprecated Use {@link #FileSource(Path)} instead.
42       */
43      @Deprecated
44      public FileSource(File file) {
45          this(Objects.requireNonNull(file, "file cannot be null").toPath());
46      }
47  
48      /**
49       * Creates a new source backed by the specified file.
50       *
51       * @param path The file, must not be {@code null}.
52       * @since 4.0.0
53       */
54      public FileSource(Path path) {
55          this.path = Objects.requireNonNull(path, "path cannot be null").toAbsolutePath();
56          this.hashCode = Objects.hash(path);
57      }
58  
59      @Override
60      public InputStream getInputStream() throws IOException {
61          return Files.newInputStream(path);
62      }
63  
64      @Override
65      public String getLocation() {
66          return path.toString();
67      }
68  
69      /**
70       * Gets the file of this source.
71       *
72       * @return The underlying file, never {@code null}.
73       * @deprecated Use {@link #getPath()} instead.
74       */
75      @Deprecated
76      public File getFile() {
77          return path.toFile();
78      }
79  
80      /**
81       * Gets the file of this source.
82       *
83       * @return The underlying file, never {@code null}.
84       * @since 4.0.0
85       */
86      public Path getPath() {
87          return path;
88      }
89  
90      @Override
91      public String toString() {
92          return getLocation();
93      }
94  
95      @Override
96      public int hashCode() {
97          return hashCode;
98      }
99  
100     @Override
101     public boolean equals(Object obj) {
102         if (this == obj) {
103             return true;
104         }
105 
106         if (obj == null) {
107             return false;
108         }
109 
110         if (!FileSource.class.equals(obj.getClass())) {
111             return false;
112         }
113 
114         FileSource other = (FileSource) obj;
115         return this.path.equals(other.path);
116     }
117 }