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

com.hazelcast.client.proxy.AbstractClientCollectionProxy Maven / Gradle / Ivy

There is a newer version: 3.6.8
Show newest version
/*
 * Copyright (c) 2008-2016, 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.client.proxy;

import com.hazelcast.client.impl.client.BaseClientRemoveListenerRequest;
import com.hazelcast.client.impl.client.ClientRequest;
import com.hazelcast.client.spi.ClientProxy;
import com.hazelcast.client.spi.EventHandler;
import com.hazelcast.collection.common.DataAwareItemEvent;
import com.hazelcast.collection.impl.collection.client.CollectionAddAllRequest;
import com.hazelcast.collection.impl.collection.client.CollectionAddListenerRequest;
import com.hazelcast.collection.impl.collection.client.CollectionAddRequest;
import com.hazelcast.collection.impl.collection.client.CollectionClearRequest;
import com.hazelcast.collection.impl.collection.client.CollectionCompareAndRemoveRequest;
import com.hazelcast.collection.impl.collection.client.CollectionContainsRequest;
import com.hazelcast.collection.impl.collection.client.CollectionGetAllRequest;
import com.hazelcast.collection.impl.collection.client.CollectionIsEmptyRequest;
import com.hazelcast.collection.impl.collection.client.CollectionRemoveListenerRequest;
import com.hazelcast.collection.impl.collection.client.CollectionRemoveRequest;
import com.hazelcast.collection.impl.collection.client.CollectionRequest;
import com.hazelcast.collection.impl.collection.client.CollectionSizeRequest;
import com.hazelcast.core.ICollection;
import com.hazelcast.core.ItemEvent;
import com.hazelcast.core.ItemEventType;
import com.hazelcast.core.ItemListener;
import com.hazelcast.core.Member;
import com.hazelcast.internal.serialization.SerializationService;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.spi.impl.PortableItemEvent;
import com.hazelcast.spi.impl.SerializableList;
import com.hazelcast.spi.impl.UnmodifiableLazyList;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * @ali 9/4/13
 */
public class AbstractClientCollectionProxy extends ClientProxy implements ICollection {

    protected final String partitionKey;

    public AbstractClientCollectionProxy(String serviceName, String name) {
        super(serviceName, name);
        partitionKey = getPartitionKey();
    }

    public int size() {
        CollectionSizeRequest request = new CollectionSizeRequest(getName());
        final Integer result = invoke(request);
        return result;
    }

    public boolean isEmpty() {
        CollectionIsEmptyRequest request = new CollectionIsEmptyRequest(getName());
        final Boolean result = invoke(request);
        return result;
    }

    public boolean contains(Object o) {
        throwExceptionIfNull(o);
        final CollectionContainsRequest request = new CollectionContainsRequest(getName(), toData(o));
        final Boolean result = invoke(request);
        return result;
    }

    public Iterator iterator() {
        return getAll().iterator();
    }

    public Object[] toArray() {
        return getAll().toArray();
    }

    public  T[] toArray(T[] a) {
        return getAll().toArray(a);
    }

    public boolean add(E e) {
        throwExceptionIfNull(e);
        final CollectionAddRequest request = new CollectionAddRequest(getName(), toData(e));
        final Boolean result = invoke(request);
        return result;
    }

    public boolean remove(Object o) {
        throwExceptionIfNull(o);
        final CollectionRemoveRequest request = new CollectionRemoveRequest(getName(), toData(o));
        final Boolean result = invoke(request);
        return result;
    }

    public boolean containsAll(Collection c) {
        throwExceptionIfNull(c);
        Set valueSet = new HashSet(c.size());
        for (Object o : c) {
            throwExceptionIfNull(o);
            valueSet.add(toData(o));
        }
        final CollectionContainsRequest request = new CollectionContainsRequest(getName(), valueSet);
        final Boolean result = invoke(request);
        return result;
    }

    public boolean addAll(Collection c) {
        throwExceptionIfNull(c);
        final List valueList = new ArrayList(c.size());
        for (E e : c) {
            throwExceptionIfNull(e);
            valueList.add(toData(e));
        }
        final CollectionAddAllRequest request = new CollectionAddAllRequest(getName(), valueList);
        final Boolean result = invoke(request);
        return result;
    }

    public boolean removeAll(Collection c) {
        return compareAndRemove(false, c);
    }

    public boolean retainAll(Collection c) {
        return compareAndRemove(true, c);
    }

    private boolean compareAndRemove(boolean retain, Collection c) {
        throwExceptionIfNull(c);
        final Set valueSet = new HashSet();
        for (Object o : c) {
            throwExceptionIfNull(o);
            valueSet.add(toData(o));
        }
        final CollectionCompareAndRemoveRequest request = new CollectionCompareAndRemoveRequest(getName(), valueSet, retain);
        final Boolean result = invoke(request);
        return result;
    }

    public void clear() {
        final CollectionClearRequest request = new CollectionClearRequest(getName());
        invoke(request);
    }

    public String addItemListener(final ItemListener listener, final boolean includeValue) {
        CollectionAddListenerRequest addRequest = new CollectionAddListenerRequest(getName(), includeValue);
        addRequest.setServiceName(getServiceName());
        EventHandler eventHandler = new EventHandler() {
            public void handle(PortableItemEvent portableItemEvent) {
                Data item = portableItemEvent.getItem();
                Member member = getContext().getClusterService().getMember(portableItemEvent.getUuid());
                ItemEventType eventType = portableItemEvent.getEventType();
                SerializationService serializationService = getContext().getSerializationService();
                ItemEvent itemEvent =
                        new DataAwareItemEvent(getName(), eventType, item, member, serializationService);
                if (eventType == ItemEventType.ADDED) {
                    listener.itemAdded(itemEvent);
                } else {
                    listener.itemRemoved(itemEvent);
                }
            }

            @Override
            public void beforeListenerRegister() {
            }

            @Override
            public void onListenerRegister() {

            }
        };
        BaseClientRemoveListenerRequest removeRequest =
                new CollectionRemoveListenerRequest(getName(), getServiceName());
        return registerListener(addRequest, removeRequest, eventHandler);
    }

    public boolean removeItemListener(String registrationId) {
        return deregisterListener(registrationId);
    }

    protected  T invoke(ClientRequest req) {
        if (req instanceof CollectionRequest) {
            CollectionRequest request = (CollectionRequest) req;
            request.setServiceName(getServiceName());
        }

        return super.invoke(req, getPartitionKey());
    }

    private Collection getAll() {
        CollectionGetAllRequest request = new CollectionGetAllRequest(getName());
        SerializableList result = invoke(request);
        List collection = result.getCollection();
        SerializationService serializationService = getContext().getSerializationService();
        return new UnmodifiableLazyList(collection, serializationService);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy