/*
 * This file is part of Bisq.
 *
 * Bisq is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or (at
 * your option) any later version.
 *
 * Bisq is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Bisq. If not, see <http://www.gnu.org/licenses/>.
 */

package bisq.common.proto.persistable;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Delegate;

@EqualsAndHashCode
public abstract class PersistableList<T extends PersistablePayload> implements PersistableEnvelope, Iterable<T> {
    @Delegate(excludes = ExcludesDelegateMethods.class)
    @Getter
    @Setter
    private List<T> list;

    public PersistableList() {
        list = new ArrayList<>();
    }

    public PersistableList(List<T> list) {
        this.list = list;
    }

    // this.stream() does not compile for unknown reasons, so add that manual delegate method
    public Stream<T> stream() {
        return list.stream();
    }

    private interface ExcludesDelegateMethods<T> {
        Stream<T> stream();
    }

    @Override
    public String toString() {
        return "PersistableList{" +
                "\n     list=" + list +
                "\n}";
    }
}

@@ -760,20 +760,85 @@
 va.util.
-List
+Collection;%0Aimport java.util.List;%0Aimport java.util.function.Consumer
 ;%0Aimport
@@ -922,67 +922,8 @@
 ter;
-%0Aimport lombok.Setter;%0Aimport lombok.experimental.Delegate;
 %0A%0A@E
@@ -1041,129 +1041,148 @@
 lope
-, Iterable%3CT%3E
  %7B%0A
+%0A
     @
-Delegate(excludes = ExcludesDelegateMethods.class)%0A    @Getter%0A    @Setter%0A    private List%3CT%3E list;
+Getter%0A    private final List%3CT%3E list = createList();%0A%0A    protected List%3CT%3E createList() %7B%0A        return new ArrayList%3C%3E();%0A    %7D
 %0A%0A  
@@ -1218,512 +1218,869 @@
     
+%7D%0A%0A
     
-list = new ArrayList%3C%3E();%0A    %7D%0A%0A    public PersistableList(List%3CT%3E list) %7B%0A        this.list = list;%0A    %7D%0A%0A    // this.stream() does not compile for unknown reasons, so add that manual delegate method%0A    public Stream%3CT%3E stream() %7B%0A        return list.stream();%0A    %7D%0A%0A    private interface ExcludesDelegateMethods%3CT%3E %7B%0A        Stream%3CT%3E stream();%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A        return %22PersistableList%7B%22 +
+public PersistableList(List%3CT%3E list) %7B%0A        setAll(list);%0A    %7D%0A%0A    public void setAll(Collection%3CT%3E collection) %7B%0A        this.list.clear();%0A        this.list.addAll(collection);%0A    %7D%0A%0A    public boolean add(T item) %7B%0A        if (!list.contains(item)) %7B%0A            list.add(item);%0A            return true;%0A        %7D%0A        return false;%0A    %7D%0A%0A    public boolean remove(T tradable) %7B%0A        return list.remove(tradable);%0A    %7D%0A%0A    public Stream%3CT%3E stream() %7B%0A        return list.stream();%0A    %7D%0A%0A    public int size() %7B%0A        return list.size();%0A    %7D%0A%0A    public boolean contains(T thing) %7B%0A        return list.contains(thing);%0A    %7D%0A%0A    public boolean isEmpty() %7B%0A        return list.isEmpty();
 %0A    
+%7D%0A%0A
     
-        %22%5Cn     list=%22 + list +%0A                %22%5Cn%7D%22
+public void forEach(Consumer%3C? super T%3E action) %7B%0A        list.forEach(action);%0A    %7D%0A%0A    public void clear() %7B%0A        list.clear()
 ;%0A  

Remove lombok Delegate from PersistableList Add final Add setAll method Remove Iterable interface