All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hazelcast.multimap.impl.operations.EntrySetResponse Maven / Gradle / Ivy

There is a newer version: 5.0-BETA-1
Show newest version
/*
 * Copyright (c) 2008-2017, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.hazelcast.multimap.impl.operations;

import com.hazelcast.multimap.impl.MultiMapDataSerializerHook;
import com.hazelcast.multimap.impl.MultiMapRecord;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import com.hazelcast.spi.NodeEngine;

import java.io.IOException;
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

public class EntrySetResponse implements IdentifiedDataSerializable {

    private Map> map;

    public EntrySetResponse() {
    }

    public EntrySetResponse(Map> map, NodeEngine nodeEngine) {
        this.map = new HashMap>(map.size());
        for (Map.Entry> entry : map.entrySet()) {
            Collection records = entry.getValue();
            Collection coll = new ArrayList(records.size());
            for (MultiMapRecord record : records) {
                coll.add(nodeEngine.toData(record.getObject()));
            }
            this.map.put(entry.getKey(), coll);
        }
    }

    public Set> getDataEntrySet() {
        Set> entrySet = new HashSet>();
        for (Map.Entry> entry : map.entrySet()) {
            Data key = entry.getKey();
            Collection coll = entry.getValue();
            for (Data data : coll) {
                entrySet.add(new AbstractMap.SimpleEntry(key, data));
            }
        }
        return entrySet;
    }

    public  Set> getObjectEntrySet(NodeEngine nodeEngine) {
        Set> entrySet = new HashSet>();
        for (Map.Entry> entry : map.entrySet()) {
            K key = nodeEngine.toObject(entry.getKey());
            Collection coll = entry.getValue();
            for (Data data : coll) {
                V val = nodeEngine.toObject(data);
                entrySet.add(new AbstractMap.SimpleEntry(key, val));
            }
        }
        return entrySet;
    }

    @Override
    public void writeData(ObjectDataOutput out) throws IOException {
        out.writeInt(map.size());
        for (Map.Entry> entry : map.entrySet()) {
            out.writeData(entry.getKey());
            Collection coll = entry.getValue();
            out.writeInt(coll.size());
            for (Data data : coll) {
                out.writeData(data);
            }
        }
    }

    @Override
    public void readData(ObjectDataInput in) throws IOException {
        int size = in.readInt();
        map = new HashMap>(size);
        for (int i = 0; i < size; i++) {
            Data key = in.readData();
            int collSize = in.readInt();
            Collection coll = new ArrayList(collSize);
            for (int j = 0; j < collSize; j++) {
                coll.add(in.readData());
            }
            map.put(key, coll);
        }
    }

    @Override
    public int getFactoryId() {
        return MultiMapDataSerializerHook.F_ID;
    }

    @Override
    public int getId() {
        return MultiMapDataSerializerHook.ENTRY_SET_RESPONSE;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy