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

com.hazelcast.map.client.MapAddEntryListenerRequest Maven / Gradle / Ivy

/*
 * Copyright (c) 2008-2013, 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.map.client;

import com.hazelcast.map.MapPortableHook;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.nio.serialization.PortableReader;
import com.hazelcast.nio.serialization.PortableWriter;
import com.hazelcast.query.Predicate;
import java.io.IOException;

public class MapAddEntryListenerRequest extends AbstractMapAddEntryListenerRequest {

    private Predicate predicate;

    public MapAddEntryListenerRequest() {
        super();
    }

    public MapAddEntryListenerRequest(String name, boolean includeValue) {
        super(name, includeValue);
    }

    public MapAddEntryListenerRequest(String name, Data key, boolean includeValue) {
        super(name, key, includeValue);
    }

    public MapAddEntryListenerRequest(String name, Data key, boolean includeValue, Predicate predicate) {
        super(name, key, includeValue);
        this.predicate = predicate;
    }

    public int getClassId() {
        return MapPortableHook.ADD_ENTRY_LISTENER;
    }

    @Override
    protected Predicate getPredicate() {
        return predicate;
    }

    public void write(PortableWriter writer) throws IOException {
        writer.writeUTF("name", name);
        writer.writeBoolean("i", includeValue);

        final boolean hasKey = key != null;
        writer.writeBoolean("key", hasKey);
        if (predicate == null) {
            writer.writeBoolean("pre", false);
            if (hasKey) {
                final ObjectDataOutput out = writer.getRawDataOutput();
                key.writeData(out);
            }
        } else {
            writer.writeBoolean("pre", true);
            final ObjectDataOutput out = writer.getRawDataOutput();
            out.writeObject(predicate);
            if (hasKey) {
                key.writeData(out);
            }
        }
        super.write(writer);
    }

    public void read(PortableReader reader) throws IOException {
        name = reader.readUTF("name");
        includeValue = reader.readBoolean("i");

        boolean hasKey = reader.readBoolean("key");
        if (reader.readBoolean("pre")) {
            final ObjectDataInput in = reader.getRawDataInput();
            predicate = in.readObject();
            if (hasKey) {
                key = new Data();
                key.readData(in);
            }
        } else if (hasKey) {
            final ObjectDataInput in = reader.getRawDataInput();
            key = new Data();
            key.readData(in);
        }
        super.read(reader);
    }

    @Override
    public String getMethodName() {
        return "addEntryListener";
    }

    @Override
    public Object[] getParameters() {
        if (key == null && predicate == null) {
            return new Object[]{null, includeValue};
        } else if (predicate == null) {
            return new Object[]{null, key, includeValue};
        } else if (key == null) {
            return new Object[]{null, predicate, includeValue};
        }
        return new Object[]{null, predicate, key, includeValue};
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy