All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.goeuro.sync4j.fs.File Maven / Gradle / Ivy

package com.goeuro.sync4j.fs;

import com.goeuro.sync4j.fs.Path.Default;

import javax.annotation.Nonnegative;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.math.BigInteger;
import java.time.OffsetDateTime;
import java.util.Objects;
import java.util.Optional;

import static com.goeuro.sync4j.fs.Detail.Kind.file;
import static java.math.BigInteger.ZERO;
import static java.util.Optional.empty;
import static java.util.Optional.of;

@Immutable
public class File extends Default implements Detail {

    @Nonnull
    public static  Builder file(@Nonnull FileSystem fileSystem, @Nonnull T path) {
        return new Builder<>(fileSystem, path);
    }

    @Nonnull
    private final String md5Hash;
    @Nonnull
    private final BigInteger size;
    @Nonnull
    private final OffsetDateTime lastModified;

    protected File(
        @Nonnull FileSystem fileSystem,
        @Nonnull T path,
        @Nonnull String md5Hash,
        @Nonnull BigInteger size,
        @Nonnull OffsetDateTime lastModified
    ) {
        super(fileSystem, path);
        this.md5Hash = md5Hash;
        this.size = size;
        this.lastModified = lastModified;
    }

    @Nonnull
    @Override
    public Optional hasSameContent(@Nullable Path other) {
        if (!(other instanceof File)) {
            return empty();
        }
        final File o = (File) other;
        return of(Objects.equals(size(), o.size())
            && Objects.equals(md5Hash(), o.md5Hash()));
    }

    @Nonnull
    @Override
    public Kind kind() {
        return file;
    }

    @Nonnull
    public String md5Hash() {
        return md5Hash;
    }

    @Nonnull
    @Nonnegative
    public BigInteger size() {
        return size;
    }

    @Nonnull
    public OffsetDateTime lastModified() {
        return lastModified;
    }

    @Override
    public String toString() {
        return loose().toUri() + " {kind: " + kind() + ", size: " + size() + ", lastModified: " + lastModified() + ", md5: " + md5Hash() + "}";
    }

    public static class Builder {

        @Nonnull
        private final FileSystem fileSystem;
        @Nonnull
        private final T path;

        @Nonnull
        private Optional md5Hash = Optional.empty();
        @Nonnegative
        @Nonnull
        private Optional size = Optional.empty();
        @Nonnull
        private Optional lastModified = Optional.empty();

        protected Builder(
            @Nonnull FileSystem fileSystem,
            @Nonnull T path
        ) {
            this.fileSystem = fileSystem;
            this.path = path;
        }

        @Nonnull
        public Builder withMd5Hash(@Nonnull String md5Hash) {
            this.md5Hash = of(md5Hash);
            return this;
        }

        @Nonnull
        public Builder ofSize(@Nonnegative @Nonnull BigInteger size) {
            this.size = of(size);
            return this;
        }

        @Nonnull
        public Builder ofSize(@Nonnegative long size) {
            return ofSize(BigInteger.valueOf(size));
        }

        @Nonnull
        public Builder whichWasLastModifiedAt(@Nonnull OffsetDateTime lastModified) {
            this.lastModified = of(lastModified);
            return this;
        }

        @Nonnull
        public File build() {
            return new File<>(
                fileSystem,
                path,
                md5Hash.orElse(""),
                size.orElse(ZERO),
                lastModified.orElseGet(OffsetDateTime::now)
            );
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy