PageRenderTime 24ms CodeModel.GetById 12ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/hazelcast/src/main/java/com/hazelcast/impl/base/Pairs.java

https://bitbucket.org/gabral6_gmailcom/hazelcast
Java | 92 lines | 57 code | 14 blank | 21 comment | 10 complexity | 81d0fbe600baf64b757b83383db29a8b MD5 | raw file
 1/*
 2 * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
 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
17package com.hazelcast.impl.base;
18
19import com.hazelcast.nio.DataSerializable;
20
21import java.io.DataInput;
22import java.io.DataOutput;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.List;
26
27public class Pairs implements DataSerializable {
28    private List<KeyValue> lsKeyValues = null;
29
30    public Pairs() {
31    }
32
33    public Pairs(int size) {
34        lsKeyValues = new ArrayList<KeyValue>(size);
35    }
36
37    public void addKeyValue(KeyValue keyValue) {
38        if (getKeyValues() == null) {
39            setKeyValues(new ArrayList<KeyValue>());
40        }
41        getKeyValues().add(keyValue);
42    }
43
44    public void writeData(DataOutput out) throws IOException {
45        int size = (getKeyValues() == null) ? 0 : getKeyValues().size();
46        out.writeInt(size);
47        for (int i = 0; i < size; i++) {
48            getKeyValues().get(i).writeData(out);
49        }
50    }
51
52    public void readData(DataInput in) throws IOException {
53        int size = in.readInt();
54        for (int i = 0; i < size; i++) {
55            if (getKeyValues() == null) {
56                setKeyValues(new ArrayList<KeyValue>());
57            }
58            KeyValue kv = new KeyValue();
59            kv.readData(in);
60            getKeyValues().add(kv);
61        }
62    }
63
64    public int size() {
65        return (getKeyValues() == null) ? 0 : getKeyValues().size();
66    }
67
68    public KeyValue getEntry(int i) {
69        return (getKeyValues() == null) ? null : getKeyValues().get(i);
70    }
71
72    /**
73     * @param lsKeyValues the lsKeyValues to set
74     */
75    public void setKeyValues(List<KeyValue> lsKeyValues) {
76        this.lsKeyValues = lsKeyValues;
77    }
78
79    /**
80     * @return the lsKeyValues
81     */
82    public List<KeyValue> getKeyValues() {
83        return lsKeyValues;
84    }
85
86    @Override
87    public String toString() {
88        return "Pairs{" +
89                "lsKeyValues=" + ((lsKeyValues == null) ? 0 : lsKeyValues.size()) +
90                '}';
91    }
92}