com.hazelcast.spi.impl.SerializableCollection Maven / Gradle / Ivy
/*
* Copyright (c) 2008-2015, 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.spi.impl;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
public final class SerializableCollection implements IdentifiedDataSerializable, Iterable {
private Collection collection;
public SerializableCollection() {
}
public SerializableCollection(Collection collection) {
this.collection = collection;
}
public SerializableCollection(Data... dataArray) {
this.collection = new ArrayList(dataArray.length);
Collections.addAll(collection, dataArray);
}
public Collection getCollection() {
return collection;
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
if (collection == null) {
out.writeInt(-1);
return;
}
out.writeInt(collection.size());
for (Data data : collection) {
out.writeData(data);
}
}
@Override
public void readData(ObjectDataInput in) throws IOException {
int size = in.readInt();
if (size == -1) {
return;
}
collection = new ArrayList(size);
for (int i = 0; i < size; i++) {
collection.add(in.readData());
}
}
@Override
public int getFactoryId() {
return SpiDataSerializerHook.F_ID;
}
@Override
public int getId() {
return SpiDataSerializerHook.COLLECTION;
}
@Override
public Iterator iterator() {
final Iterator iterator = collection == null ? null : collection.iterator();
return new Iterator() {
@Override
public boolean hasNext() {
return iterator != null && iterator.hasNext();
}
@Override
public Data next() {
return iterator.next();
}
@Override
public void remove() {
iterator.remove();
}
};
}
public int size() {
return collection == null ? 0 : collection.size();
}
}