View Javadoc
1   /*
2    * Copyright 2016 The Lannister Project
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.anyflow.lannister.cluster;
17  
18  import java.io.IOException;
19  import java.util.HashSet;
20  
21  import com.google.common.collect.Lists;
22  import com.hazelcast.nio.ObjectDataInput;
23  import com.hazelcast.nio.ObjectDataOutput;
24  import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
25  
26  import net.anyflow.lannister.serialization.SerializableFactory;
27  
28  public class SerializableIntegerSet extends HashSet<Integer> implements IdentifiedDataSerializable {
29  	private static final long serialVersionUID = -2853172691112908250L;
30  	public static final int ID = 10;
31  
32  	public SerializableIntegerSet() {
33  	}
34  
35  	public SerializableIntegerSet(Integer... items) {
36  		this();
37  
38  		this.addAll(Lists.newArrayList(items));
39  	}
40  
41  	@Override
42  	public int getFactoryId() {
43  		return SerializableFactory.ID;
44  	}
45  
46  	@Override
47  	public int getId() {
48  		return ID;
49  	}
50  
51  	@Override
52  	public void writeData(ObjectDataOutput out) throws IOException {
53  		out.writeInt(size());
54  
55  		for (Integer item : this) {
56  			out.writeInt(item);
57  		}
58  	}
59  
60  	@Override
61  	public void readData(ObjectDataInput in) throws IOException {
62  		int size = in.readInt();
63  
64  		for (int i = 0; i < size; ++i) {
65  			this.add(in.readInt());
66  		}
67  	}
68  }