com.io7m.changelog.core.CChangelog Maven / Gradle / Ivy
Show all versions of io7m-changelog-core Show documentation
package com.io7m.changelog.core;
import java.net.URI;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import javax.annotation.Generated;
/**
* The type of changelogs.
*/
@SuppressWarnings({"all"})
@Generated({"Immutables.generator", "CChangelogType"})
public final class CChangelog implements CChangelogType {
private final String project;
private final List releases;
private final Map ticketSystems;
private CChangelog(
String project,
Iterable extends CRelease> releases,
Map ticketSystems) {
this.project = Objects.requireNonNull(project, "project");
this.releases = createUnmodifiableList(false, createSafeList(releases, true, false));
this.ticketSystems = createUnmodifiableMap(true, false, ticketSystems);
}
private CChangelog(
CChangelog original,
String project,
List releases,
Map ticketSystems) {
this.project = project;
this.releases = releases;
this.ticketSystems = ticketSystems;
}
/**
* @return The project name
*/
@Override
public String project() {
return project;
}
/**
* @return The list of releases
*/
@Override
public List releases() {
return releases;
}
/**
* @return The ticket systems
*/
@Override
public Map ticketSystems() {
return ticketSystems;
}
/**
* Copy the current immutable object by setting a value for the {@link CChangelogType#project() project} attribute.
* An equals check used to prevent copying of the same value by returning {@code this}.
* @param value A new value for project
* @return A modified copy of the {@code this} object
*/
public final CChangelog withProject(String value) {
if (this.project.equals(value)) return this;
String newValue = Objects.requireNonNull(value, "project");
return new CChangelog(this, newValue, this.releases, this.ticketSystems);
}
/**
* Copy the current immutable object with elements that replace the content of {@link CChangelogType#releases() releases}.
* @param elements The elements to set
* @return A modified copy of {@code this} object
*/
public final CChangelog withReleases(CRelease... elements) {
List newValue = createUnmodifiableList(false, createSafeList(Arrays.asList(elements), true, false));
return new CChangelog(this, this.project, newValue, this.ticketSystems);
}
/**
* Copy the current immutable object with elements that replace the content of {@link CChangelogType#releases() releases}.
* A shallow reference equality check is used to prevent copying of the same value by returning {@code this}.
* @param elements An iterable of releases elements to set
* @return A modified copy of {@code this} object
*/
public final CChangelog withReleases(Iterable extends CRelease> elements) {
if (this.releases == elements) return this;
List newValue = createUnmodifiableList(false, createSafeList(elements, true, false));
return new CChangelog(this, this.project, newValue, this.ticketSystems);
}
/**
* Copy the current immutable object by replacing the {@link CChangelogType#ticketSystems() ticketSystems} 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 ticketSystems map
* @return A modified copy of {@code this} object
*/
public final CChangelog withTicketSystems(Map entries) {
if (this.ticketSystems == entries) return this;
Map newValue = createUnmodifiableMap(true, false, entries);
return new CChangelog(this, this.project, this.releases, newValue);
}
/**
* This instance is equal to all instances of {@code CChangelog} that have equal attribute values.
* @return {@code true} if {@code this} is equal to {@code another} instance
*/
@Override
public boolean equals(Object another) {
if (this == another) return true;
return another instanceof CChangelog
&& equalTo((CChangelog) another);
}
private boolean equalTo(CChangelog another) {
return project.equals(another.project)
&& releases.equals(another.releases)
&& ticketSystems.equals(another.ticketSystems);
}
/**
* Computes a hash code from attributes: {@code project}, {@code releases}, {@code ticketSystems}.
* @return hashCode value
*/
@Override
public int hashCode() {
int h = 31;
h = h * 17 + project.hashCode();
h = h * 17 + releases.hashCode();
h = h * 17 + ticketSystems.hashCode();
return h;
}
/**
* Prints the immutable value {@code CChangelog} with attribute values.
* @return A string representation of the value
*/
@Override
public String toString() {
return "CChangelog{"
+ "project=" + project
+ ", releases=" + releases
+ ", ticketSystems=" + ticketSystems
+ "}";
}
/**
* Construct a new immutable {@code CChangelog} instance.
* @param project The value for the {@code project} attribute
* @param releases The value for the {@code releases} attribute
* @param ticketSystems The value for the {@code ticketSystems} attribute
* @return An immutable CChangelog instance
*/
public static CChangelog of(String project, List releases, Map ticketSystems) {
return of(project, (Iterable extends CRelease>) releases, ticketSystems);
}
/**
* Construct a new immutable {@code CChangelog} instance.
* @param project The value for the {@code project} attribute
* @param releases The value for the {@code releases} attribute
* @param ticketSystems The value for the {@code ticketSystems} attribute
* @return An immutable CChangelog instance
*/
public static CChangelog of(String project, Iterable extends CRelease> releases, Map ticketSystems) {
return new CChangelog(project, releases, ticketSystems);
}
/**
* Creates an immutable copy of a {@link CChangelogType} 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 CChangelog instance
*/
public static CChangelog copyOf(CChangelogType instance) {
if (instance instanceof CChangelog) {
return (CChangelog) instance;
}
return CChangelog.builder()
.from(instance)
.build();
}
/**
* Creates a builder for {@link CChangelog CChangelog}.
* @return A new CChangelog builder
*/
public static CChangelog.Builder builder() {
return new CChangelog.Builder();
}
/**
* Builds instances of type {@link CChangelog CChangelog}.
* 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.
*/
public static final class Builder {
private static final long INIT_BIT_PROJECT = 0x1L;
private long initBits = 0x1L;
private String project;
private List releases = new ArrayList();
private Map ticketSystems = new LinkedHashMap();
private Builder() {
}
/**
* Fill a builder with attribute values from the provided {@code CChangelogType} 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
*/
public final Builder from(CChangelogType instance) {
Objects.requireNonNull(instance, "instance");
setProject(instance.project());
addAllReleases(instance.releases());
putAllTicketSystems(instance.ticketSystems());
return this;
}
/**
* Initializes the value for the {@link CChangelogType#project() project} attribute.
* @param project The value for project
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setProject(String project) {
this.project = Objects.requireNonNull(project, "project");
initBits &= ~INIT_BIT_PROJECT;
return this;
}
/**
* Adds one element to {@link CChangelogType#releases() releases} list.
* @param element A releases element
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addReleases(CRelease element) {
this.releases.add(Objects.requireNonNull(element, "releases element"));
return this;
}
/**
* Adds elements to {@link CChangelogType#releases() releases} list.
* @param elements An array of releases elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addReleases(CRelease... elements) {
for (CRelease element : elements) {
this.releases.add(Objects.requireNonNull(element, "releases element"));
}
return this;
}
/**
* Sets or replaces all elements for {@link CChangelogType#releases() releases} list.
* @param elements An iterable of releases elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setReleases(Iterable extends CRelease> elements) {
this.releases.clear();
return addAllReleases(elements);
}
/**
* Adds elements to {@link CChangelogType#releases() releases} list.
* @param elements An iterable of releases elements
* @return {@code this} builder for use in a chained invocation
*/
public final Builder addAllReleases(Iterable extends CRelease> elements) {
for (CRelease element : elements) {
this.releases.add(Objects.requireNonNull(element, "releases element"));
}
return this;
}
/**
* Put one entry to the {@link CChangelogType#ticketSystems() ticketSystems} map.
* @param key The key in the ticketSystems map
* @param value The associated value in the ticketSystems map
* @return {@code this} builder for use in a chained invocation
*/
public final Builder putTicketSystems(String key, URI value) {
this.ticketSystems.put(
Objects.requireNonNull(key, "ticketSystems key"),
Objects.requireNonNull(value, "ticketSystems value"));
return this;
}
/**
* Put one entry to the {@link CChangelogType#ticketSystems() ticketSystems} map. Nulls are not permitted
* @param entry The key and value entry
* @return {@code this} builder for use in a chained invocation
*/
public final Builder putTicketSystems(Map.Entry entry) {
String k = entry.getKey();
URI v = entry.getValue();
this.ticketSystems.put(
Objects.requireNonNull(k, "ticketSystems key"),
Objects.requireNonNull(v, "ticketSystems value"));
return this;
}
/**
* Sets or replaces all mappings from the specified map as entries for the {@link CChangelogType#ticketSystems() ticketSystems} map. Nulls are not permitted
* @param ticketSystems The entries that will be added to the ticketSystems map
* @return {@code this} builder for use in a chained invocation
*/
public final Builder setTicketSystems(Map ticketSystems) {
this.ticketSystems.clear();
return putAllTicketSystems(ticketSystems);
}
/**
* Put all mappings from the specified map as entries to {@link CChangelogType#ticketSystems() ticketSystems} map. Nulls are not permitted
* @param ticketSystems The entries that will be added to the ticketSystems map
* @return {@code this} builder for use in a chained invocation
*/
public final Builder putAllTicketSystems(Map ticketSystems) {
for (Map.Entry entry : ticketSystems.entrySet()) {
String k = entry.getKey();
URI v = entry.getValue();
this.ticketSystems.put(
Objects.requireNonNull(k, "ticketSystems key"),
Objects.requireNonNull(v, "ticketSystems value"));
}
return this;
}
/**
* Builds a new {@link CChangelog CChangelog}.
* @return An immutable instance of CChangelog
* @throws java.lang.IllegalStateException if any required attributes are missing
*/
public CChangelog build() {
if (initBits != 0) {
throw new IllegalStateException(formatRequiredAttributesMessage());
}
return new CChangelog(
null,
project,
createUnmodifiableList(true, releases),
createUnmodifiableMap(false, false, ticketSystems));
}
private String formatRequiredAttributesMessage() {
List attributes = new ArrayList();
if ((initBits & INIT_BIT_PROJECT) != 0) attributes.add("project");
return "Cannot build CChangelog, some of required attributes are not set " + attributes;
}
}
private static List createSafeList(Iterable extends T> iterable, boolean checkNulls, boolean skipNulls) {
ArrayList list;
if (iterable instanceof Collection>) {
int size = ((Collection>) iterable).size();
if (size == 0) return Collections.emptyList();
list = new ArrayList();
} else {
list = new ArrayList();
}
for (T element : iterable) {
if (skipNulls && element == null) continue;
if (checkNulls) Objects.requireNonNull(element, "element");
list.add(element);
}
return list;
}
private static List createUnmodifiableList(boolean clone, List list) {
switch(list.size()) {
case 0: return Collections.emptyList();
case 1: return Collections.singletonList(list.get(0));
default:
if (clone) {
return Collections.unmodifiableList(new ArrayList(list));
} else {
if (list instanceof ArrayList>) {
((ArrayList>) list).trimToSize();
}
return Collections.unmodifiableList(list);
}
}
}
private static Map createUnmodifiableMap(boolean checkNulls, boolean skipNulls, Map extends K, ? extends V> map) {
switch (map.size()) {
case 0: return Collections.emptyMap();
case 1: {
Map.Entry extends K, ? extends V> e = map.entrySet().iterator().next();
K k = e.getKey();
V v = e.getValue();
if (checkNulls) {
Objects.requireNonNull(k, "key");
Objects.requireNonNull(v, "value");
}
if (skipNulls && (k == null || v == null)) {
return Collections.emptyMap();
}
return Collections.singletonMap(k, v);
}
default: {
Map linkedMap = new LinkedHashMap(map.size());
if (skipNulls || checkNulls) {
for (Map.Entry extends K, ? extends V> e : map.entrySet()) {
K k = e.getKey();
V v = e.getValue();
if (skipNulls) {
if (k == null || v == null) continue;
} else if (checkNulls) {
Objects.requireNonNull(k, "key");
Objects.requireNonNull(v, "value");
}
linkedMap.put(k, v);
}
} else {
linkedMap.putAll(map);
}
return Collections.unmodifiableMap(linkedMap);
}
}
}
}