Skip to content

Instantly share code, notes, and snippets.

@ZiiSolutions
Created May 1, 2018 12:24
Show Gist options
  • Save ZiiSolutions/8a9afb587126fe679f5989b046af5997 to your computer and use it in GitHub Desktop.
Save ZiiSolutions/8a9afb587126fe679f5989b046af5997 to your computer and use it in GitHub Desktop.
package com.pressassociation.explore.api;
import com.pressassociation.common.annotations.UsedReflectively;
import com.pressassociation.common.base.MoreCollectors;
import com.pressassociation.content.ninjs.Ninjs;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Stream;
import javax.annotation.Nullable;
import static com.pressassociation.common.base.MoreOptionals.absent;
import static com.pressassociation.common.base.MorePreconditions.checkNonNegative;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;
/**
* Models representing dashboard summary ninjs response from Explore API.
*/
@AutoValue
@JsonDeserialize(builder = DashboardSummary.Builder.class)
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder(alphabetic = true)
public abstract class DashboardSummary {
/**
* Creates a builder of {@link DashboardSummary} instances.
*
* @return the new builder
*/
public static Builder builder() {
return Builder.create();
}
/**
* Creates an {@link DashboardSummary} based on the provided iterable set of items.
*
* @param items the items in the list
* @return the new item list
*/
public static DashboardSummary copyOf(Iterable<Ninjs> items) {
ImmutableList<Ninjs> immutableItems = ImmutableList.copyOf(items);
return builder()
.items(immutableItems)
.offset(0)
.total(immutableItems.size())
.build();
}
/**
* Creates an {@link DashboardSummary} based on the provided iterable set of items.
*
* @param items the items in the list
* @return the new item list
*/
public static DashboardSummary copyOf(Stream<Ninjs> items) {
return copyOf(items.collect(MoreCollectors.toImmutableList()));
}
/**
* Creates an {@link DashboardSummary} based on the provided items.
*
* @param items the items in the list
* @return the new item list
*/
public static DashboardSummary of(Ninjs... items) {
return builder()
.items(ImmutableList.copyOf(items))
.offset(0)
.total(items.length)
.build();
}
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private static void checkUnconfigured(Optional<?> value, String label) {
checkState(!value.isPresent(), "%s already configured as <%s>", label, value);
}
/**
* Builder of {@link DashboardSummary} instances.
*/
@AutoValue.Builder
@JsonIgnoreProperties("links")
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
public abstract static class Builder {
/**
* Creates a builder of {@link DashboardSummary} instances.
*
* @return the new builder
*/
@JsonCreator
static Builder create() {
return new AutoValue_DashboardSummary.Builder();
}
/**
* Builder constructor with default values.
*/
protected Builder() {
limit(absent());
offset(absent());
total(absent());
}
/**
* Builds a new {@link DashboardSummary} instance as described by the builder.
*
* @return the new instance
*/
abstract DashboardSummary autoBuild();
/**
* Sets the events property.
*
* @param value the new events property
* @return the builder
*/
@JsonIgnore
public abstract Builder events(ImmutableMap<String, Event> value);
/**
* The configured immutable items property.
*
* @return the configured value
*/
@JsonIgnore
abstract ImmutableList<Ninjs> immutableItems();
/**
* Sets the immutable items property.
*
* @param value the new immutableItems property
* @return the builder
*/
@JsonIgnore
abstract Builder immutableItems(ImmutableList<Ninjs> value);
/**
* The configured limit property.
*
* @return the configured value
*/
@JsonIgnore
abstract Optional<Integer> limit();
/**
* Sets the limit property.
*
* @param value the new limit property
* @return the builder
*/
@JsonIgnore
abstract Builder limit(Optional<Integer> value);
/**
* The configured offset property.
*
* @return the configured value
*/
@JsonIgnore
abstract Optional<Integer> offset();
/**
* Sets the offset property.
*
* @param value the new offset property
* @return the builder
*/
@JsonIgnore
abstract Builder offset(Optional<Integer> value);
/**
* The configured total property.
*
* @return the configured value
*/
@JsonIgnore
abstract Optional<Integer> total();
/**
* Sets the total property.
*
* @param value the new total property
* @return the builder
*/
@JsonIgnore
abstract Builder total(Optional<Integer> value);
/**
* Builds a new {@link DashboardSummary} instance as described by the builder.
*
* @return the new instance
*/
public DashboardSummary build() {
if (nullableImmutableItems() == null) {
immutableItems(ImmutableList.of());
}
return autoBuild();
}
/**
* Sets the items property.
*
* @param item the first item
* @param additionalItems any additional items
* @return the builder
*/
public Builder items(Ninjs item, Ninjs... additionalItems) {
checkNotNull(item, "item must not be null");
checkNotNull(additionalItems, "additionalItems must not be null");
checkImmutableItemsState();
return immutableItems(ImmutableList.copyOf(Lists.asList(item, additionalItems)));
}
/**
* Sets the items property.
*
* @param items the new items
* @return the builder
*/
@JsonIgnore
public Builder items(Iterable<Ninjs> items) {
checkNotNull(items, "items must not be null");
checkImmutableItemsState();
return immutableItems(ImmutableList.copyOf(items));
}
/**
* Sets the items property.
*
* @param items the new items
* @return the builder
*/
@JsonIgnore
public Builder items(Iterator<Ninjs> items) {
checkNotNull(items, "items must not be null");
checkImmutableItemsState();
return immutableItems(ImmutableList.copyOf(items));
}
/**
* Sets the items property.
*
* @param items the new items
* @return the builder
*/
@JsonIgnore
public Builder items(Stream<Ninjs> items) {
checkNotNull(items, "items must not be null");
checkImmutableItemsState();
return immutableItems(ImmutableList.copyOf(items.iterator()));
}
/**
* Sets the events property.
*
* @param events the events map
* @return the builder
*/
@JsonProperty
public Builder events(Map<String, Event> events) {
checkNotNull(events, "events must not be null");
return events(ImmutableMap.copyOf(events));
}
/**
* Sets the limit property.
*
* @param limit the new limit value
* @return the builder
*/
@JsonProperty
public Builder limit(int limit) {
checkNonNegative(limit, "limit must not be negative but was <%s>", limit);
checkUnconfigured(limit(), "limit");
return limit(Optional.of(limit));
}
/**
* Sets the offset property.
*
* @param offset the new offset value
* @return the builder
*/
@JsonProperty
public Builder offset(int offset) {
checkNonNegative(offset, "offset must not be negative but was <%s>", offset);
checkUnconfigured(offset(), "offset");
return offset(Optional.of(offset));
}
/**
* Sets the total property.
*
* @param total the new total value
* @return the builder
*/
@JsonProperty
public Builder total(int total) {
checkNonNegative(total, "total must not be negative but was <%s>", total);
checkUnconfigured(total(), "total");
return total(Optional.of(total));
}
private void checkImmutableItemsState() {
ImmutableList<Ninjs> currentValue = nullableImmutableItems();
checkState(currentValue == null, "items already configured as <%s>", currentValue);
}
@JsonProperty("item")
private Builder mutableItems(List<Ninjs> value) {
// TODO(somebody): should this really be deserialising to 'item'
return immutableItems(ImmutableList.copyOf(value));
}
@Nullable
private ImmutableList<Ninjs> nullableImmutableItems() {
try {
return immutableItems();
} catch (IllegalStateException ignored) {
// TODO(somebody): Determine if there is a better approach, catching the exception is untidy
// AutoValue throws IllegalStateException if accessor is called before it has been configured
}
return null;
}
}
@JsonIgnore
@UsedReflectively
abstract ImmutableMap<String, Event> events();
/**
* The configured immutable items.
*
* @return an immutable list of items
*/
@JsonIgnore
@UsedReflectively
abstract ImmutableList<Ninjs> immutableItems();
/**
* The limit of this batch.
*
* @return an optional integer of the limit
*/
@JsonIgnore
@UsedReflectively
abstract Optional<Integer> limit();
/**
* The offset for this batch.
*
* @return an optional integer of the offset
*/
@JsonIgnore
@UsedReflectively
abstract Optional<Integer> offset();
/**
* The total number of items.
*
* @return an optional integer of the total
*/
@JsonIgnore
@UsedReflectively
abstract Optional<Integer> total();
@JsonProperty("events")
@Nullable
public ImmutableMap<String, Event> getEvents() {
return events();
}
/**
* The items.
*/
@JsonProperty("item")
public ImmutableList<Ninjs> getItems() {
return immutableItems();
}
/**
* The limit of this batch.
*
* @return null if not present otherwise the integer limit of this batch
*/
@JsonProperty("limit")
@Nullable
public Integer getLimit() {
return limit().orElse(null);
}
/**
* The offset of this batch.
*
* @return null if not present otherwise the integer offset of this batch
*/
@JsonProperty("offset")
@Nullable
public Integer getOffset() {
return offset().orElse(null);
}
/**
* The total number of items matched by the query.
*
* @return null if not present otherwise the total number of items
*/
@JsonProperty("total")
@Nullable
public Integer getTotal() {
return total().orElse(null);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment