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 * @deprecated since 4.0.0, use {@link org.apache.maven.api.services} instead
32 */
33 @Deprecated(since = "4.0.0")
34 public class FileSource implements Source {
35 private final Path path;
36
37 private final int hashCode;
38
39 /**
40 * Creates a new source backed by the specified file.
41 *
42 * @param file The file, must not be {@code null}.
43 * @deprecated Use {@link #FileSource(Path)} instead.
44 */
45 @Deprecated
46 public FileSource(File file) {
47 this(Objects.requireNonNull(file, "file cannot be null").toPath());
48 }
49
50 /**
51 * Creates a new source backed by the specified file.
52 *
53 * @param path The file, must not be {@code null}.
54 * @since 4.0.0
55 */
56 public FileSource(Path path) {
57 this.path = Objects.requireNonNull(path, "path cannot be null").toAbsolutePath();
58 this.hashCode = Objects.hash(path);
59 }
60
61 @Override
62 public InputStream getInputStream() throws IOException {
63 return Files.newInputStream(path);
64 }
65
66 @Override
67 public String getLocation() {
68 return path.toString();
69 }
70
71 /**
72 * Gets the file of this source.
73 *
74 * @return The underlying file, never {@code null}.
75 * @deprecated Use {@link #getPath()} instead.
76 */
77 @Deprecated
78 public File getFile() {
79 return path.toFile();
80 }
81
82 /**
83 * Gets the file of this source.
84 *
85 * @return The underlying file, never {@code null}.
86 * @since 4.0.0
87 */
88 public Path getPath() {
89 return path;
90 }
91
92 @Override
93 public String toString() {
94 return getLocation();
95 }
96
97 @Override
98 public int hashCode() {
99 return hashCode;
100 }
101
102 @Override
103 public boolean equals(Object obj) {
104 if (this == obj) {
105 return true;
106 }
107
108 if (obj == null) {
109 return false;
110 }
111
112 if (!FileSource.class.equals(obj.getClass())) {
113 return false;
114 }
115
116 FileSource other = (FileSource) obj;
117 return this.path.equals(other.path);
118 }
119 }