/*
 * 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.core.dao.governance.blindvote.storage;

import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.persistence.PersistableNetworkPayloadStore;

import com.google.protobuf.Message;

import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;


/**
 * We store only the payload in the PB file to save disc space. The hash of the payload can be created anyway and
 * is only used as key in the map. So we have a hybrid data structure which is represented as list in the protobuffer
 * definition and provide a hashMap for the domain access.
 */
@Slf4j
public class BlindVoteStore extends PersistableNetworkPayloadStore {

    BlindVoteStore() {
    }


    ///////////////////////////////////////////////////////////////////////////////////////////
    // PROTO BUFFER
    ///////////////////////////////////////////////////////////////////////////////////////////

    private BlindVoteStore(List<BlindVotePayload> list) {
        list.forEach(item -> map.put(new P2PDataStorage.ByteArray(item.getHash()), item));
    }

    public Message toProtoMessage() {
        return protobuf.PersistableEnvelope.newBuilder()
                .setBlindVoteStore(getBuilder())
                .build();
    }

    private protobuf.BlindVoteStore.Builder getBuilder() {
        final List<protobuf.BlindVotePayload> protoList = map.values().stream()
                .map(payload -> (BlindVotePayload) payload)
                .map(BlindVotePayload::toProtoBlindVotePayload)
                .collect(Collectors.toList());
        return protobuf.BlindVoteStore.newBuilder().addAllItems(protoList);
    }

    public static BlindVoteStore fromProto(protobuf.BlindVoteStore proto) {
        List<BlindVotePayload> list = proto.getItemsList().stream()
                .map(BlindVotePayload::fromProto).collect(Collectors.toList());
        return new BlindVoteStore(list);
    }

    public boolean containsKey(P2PDataStorage.ByteArray hash) {
        return map.containsKey(hash);
    }
}

@@ -732,56 +732,8 @@
 e;%0A%0A
-import bisq.network.p2p.storage.P2PDataStorage;%0A
 impo
@@ -804,16 +804,16 @@
 dStore;%0A
+
 %0Aimport 
@@ -1314,12 +1314,29 @@
 tore
+%3CBlindVotePayload%3E
  %7B%0A
-%0A
     
@@ -1645,88 +1645,18 @@
     
-list.forEach(item -%3E map.put(new P2PDataStorage.ByteArray(item.getHash()), item)
+super(list
 );%0A 
@@ -2447,32 +2447,32 @@
 tors.toList());%0A
+
         return n
@@ -2505,116 +2505,7 @@
    %7D
-%0A%0A    public boolean containsKey(P2PDataStorage.ByteArray hash) %7B%0A        return map.containsKey(hash);%0A    %7D
 %0A%7D%0A

Apply generics and super class methods