001// Generated by delombok at Mon Jan 02 16:27:27 UTC 2023
002package io.cui.tools.collect;
003
004import java.io.Serializable;
005import java.util.ArrayList;
006import java.util.Collection;
007import java.util.Collections;
008import java.util.List;
009
010/**
011 * <h2>Overview</h2>
012 * Default implementation of {@link PartialCollection} based on {@link ArrayList}.
013 * <h3>Usage</h3>
014 * <p>
015 * See {@link PartialArrayList#of(List, int)}
016 * </p>
017 *
018 * @param <T> at least {@link Serializable}
019 */
020public class PartialArrayList<T extends Serializable> extends ArrayList<T> implements PartialCollection<T> {
021    private static final long serialVersionUID = -7548645400982124555L;
022    private final boolean moreAvailable;
023
024    /**
025     * Default constructor.
026     *
027     * @param list the list of entities to store.
028     * @param moreAvailable the flag to store.
029     */
030    public PartialArrayList(Collection<T> list, boolean moreAvailable) {
031        super(list);
032        this.moreAvailable = moreAvailable;
033    }
034
035    /**
036     * Static constructor for an empty instance.
037     *
038     * @param <T>
039     * @return an empty {@link PartialArrayList}.
040     */
041    public static <T extends Serializable> PartialArrayList<T> emptyList() {
042        return new PartialArrayList<>(Collections.emptyList(), false);
043    }
044
045    /**
046     * Convenience method for creating a {@link PartialArrayList} as sublist for the given
047     * collection with setting the {@link PartialCollection#isMoreAvailable()} automatically
048     *
049     * @param full the complete List to be wrapped, may be larger than the limit. If so, a sublist
050     *            will be used.
051     * @param limit to be checked against
052     *
053     * @param <T> identifying the type of contained elements
054     * @return a newly created {@link PartialArrayList}.
055     */
056    public static <T extends Serializable> PartialArrayList<T> of(List<T> full, int limit) {
057        if (MoreCollections.isEmpty(full)) {
058            return emptyList();
059        }
060        int actualSize = full.size();
061        if (actualSize <= limit) {
062            return new PartialArrayList<>(full, false);
063        } else {
064            return new PartialArrayList<>(full.subList(0, limit), true);
065        }
066    }
067
068    @java.lang.Override
069    @java.lang.SuppressWarnings("all")
070    @lombok.Generated
071    public boolean equals(final java.lang.Object o) {
072        if (o == this) return true;
073        if (!(o instanceof PartialArrayList)) return false;
074        final PartialArrayList<?> other = (PartialArrayList<?>) o;
075        if (!other.canEqual((java.lang.Object) this)) return false;
076        if (!super.equals(o)) return false;
077        if (this.isMoreAvailable() != other.isMoreAvailable()) return false;
078        return true;
079    }
080
081    @java.lang.SuppressWarnings("all")
082    @lombok.Generated
083    protected boolean canEqual(final java.lang.Object other) {
084        return other instanceof PartialArrayList;
085    }
086
087    @java.lang.Override
088    @java.lang.SuppressWarnings("all")
089    @lombok.Generated
090    public int hashCode() {
091        final int PRIME = 59;
092        int result = super.hashCode();
093        result = result * PRIME + (this.isMoreAvailable() ? 79 : 97);
094        return result;
095    }
096
097    @java.lang.Override
098    @java.lang.SuppressWarnings("all")
099    @lombok.Generated
100    public java.lang.String toString() {
101        return "PartialArrayList(super=" + super.toString() + ", moreAvailable=" + this.isMoreAvailable() + ")";
102    }
103
104    @java.lang.SuppressWarnings("all")
105    @lombok.Generated
106    public boolean isMoreAvailable() {
107        return this.moreAvailable;
108    }
109}