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

com.palantir.timelock.feedback.LeaderElectionDuration Maven / Gradle / Ivy

There is a newer version: 0.1152.0
Show newest version
package com.palantir.timelock.feedback;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.google.errorprone.annotations.CheckReturnValue;
import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.Safe;
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.processing.Generated;

@JsonDeserialize(builder = LeaderElectionDuration.Builder.class)
@Generated("com.palantir.conjure.java.types.BeanGenerator")
public final class LeaderElectionDuration {
    private final UUID oldLeader;

    private final UUID newLeader;

    private final Duration duration;

    private int memoizedHashCode;

    private LeaderElectionDuration(UUID oldLeader, UUID newLeader, Duration duration) {
        validateFields(oldLeader, newLeader, duration);
        this.oldLeader = oldLeader;
        this.newLeader = newLeader;
        this.duration = duration;
    }

    @JsonProperty("oldLeader")
    @Safe
    public UUID getOldLeader() {
        return this.oldLeader;
    }

    @JsonProperty("newLeader")
    @Safe
    public UUID getNewLeader() {
        return this.newLeader;
    }

    @JsonProperty("duration")
    public Duration getDuration() {
        return this.duration;
    }

    @Override
    public boolean equals(@Nullable Object other) {
        return this == other || (other instanceof LeaderElectionDuration && equalTo((LeaderElectionDuration) other));
    }

    private boolean equalTo(LeaderElectionDuration other) {
        if (this.memoizedHashCode != 0
                && other.memoizedHashCode != 0
                && this.memoizedHashCode != other.memoizedHashCode) {
            return false;
        }
        return this.oldLeader.equals(other.oldLeader)
                && this.newLeader.equals(other.newLeader)
                && this.duration.equals(other.duration);
    }

    @Override
    public int hashCode() {
        int result = memoizedHashCode;
        if (result == 0) {
            int hash = 1;
            hash = 31 * hash + this.oldLeader.hashCode();
            hash = 31 * hash + this.newLeader.hashCode();
            hash = 31 * hash + this.duration.hashCode();
            result = hash;
            memoizedHashCode = result;
        }
        return result;
    }

    @Override
    public String toString() {
        return "LeaderElectionDuration{oldLeader: " + oldLeader + ", newLeader: " + newLeader + ", duration: "
                + duration + '}';
    }

    public static LeaderElectionDuration of(@Safe UUID oldLeader, @Safe UUID newLeader, Duration duration) {
        return builder()
                .oldLeader(oldLeader)
                .newLeader(newLeader)
                .duration(duration)
                .build();
    }

    private static void validateFields(UUID oldLeader, UUID newLeader, Duration duration) {
        List missingFields = null;
        missingFields = addFieldIfMissing(missingFields, oldLeader, "oldLeader");
        missingFields = addFieldIfMissing(missingFields, newLeader, "newLeader");
        missingFields = addFieldIfMissing(missingFields, duration, "duration");
        if (missingFields != null) {
            throw new SafeIllegalArgumentException(
                    "Some required fields have not been set", SafeArg.of("missingFields", missingFields));
        }
    }

    private static List addFieldIfMissing(List prev, Object fieldValue, String fieldName) {
        List missingFields = prev;
        if (fieldValue == null) {
            if (missingFields == null) {
                missingFields = new ArrayList<>(3);
            }
            missingFields.add(fieldName);
        }
        return missingFields;
    }

    public static Builder builder() {
        return new Builder();
    }

    @Generated("com.palantir.conjure.java.types.BeanBuilderGenerator")
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static final class Builder {
        boolean _buildInvoked;

        private @Safe UUID oldLeader;

        private @Safe UUID newLeader;

        private Duration duration;

        private Builder() {}

        public Builder from(LeaderElectionDuration other) {
            checkNotBuilt();
            oldLeader(other.getOldLeader());
            newLeader(other.getNewLeader());
            duration(other.getDuration());
            return this;
        }

        @JsonSetter("oldLeader")
        public Builder oldLeader(@Nonnull @Safe UUID oldLeader) {
            checkNotBuilt();
            this.oldLeader = Preconditions.checkNotNull(oldLeader, "oldLeader cannot be null");
            return this;
        }

        @JsonSetter("newLeader")
        public Builder newLeader(@Nonnull @Safe UUID newLeader) {
            checkNotBuilt();
            this.newLeader = Preconditions.checkNotNull(newLeader, "newLeader cannot be null");
            return this;
        }

        @JsonSetter("duration")
        public Builder duration(@Nonnull Duration duration) {
            checkNotBuilt();
            this.duration = Preconditions.checkNotNull(duration, "duration cannot be null");
            return this;
        }

        @CheckReturnValue
        public LeaderElectionDuration build() {
            checkNotBuilt();
            this._buildInvoked = true;
            return new LeaderElectionDuration(oldLeader, newLeader, duration);
        }

        private void checkNotBuilt() {
            Preconditions.checkState(!_buildInvoked, "Build has already been called");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy