org.immutables.fixture.jdk.ImmutableJdk9Collections Maven / Gradle / Ivy
Show all versions of value-fixture Show documentation
package org.immutables.fixture.jdk;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Var;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import javax.annotation.CheckReturnValue;
import javax.annotation.Nullable;
import javax.annotation.ParametersAreNonnullByDefault;
import javax.annotation.concurrent.Immutable;
import javax.annotation.concurrent.NotThreadSafe;
import org.immutables.value.Generated;
/**
* Immutable implementation of {@link Jdk9Collections}.
*
* Use the builder to create immutable instances:
* {@code ImmutableJdk9Collections.builder()}.
*/
@Generated(from = "Jdk9Collections", generator = "Immutables")
@SuppressWarnings({"all"})
@ParametersAreNonnullByDefault
@javax.annotation.processing.Generated("org.immutables.processor.ProxyProcessor")
@Immutable
@CheckReturnValue
public final class ImmutableJdk9Collections implements Jdk9Collections {
private final Set s;
private final List l;
private final Map m;
private ImmutableJdk9Collections(
Set s,
List l,
Map m) {
this.s = s;
this.l = l;
this.m = m;
}
/**
* @return The value of the {@code s} attribute
*/
@Override
public Set s() {
return s;
}
/**
* @return The value of the {@code l} attribute
*/
@Override
public List l() {
return l;
}
/**
* @return The value of the {@code m} attribute
*/
@Override
public Map m() {
return m;
}
/**
* Copy the current immutable object with elements that replace the content of {@link Jdk9Collections#s() s}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableJdk9Collections withS(String... elements) {
Set newValue = Set.of(elements);
return new ImmutableJdk9Collections(newValue, this.l, this.m);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Jdk9Collections#s() s}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of s elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableJdk9Collections withS(Iterable elements) {
if (this.s == elements) return this;
Set newValue = elements instanceof Collection>
? Set.copyOf((Collection) elements)
: StreamSupport.stream(elements.spliterator(), false)
.collect(Collectors.toUnmodifiableSet());
return new ImmutableJdk9Collections(newValue, this.l, this.m);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Jdk9Collections#l() l}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableJdk9Collections withL(String... elements) {
List newValue = List.of(elements);
return new ImmutableJdk9Collections(this.s, newValue, this.m);
}
/**
* Copy the current immutable object with elements that replace the content of {@link Jdk9Collections#l() l}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of l elements to set
* @return A modified copy of {@code this} object
*/
public final ImmutableJdk9Collections withL(Iterable elements) {
if (this.l == elements) return this;
List newValue = elements instanceof Collection>
? List.copyOf((Collection) elements)
: StreamSupport.stream(elements.spliterator(), false)
.collect(Collectors.toUnmodifiableList());
return new ImmutableJdk9Collections(this.s, newValue, this.m);
}
/**
* Copy the current immutable object by replacing the {@link Jdk9Collections#m() m} map with the specified map.
* Nulls are not permitted as keys or values.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param entries The entries to be added to the m map
* @return A modified copy of {@code this} object
*/
public final ImmutableJdk9Collections withM(Map entries) {
if (this.m == entries) return this;
Map newValue = Map.copyOf(entries);
return new ImmutableJdk9Collections(this.s, this.l, newValue);
}
/**
* This instance is equal to all instances of {@code ImmutableJdk9Collections} 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 ImmutableJdk9Collections
&& equalTo(0, (ImmutableJdk9Collections) another);
}
private boolean equalTo(int synthetic, ImmutableJdk9Collections another) {
return s.equals(another.s)
&& l.equals(another.l)
&& m.equals(another.m);
}
/**
* Computes a hash code from attributes: {@code s}, {@code l}, {@code m}.
* @return hashCode value
*/
@Override
public int hashCode() {
@Var int h = 5381;
h += (h << 5) + s.hashCode();
h += (h << 5) + l.hashCode();
h += (h << 5) + m.hashCode();
return h;
}
/**
* Prints the immutable value {@code Jdk9Collections} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "Jdk9Collections{"
+ "s=" + s
+ ", l=" + l
+ ", m=" + m
+ "}";
}
/**
* Creates an immutable copy of a {@link Jdk9Collections} 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 Jdk9Collections instance
*/
public static ImmutableJdk9Collections copyOf(Jdk9Collections instance) {
if (instance instanceof ImmutableJdk9Collections) {
return (ImmutableJdk9Collections) instance;
}
return ImmutableJdk9Collections.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link ImmutableJdk9Collections ImmutableJdk9Collections}.
*
* ImmutableJdk9Collections.builder()
* .addS|addAllS(String) // {@link Jdk9Collections#s() s} elements
* .addL|addAllL(String) // {@link Jdk9Collections#l() l} elements
* .putM|putAllM(String => int) // {@link Jdk9Collections#m() m} mappings
* .build();
*
* @return A new ImmutableJdk9Collections builder
*/
public static ImmutableJdk9Collections.Builder builder() {
return new ImmutableJdk9Collections.Builder();
}
/**
* Builds instances of type {@link ImmutableJdk9Collections ImmutableJdk9Collections}.
* 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.
*/
@Generated(from = "Jdk9Collections", generator = "Immutables")
@NotThreadSafe
public static final class Builder {
private List s = new ArrayList();
private List l = new ArrayList();
private Map m = new LinkedHashMap();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code Jdk9Collections} instance.
* Regular attribute values will be replaced with those from the given instance.
* Absent optional values will not replace present values.
* Collection elements and entries will be added, not replaced.
* @param instance The instance from which to copy values
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder from(Jdk9Collections instance) {
Objects.requireNonNull(instance, "instance");
addAllS(instance.s());
addAllL(instance.l());
putAllM(instance.m());
return this;
}
/**
* Adds one element to {@link Jdk9Collections#s() s} set.
* @param element A s element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addS(String element) {
this.s.add(Objects.requireNonNull(element, "s element"));
return this;
}
/**
* Adds elements to {@link Jdk9Collections#s() s} set.
* @param elements An array of s elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addS(String... elements) {
for (String element : elements) {
this.s.add(Objects.requireNonNull(element, "s element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link Jdk9Collections#s() s} set.
* @param elements An iterable of s elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder s(Iterable elements) {
this.s.clear();
return addAllS(elements);
}
/**
* Adds elements to {@link Jdk9Collections#s() s} set.
* @param elements An iterable of s elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addAllS(Iterable elements) {
for (String element : elements) {
this.s.add(Objects.requireNonNull(element, "s element"));
}
return this;
}
/**
* Adds one element to {@link Jdk9Collections#l() l} list.
* @param element A l element
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addL(String element) {
this.l.add(Objects.requireNonNull(element, "l element"));
return this;
}
/**
* Adds elements to {@link Jdk9Collections#l() l} list.
* @param elements An array of l elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addL(String... elements) {
for (String element : elements) {
this.l.add(Objects.requireNonNull(element, "l element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link Jdk9Collections#l() l} list.
* @param elements An iterable of l elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder l(Iterable elements) {
this.l.clear();
return addAllL(elements);
}
/**
* Adds elements to {@link Jdk9Collections#l() l} list.
* @param elements An iterable of l elements
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder addAllL(Iterable elements) {
for (String element : elements) {
this.l.add(Objects.requireNonNull(element, "l element"));
}
return this;
}
/**
* Put one entry to the {@link Jdk9Collections#m() m} map.
* @param key The key in the m map
* @param value The associated value in the m map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putM(String key, int value) {
this.m.put(
Objects.requireNonNull(key, "m key"),
value);
return this;
}
/**
* Put one entry to the {@link Jdk9Collections#m() m} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putM(Map.Entry entry) {
String k = entry.getKey();
Integer v = entry.getValue();
this.m.put(
Objects.requireNonNull(k, "m key"),
Objects.requireNonNull(v, v == null ? "m value for key: " + k : null));
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link Jdk9Collections#m() m} map. Nulls are not permitted
* @param entries The entries that will be added to the m map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder m(Map entries) {
this.m.clear();
return putAllM(entries);
}
/**
* Put all mappings from the specified map as entries to {@link Jdk9Collections#m() m} map. Nulls are not permitted
* @param entries The entries that will be added to the m map
* @return {@code this} builder for use in a chained invocation
*/
@CanIgnoreReturnValue
public final Builder putAllM(Map entries) {
for (Map.Entry e : entries.entrySet()) {
String k = e.getKey();
Integer v = e.getValue();
this.m.put(
Objects.requireNonNull(k, "m key"),
Objects.requireNonNull(v, v == null ? "m value for key: " + k : null));
}
return this;
}
/**
* Builds a new {@link ImmutableJdk9Collections ImmutableJdk9Collections}.
* @return An immutable instance of Jdk9Collections
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public ImmutableJdk9Collections build() {
return new ImmutableJdk9Collections(Set.copyOf(s), List.copyOf(l), Map.copyOf(m));
}
}
}