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

com.myperfit.sdk.transactional.PerfitTransactional Maven / Gradle / Ivy

There is a newer version: 1.1.8
Show newest version
package com.myperfit.sdk.transactional;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.json.simple.parser.JSONParser;

/**
 * Immutable implementation of {@link AbstractPerfitTransactional}.
 * 

* Use the builder to create immutable instances: * {@code PerfitTransactional.builder()}. */ @SuppressWarnings({"all"}) @ParametersAreNonnullByDefault @Immutable public final class PerfitTransactional extends AbstractPerfitTransactional { private final String apiKey; private final String url; private final ObjectMapper mapper; private final JSONParser parser; private PerfitTransactional(PerfitTransactional.Builder builder) { this.apiKey = builder.apiKey; if (builder.url != null) { initShim.url(builder.url); } if (builder.mapper != null) { initShim.mapper(builder.mapper); } if (builder.parser != null) { initShim.parser(builder.parser); } this.url = initShim.url(); this.mapper = initShim.mapper(); this.parser = initShim.parser(); this.initShim = null; } private PerfitTransactional( String apiKey, String url, ObjectMapper mapper, JSONParser parser) { this.apiKey = apiKey; this.url = url; this.mapper = mapper; this.parser = parser; this.initShim = null; } private static final int STAGE_INITIALIZING = -1; private static final int STAGE_UNINITIALIZED = 0; private static final int STAGE_INITIALIZED = 1; private transient volatile InitShim initShim = new InitShim(); private final class InitShim { private String url; private int urlBuildStage; String url() { if (urlBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (urlBuildStage == STAGE_UNINITIALIZED) { urlBuildStage = STAGE_INITIALIZING; this.url = Objects.requireNonNull(PerfitTransactional.super.url(), "url"); urlBuildStage = STAGE_INITIALIZED; } return this.url; } void url(String url) { this.url = url; urlBuildStage = STAGE_INITIALIZED; } private ObjectMapper mapper; private int mapperBuildStage; ObjectMapper mapper() { if (mapperBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (mapperBuildStage == STAGE_UNINITIALIZED) { mapperBuildStage = STAGE_INITIALIZING; this.mapper = Objects.requireNonNull(PerfitTransactional.super.mapper(), "mapper"); mapperBuildStage = STAGE_INITIALIZED; } return this.mapper; } void mapper(ObjectMapper mapper) { this.mapper = mapper; mapperBuildStage = STAGE_INITIALIZED; } private JSONParser parser; private int parserBuildStage; JSONParser parser() { if (parserBuildStage == STAGE_INITIALIZING) throw new IllegalStateException(formatInitCycleMessage()); if (parserBuildStage == STAGE_UNINITIALIZED) { parserBuildStage = STAGE_INITIALIZING; this.parser = Objects.requireNonNull(PerfitTransactional.super.parser(), "parser"); parserBuildStage = STAGE_INITIALIZED; } return this.parser; } void parser(JSONParser parser) { this.parser = parser; parserBuildStage = STAGE_INITIALIZED; } private String formatInitCycleMessage() { ArrayList attributes = new ArrayList(); if (urlBuildStage == STAGE_INITIALIZING) attributes.add("url"); if (mapperBuildStage == STAGE_INITIALIZING) attributes.add("mapper"); if (parserBuildStage == STAGE_INITIALIZING) attributes.add("parser"); return "Cannot build PerfitTransactional, attribute initializers form cycle" + attributes; } } /** * @return The value of the {@code apiKey} attribute */ @Override public String apiKey() { return apiKey; } /** * @return The value of the {@code url} attribute */ @Override public String url() { InitShim shim = this.initShim; return shim != null ? shim.url() : this.url; } /** * @return The value of the {@code mapper} attribute */ @Override public ObjectMapper mapper() { InitShim shim = this.initShim; return shim != null ? shim.mapper() : this.mapper; } /** * @return The value of the {@code parser} attribute */ @Override public JSONParser parser() { InitShim shim = this.initShim; return shim != null ? shim.parser() : this.parser; } /** * Copy the current immutable object by setting a value for the {@link AbstractPerfitTransactional#apiKey() apiKey} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for apiKey * @return A modified copy of the {@code this} object */ public final PerfitTransactional withApiKey(String value) { if (this.apiKey.equals(value)) return this; String newValue = Objects.requireNonNull(value, "apiKey"); return new PerfitTransactional(newValue, this.url, this.mapper, this.parser); } /** * Copy the current immutable object by setting a value for the {@link AbstractPerfitTransactional#url() url} attribute. * An equals check used to prevent copying of the same value by returning {@code this}. * @param value A new value for url * @return A modified copy of the {@code this} object */ public final PerfitTransactional withUrl(String value) { if (this.url.equals(value)) return this; String newValue = Objects.requireNonNull(value, "url"); return new PerfitTransactional(this.apiKey, newValue, this.mapper, this.parser); } /** * Copy the current immutable object by setting a value for the {@link AbstractPerfitTransactional#mapper() mapper} attribute. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for mapper * @return A modified copy of the {@code this} object */ public final PerfitTransactional withMapper(ObjectMapper value) { if (this.mapper == value) return this; ObjectMapper newValue = Objects.requireNonNull(value, "mapper"); return new PerfitTransactional(this.apiKey, this.url, newValue, this.parser); } /** * Copy the current immutable object by setting a value for the {@link AbstractPerfitTransactional#parser() parser} attribute. * A shallow reference equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for parser * @return A modified copy of the {@code this} object */ public final PerfitTransactional withParser(JSONParser value) { if (this.parser == value) return this; JSONParser newValue = Objects.requireNonNull(value, "parser"); return new PerfitTransactional(this.apiKey, this.url, this.mapper, newValue); } /** * This instance is equal to all instances of {@code PerfitTransactional} that have equal attribute values. * @return {@code true} if {@code this} is equal to {@code another} instance */ @Override public boolean equals(@Nullable Object another) { if (this == another) return true; return another instanceof PerfitTransactional && equalTo((PerfitTransactional) another); } private boolean equalTo(PerfitTransactional another) { return apiKey.equals(another.apiKey) && url.equals(another.url) && mapper.equals(another.mapper) && parser.equals(another.parser); } /** * Computes a hash code from attributes: {@code apiKey}, {@code url}, {@code mapper}, {@code parser}. * @return hashCode value */ @Override public int hashCode() { int h = 5381; h += (h << 5) + apiKey.hashCode(); h += (h << 5) + url.hashCode(); h += (h << 5) + mapper.hashCode(); h += (h << 5) + parser.hashCode(); return h; } /** * Prints the immutable value {@code PerfitTransactional} with attribute values. * @return A string representation of the value */ @Override public String toString() { return "PerfitTransactional{" + "apiKey=" + apiKey + ", url=" + url + ", mapper=" + mapper + ", parser=" + parser + "}"; } /** * Creates an immutable copy of a {@link AbstractPerfitTransactional} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param instance The instance to copy * @return A copied immutable PerfitTransactional instance */ public static PerfitTransactional copyOf(AbstractPerfitTransactional instance) { if (instance instanceof PerfitTransactional) { return (PerfitTransactional) instance; } return PerfitTransactional.builder() .from(instance) .build(); } /** * Creates a builder for {@link PerfitTransactional PerfitTransactional}. * @return A new PerfitTransactional builder */ public static PerfitTransactional.Builder builder() { return new PerfitTransactional.Builder(); } /** * Builds instances of type {@link PerfitTransactional PerfitTransactional}. * Initialize attributes and then invoke the {@link #build()} method to create an * immutable instance. *

{@code Builder} is not thread-safe and generally should not be stored in a field or collection, * but instead used immediately to create instances. */ @NotThreadSafe public static final class Builder { private static final long INIT_BIT_API_KEY = 0x1L; private long initBits = 0x1L; private @Nullable String apiKey; private @Nullable String url; private @Nullable ObjectMapper mapper; private @Nullable JSONParser parser; private Builder() { } /** * Fill a builder with attribute values from the provided {@code AbstractPerfitTransactional} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final Builder from(AbstractPerfitTransactional instance) { Objects.requireNonNull(instance, "instance"); apiKey(instance.apiKey()); url(instance.url()); mapper(instance.mapper()); parser(instance.parser()); return this; } /** * Initializes the value for the {@link AbstractPerfitTransactional#apiKey() apiKey} attribute. * @param apiKey The value for apiKey * @return {@code this} builder for use in a chained invocation */ public final Builder apiKey(String apiKey) { this.apiKey = Objects.requireNonNull(apiKey, "apiKey"); initBits &= ~INIT_BIT_API_KEY; return this; } /** * Initializes the value for the {@link AbstractPerfitTransactional#url() url} attribute. *

If not set, this attribute will have a default value as returned by the initializer of {@link AbstractPerfitTransactional#url() url}. * @param url The value for url * @return {@code this} builder for use in a chained invocation */ public final Builder url(String url) { this.url = Objects.requireNonNull(url, "url"); return this; } /** * Initializes the value for the {@link AbstractPerfitTransactional#mapper() mapper} attribute. *

If not set, this attribute will have a default value as returned by the initializer of {@link AbstractPerfitTransactional#mapper() mapper}. * @param mapper The value for mapper * @return {@code this} builder for use in a chained invocation */ public final Builder mapper(ObjectMapper mapper) { this.mapper = Objects.requireNonNull(mapper, "mapper"); return this; } /** * Initializes the value for the {@link AbstractPerfitTransactional#parser() parser} attribute. *

If not set, this attribute will have a default value as returned by the initializer of {@link AbstractPerfitTransactional#parser() parser}. * @param parser The value for parser * @return {@code this} builder for use in a chained invocation */ public final Builder parser(JSONParser parser) { this.parser = Objects.requireNonNull(parser, "parser"); return this; } /** * Builds a new {@link PerfitTransactional PerfitTransactional}. * @return An immutable instance of PerfitTransactional * @throws java.lang.IllegalStateException if any required attributes are missing */ public PerfitTransactional build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return new PerfitTransactional(this); } private String formatRequiredAttributesMessage() { List attributes = new ArrayList(); if ((initBits & INIT_BIT_API_KEY) != 0) attributes.add("apiKey"); return "Cannot build PerfitTransactional, some of required attributes are not set " + attributes; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy