Skip to content

Commit

Permalink
chore: extract method to check Path is S3Path (#225)
Browse files Browse the repository at this point in the history
* chore: extract method to checkPath is S3. DRY

* chore(review): Use `Objects.requireNotNull` and remove unnecessary parenthesis
  • Loading branch information
guicamest authored Oct 25, 2023
1 parent 9b92346 commit c418bd5
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 36 deletions.
46 changes: 22 additions & 24 deletions src/main/java/software/amazon/nio/spi/s3/S3FileSystemProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ public SeekableByteChannel newByteChannel(Path path, Set<? extends OpenOption> o
options = Collections.emptySet();
}

final S3Path s3Path = (S3Path)path;
final S3Path s3Path = checkPath(path);
final S3SeekableByteChannel channel;
final S3FileSystem fs = s3Path.getFileSystem();

Expand Down Expand Up @@ -395,7 +395,7 @@ protected DirectoryStream<Path> newDirectoryStream(S3AsyncClient s3Client, Path
*/
@Override
public DirectoryStream<Path> newDirectoryStream(Path dir, DirectoryStream.Filter<? super Path> filter) throws IOException {
S3Path s3Path = (S3Path) dir;
S3Path s3Path = checkPath(dir);

String dirName = s3Path.toAbsolutePath().getKey();

Expand Down Expand Up @@ -482,9 +482,10 @@ private Iterator<Path> pathIteratorForPublisher (
*/
@Override
public void createDirectory(Path dir, FileAttribute<?>... attrs) throws IOException {
S3FileSystem fs = ((S3Path)dir).getFileSystem();
S3Path s3Directory = checkPath(dir);
S3FileSystem fs = s3Directory.getFileSystem();
try {
String directoryKey = ((S3Path)dir).toRealPath(NOFOLLOW_LINKS).getKey();
String directoryKey = s3Directory.toRealPath(NOFOLLOW_LINKS).getKey();
if (!directoryKey.endsWith(S3Path.PATH_SEPARATOR) && !directoryKey.isEmpty()) {
directoryKey = directoryKey + S3Path.PATH_SEPARATOR;
}
Expand Down Expand Up @@ -541,9 +542,9 @@ protected void createDirectory(S3AsyncClient s3Client, Path dir, FileAttribute<?
*/
@Override
public void delete(Path path) throws IOException {
S3FileSystem fs = ((S3Path)path).getFileSystem();
final S3Path s3Path = checkPath(path);
try {
final S3Path s3Path = (S3Path) path;
final S3FileSystem fs = s3Path.getFileSystem();
final S3AsyncClient s3Client = fs.client();
final String bucketName = fs.bucketName();

Expand Down Expand Up @@ -625,10 +626,10 @@ public void copy(Path source, Path target, CopyOption... options) throws IOExcep

List<CopyOption> copyOptions = Arrays.asList(options);

S3Path s3SourcePath = (S3Path) source;
S3Path s3TargetPath = (S3Path) target;
S3Path s3SourcePath = checkPath(source);
S3Path s3TargetPath = checkPath(target);

final S3FileSystem fs = ((S3Path)source).getFileSystem();
final S3FileSystem fs = s3SourcePath.getFileSystem();
final S3AsyncClient s3Client = fs.client();
final String bucketName = fs.bucketName();

Expand Down Expand Up @@ -692,7 +693,7 @@ public void copy(Path source, Path target, CopyOption... options) throws IOExcep
*/
@Deprecated
protected void copy(S3AsyncClient s3Client, Path source, Path target, CopyOption... options) throws IOException, ExecutionException, InterruptedException {
copy(forceAwsClient((S3Path)source, s3Client), target, options);
copy(forceAwsClient(source, s3Client), target, options);
}

protected boolean exists(S3AsyncClient s3Client, S3Path path) throws InterruptedException, TimeoutException {
Expand Down Expand Up @@ -857,9 +858,7 @@ public FileStore getFileStore(Path path) {
@Override
public void checkAccess(Path path, AccessMode... modes) throws IOException {
try {
assert path instanceof S3Path;

final S3Path s3Path = (S3Path) path.toRealPath(NOFOLLOW_LINKS);
final S3Path s3Path = checkPath(path.toRealPath(NOFOLLOW_LINKS));
final S3FileSystem fs = s3Path.getFileSystem();
final String bucketName = fs.bucketName();
final S3AsyncClient s3Client = fs.client();
Expand Down Expand Up @@ -943,12 +942,8 @@ protected void checkAccess(S3AsyncClient s3Client, Path path, AccessMode... mode
*/
@Override
public <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V> type, LinkOption... options) {
Objects.requireNonNull(path, "cannot obtain attributes for a null path");
Objects.requireNonNull(type, "the type of attribute view required cannot be null");

if (!(path instanceof S3Path))
throw new IllegalArgumentException("path must be an S3 Path");
S3Path s3Path = (S3Path) path;
S3Path s3Path = checkPath(path);

if (type.equals(BasicFileAttributeView.class) || type.equals(S3FileAttributeView.class)) {
@SuppressWarnings("unchecked") final V v = (V) new S3FileAttributeView(s3Path);
Expand All @@ -971,11 +966,8 @@ public <V extends FileAttributeView> V getFileAttributeView(Path path, Class<V>
*/
@Override
public <A extends BasicFileAttributes> A readAttributes(Path path, Class<A> type, LinkOption... options) {
Objects.requireNonNull(path);
Objects.requireNonNull(type);
if (!(path instanceof S3Path))
throw new IllegalArgumentException("path must be an S3Path instance");
S3Path s3Path = (S3Path) path;
S3Path s3Path = checkPath(path);
S3AsyncClient s3Client = s3Path.getFileSystem().client();

if (type.equals(BasicFileAttributes.class) || type.equals(S3BasicFileAttributes.class)) {
Expand Down Expand Up @@ -1032,9 +1024,8 @@ protected <A extends BasicFileAttributes> A readAttributes(S3AsyncClient s3Async
*/
@Override
public Map<String, Object> readAttributes(Path path, String attributes, LinkOption... options) {
Objects.requireNonNull(path);
Objects.requireNonNull(attributes);
S3Path s3Path = (S3Path) path;
S3Path s3Path = checkPath(path);

S3AsyncClient s3Client = s3Path.getFileSystem().client();

Expand Down Expand Up @@ -1142,4 +1133,11 @@ private S3Path forceAwsClient(final Path path, final S3AsyncClient client) {
p.getFileSystem().clientProvider(new FixedS3ClientProvider(client));
return p;
}

protected static S3Path checkPath(Path obj) {
Objects.requireNonNull(obj);
if (!(obj instanceof S3Path))
throw new ProviderMismatchException();
return (S3Path)obj;
}
}
22 changes: 10 additions & 12 deletions src/main/java/software/amazon/nio/spi/s3/S3Path.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import java.util.Objects;

import static java.nio.file.LinkOption.NOFOLLOW_LINKS;
import static software.amazon.nio.spi.s3.S3FileSystemProvider.checkPath;

import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.nio.spi.s3.config.S3NioSpiConfiguration;

Expand Down Expand Up @@ -448,8 +450,7 @@ public S3Path normalize() {
*/
@Override
public S3Path resolve(Path other) {
if(!(other instanceof S3Path)) throw new ProviderMismatchException("a non s3 path cannot be resolved against and S3Path");
S3Path s3Other = (S3Path) other;
S3Path s3Other = checkPath(other);

if(!this.bucketName().equals(s3Other.bucketName())) throw new IllegalArgumentException("S3Paths cannot be resolved when they are from different buckets");

Expand Down Expand Up @@ -541,21 +542,20 @@ public S3Path resolveSibling(String other) {
*/
@Override
public S3Path relativize(Path other) {
if(!(other instanceof S3Path)) throw new IllegalArgumentException("path is not an S3Path");
S3Path otherPath = checkPath(other);

if(this.equals(other)) return from("");
if(this.equals(otherPath)) return from("");

if(this.isAbsolute() != other.isAbsolute()) throw new IllegalArgumentException("to obtain a relative path both must be absolute or both must be relative");
if(!Objects.equals(this.bucketName(), ((S3Path) other).bucketName())) throw new IllegalArgumentException("cannot relativize S3Paths from different buckets");
if(this.isAbsolute() != otherPath.isAbsolute()) throw new IllegalArgumentException("to obtain a relative path both must be absolute or both must be relative");
if(!Objects.equals(this.bucketName(), otherPath.bucketName())) throw new IllegalArgumentException("cannot relativize S3Paths from different buckets");

S3Path otherPath = (S3Path) other;
if(this.isEmpty()) return otherPath;

int nameCount = this.getNameCount();
int otherNameCount = other.getNameCount();
int otherNameCount = otherPath.getNameCount();

int limit = Math.min(nameCount, otherNameCount);
int differenceCount = getDifferenceCount(other, limit);
int differenceCount = getDifferenceCount(otherPath, limit);

int parentDirCount = nameCount - differenceCount;
if (differenceCount < otherNameCount) {
Expand Down Expand Up @@ -781,9 +781,7 @@ public Iterator<Path> iterator() {
*/
@Override
public int compareTo(Path other) {
if(!(other instanceof S3Path)) throw new ClassCastException("compared paths must be from the same provider");

S3Path o = (S3Path) other;
S3Path o = checkPath(other);
if(o.fileSystem != this.fileSystem) throw new ClassCastException("compared S3 paths must be from the same bucket");
return this.toRealPath(NOFOLLOW_LINKS).toString().compareTo(
o.toRealPath(NOFOLLOW_LINKS).toString());
Expand Down

0 comments on commit c418bd5

Please sign in to comment.