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

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

There is a newer version: 0.14
Show newest version
package com.goeuro.sync4j.sync;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Objects;
import java.util.Optional;
import java.util.UUID;

import static java.time.OffsetDateTime.now;
import static java.util.Objects.requireNonNull;
import static java.util.Optional.ofNullable;

@Immutable
public class LockFileContent {

    @Nonnull
    public static Builder lockFileContent() {
        return new Builder();
    }

    @Nonnull
    private final UUID id;
    @Nonnull
    private final OffsetDateTime lastPing;
    @Nonnull
    private final Optional owner;

    protected LockFileContent(
        @Nonnull UUID id,
        @Nonnull OffsetDateTime lastPing,
        @Nonnull Optional owner
    ) {
        this.id = id;
        this.lastPing = lastPing;
        this.owner = owner;
    }

    @Nonnull
    public UUID id() {
        return id;
    }

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

    @Nonnull
    public Optional owner() {
        return owner;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) { return true; }
        if (o == null || getClass() != o.getClass()) { return false; }
        final LockFileContent that = (LockFileContent) o;
        return Objects.equals(id, that.id) &&
            Objects.equals(lastPing, that.lastPing) &&
            Objects.equals(owner, that.owner);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, lastPing, owner);
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder();
        sb.append("#").append(id());
        sb.append(" last pinged ").append(lastPing());
        owner().ifPresent(owner -> sb.append(", owned by ").append(owner));
        return sb.toString();
    }

    public static class Builder {

        @Nonnull
        private Optional id = Optional.empty();
        @Nonnull
        private Optional lastPing = Optional.empty();
        @Nonnull
        private Optional owner = Optional.empty();

        @Nonnull
        public Builder withId(@Nonnull Optional id) {
            this.id = requireNonNull(id);
            return this;
        }

        @Nonnull
        public Builder withId(@Nullable UUID id) {
            return withId(ofNullable(id));
        }

        @Nonnull
        public Builder lastPingedAt(@Nonnull Optional lastPing) {
            this.lastPing = requireNonNull(lastPing);
            return this;
        }

        @Nonnull
        public Builder lastPingedAt(@Nullable OffsetDateTime lastPing) {
            return lastPingedAt(ofNullable(lastPing));
        }

        @Nonnull
        public Builder withOwner(@Nonnull Optional owner) {
            this.owner = requireNonNull(owner);
            return this;
        }

        @Nonnull
        public Builder withOwner(@Nullable String owner) {
            return withOwner(ofNullable(owner));
        }

        @Nonnull
        public LockFileContent build() {
            return new LockFileContent(
                id.orElseGet(UUID::randomUUID),
                lastPing.orElseGet(() -> now(ZoneId.of("Z"))),
                owner
            );
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy