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

org.glowroot.common.MessageCount Maven / Gradle / Ivy

There is a newer version: 0.8.4
Show newest version
package org.glowroot.common;

import org.glowroot.shaded.google.common.base.MoreObjects;
import org.glowroot.shaded.google.common.base.Preconditions;
import org.glowroot.shaded.google.common.collect.Lists;
import java.util.Collection;
import javax.annotation.Generated;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;

/**
 * Immutable implementation of {@link SpyingLogbackFilter.MessageCountBase}.
 * 

* Use builder to create immutable instances: * {@code MessageCount.builder()}. * Use static factory method to create immutable instances: * {@code MessageCount.of()}. */ @SuppressWarnings("all") @ParametersAreNonnullByDefault @Generated({"Immutables.generator", "SpyingLogbackFilter.MessageCountBase"}) @Immutable public final class MessageCount extends SpyingLogbackFilter.MessageCountBase { private final int expectedCount; private final int unexpectedCount; private MessageCount(int expectedCount, int unexpectedCount) { this.expectedCount = expectedCount; this.unexpectedCount = unexpectedCount; } private MessageCount(MessageCount.Builder builder) { this.expectedCount = builder.expectedCount; this.unexpectedCount = builder.unexpectedCount; } private MessageCount(MessageCount original, int expectedCount, int unexpectedCount) { this.expectedCount = expectedCount; this.unexpectedCount = unexpectedCount; } /** * {@inheritDoc} * @return value of {@code expectedCount} attribute */ @Override public int expectedCount() { return expectedCount; } /** * {@inheritDoc} * @return value of {@code unexpectedCount} attribute */ @Override public int unexpectedCount() { return unexpectedCount; } /** * Copy current immutable object by setting value for {@link SpyingLogbackFilter.MessageCountBase#expectedCount() expectedCount}. * Value equality check is used to prevent copying of the same value by returning {@code this}. * @param value new value for expectedCount * @return modified copy of the {@code this} object */ public final MessageCount withExpectedCount(int value) { if (this.expectedCount == value) { return this; } int newValue = value; return new MessageCount(this, newValue, this.unexpectedCount); } /** * Copy current immutable object by setting value for {@link SpyingLogbackFilter.MessageCountBase#unexpectedCount() unexpectedCount}. * Value equality check is used to prevent copying of the same value by returning {@code this}. * @param value new value for unexpectedCount * @return modified copy of the {@code this} object */ public final MessageCount withUnexpectedCount(int value) { if (this.unexpectedCount == value) { return this; } int newValue = value; return new MessageCount(this, this.expectedCount, newValue); } /** * This instance is equal to instances of {@code MessageCount} with equal attribute values. * @return {@code true} if {@code this} is equal to {@code another} instance */ @Override public boolean equals(@Nullable Object another) { return this == another || (another instanceof MessageCount && equalTo((MessageCount) another)); } private boolean equalTo(MessageCount another) { return expectedCount == another.expectedCount && unexpectedCount == another.unexpectedCount; } /** * Computes hash code from attributes: {@code expectedCount}, {@code unexpectedCount}. * @return hashCode value */ @Override public int hashCode() { int h = 31; h = h * 17 + expectedCount; h = h * 17 + unexpectedCount; return h; } /** * Prints immutable value {@code MessageCount{...}} with attribute values, * excluding any non-generated and auxiliary attributes. * @return string representation of value */ @Override public String toString() { return MoreObjects.toStringHelper("MessageCount") .add("expectedCount", expectedCount) .add("unexpectedCount", unexpectedCount) .toString(); } /** * Construct new immutable {@code MessageCount} instance. * @param expectedCount value for {@code expectedCount} * @param unexpectedCount value for {@code unexpectedCount} * @return immutable MessageCount instance */ public static MessageCount of(int expectedCount, int unexpectedCount) { return new MessageCount(expectedCount, unexpectedCount); } /** * Creates immutable copy of {@link SpyingLogbackFilter.MessageCountBase}. * Uses accessors to get values to initialize immutable instance. * If an instance is already immutable, it is returned as is. * @return copied immutable MessageCount instance */ public static MessageCount copyOf(SpyingLogbackFilter.MessageCountBase instance) { if (instance instanceof MessageCount) { return (MessageCount) instance; } return MessageCount.builder() .from(instance) .build(); } /** * Creates builder for {@link org.glowroot.common.MessageCount}. * @return new MessageCount builder */ public static MessageCount.Builder builder() { return new MessageCount.Builder(); } /** * Builds instances of {@link org.glowroot.common.MessageCount}. * Initialized attributes and then invoke {@link #build()} method to create * immutable instance. *

Builder is not thread safe and generally should not be stored in field or collection, * but used immediately to create instances. */ @NotThreadSafe public static final class Builder { private static final long INITIALIZED_BITSET_ALL = 0x3; private static final long INITIALIZED_BIT_EXPECTED_COUNT = 0x1L; private static final long INITIALIZED_BIT_UNEXPECTED_COUNT = 0x2L; private long initializedBitset; private int expectedCount; private int unexpectedCount; private Builder() {} /** * Adjust builder with values from provided {@link SpyingLogbackFilter.MessageCountBase} instance. * Regular attribute values will be overridden, i.e. replaced with ones of an instance. * Instance's absent optional values will not be copied (will not override current). * Collection elements and entries will be added, not replaced. * @param instance instance to copy values from * @return {@code this} builder for chained invocation */ public final Builder from(SpyingLogbackFilter.MessageCountBase instance) { Preconditions.checkNotNull(instance); expectedCount(instance.expectedCount()); unexpectedCount(instance.unexpectedCount()); return this; } /** * Initializes value for {@link SpyingLogbackFilter.MessageCountBase#expectedCount() expectedCount}. * @param expectedCount value for expectedCount * @return {@code this} builder for chained invocation */ public final Builder expectedCount(int expectedCount) { this.expectedCount = expectedCount; initializedBitset |= INITIALIZED_BIT_EXPECTED_COUNT; return this; } /** * Initializes value for {@link SpyingLogbackFilter.MessageCountBase#unexpectedCount() unexpectedCount}. * @param unexpectedCount value for unexpectedCount * @return {@code this} builder for chained invocation */ public final Builder unexpectedCount(int unexpectedCount) { this.unexpectedCount = unexpectedCount; initializedBitset |= INITIALIZED_BIT_UNEXPECTED_COUNT; return this; } /** * Builds new {@link org.glowroot.common.MessageCount}. * @return immutable instance of MessageCount */ public MessageCount build() { checkRequiredAttributes(); return new MessageCount(this); } private boolean expectedCountIsSet() { return (initializedBitset & INITIALIZED_BIT_EXPECTED_COUNT) != 0; } private boolean unexpectedCountIsSet() { return (initializedBitset & INITIALIZED_BIT_UNEXPECTED_COUNT) != 0; } private void checkRequiredAttributes() { if (initializedBitset != INITIALIZED_BITSET_ALL) { throw new IllegalStateException(formatRequiredAttributesMessage()); } } private String formatRequiredAttributesMessage() { Collection attributes = Lists.newArrayList(); if (!expectedCountIsSet()) { attributes.add("expectedCount"); } if (!unexpectedCountIsSet()) { attributes.add("unexpectedCount"); } return "Cannot build MessageCount, some of required attributes are not set " + attributes; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy