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

com.goeuro.sync4j.sync.SynchronizeStep Maven / Gradle / Ivy

package com.goeuro.sync4j.sync;

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

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.util.Objects;
import java.util.Optional;

import static java.util.Optional.empty;
import static java.util.Optional.ofNullable;

@Immutable
public class SynchronizeStep {

    @Nonnull
    public static  Builder step(@Nonnull SynchronizeTask task) {
        return step(task, task.from(), task.to());
    }

    @Nonnull
    public static  Builder step(@Nonnull SynchronizeTask task, @Nonnull Path from, @Nonnull Path to) {
        return new Builder<>(task, from, to);
    }

    @Nonnull
    private final Path from;
    @Nonnull
    private final Path to;
    @Nonnull
    private final SynchronizeTask task;
    @Nonnull
    private final Optional> parent;

    protected SynchronizeStep(
        @Nonnull SynchronizeTask task,
        @Nonnull Optional> parent,
        @Nonnull Path from,
        @Nonnull Path to
    ) {
        this.from = from;
        this.task = task;
        this.to = to;
        this.parent = parent;
    }

    @Nonnull
    public Path from() {
        return from;
    }

    @Nonnull
    public Path to() {
        return to;
    }

    @Nonnull
    public SynchronizeTask task() {
        return task;
    }

    @Nonnull
    public Optional> parent() {
        return parent;
    }

    @Nonnull
    public SynchronizeStep forActual(@Nonnull Path from, @Nonnull Path to) {
        return step(task(), from, to)
            .withParent(this)
            .build();
    }

    @Nonnull
    public SynchronizeStep forActualTo(@Nonnull Path to) {
        return forActual(from(), to);
    }

    @Nonnull
    public SynchronizeStep forActualFrom(@Nonnull Path from) {
        return forActual(from, to());
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof SynchronizeStep)) {
            return false;
        }
        final SynchronizeStep that = (SynchronizeStep) o;
        return Objects.equals(from(), that.from()) &&
            Objects.equals(to(), that.to());
    }

    @Override
    public int hashCode() {
        return Objects.hash(from(), to());
    }

    @Override
    public String toString() {
        return from() + " -> " + to();
    }

    public static class Builder {

        @Nonnull
        private final Path from;
        @Nonnull
        private final Path to;
        @Nonnull
        private final SynchronizeTask task;
        @Nonnull
        private Optional> parent = empty();

        protected Builder(
            @Nonnull SynchronizeTask task,
            @Nonnull Path from,
            @Nonnull Path to
        ) {
            this.from = from;
            this.to = to;
            this.task = task;
        }

        @Nonnull
        public Builder withParent(@Nullable SynchronizeStep parent) {
            this.parent = ofNullable(parent);
            return this;
        }

        @Nonnull
        public SynchronizeStep build() {
            //noinspection unchecked
            return new SynchronizeStep<>(
                task,
                parent,
                from,
                to
            );
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy