/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. package com.hazelcast.impl.base;
  17. import com.hazelcast.nio.DataSerializable;
  18. import java.io.DataInput;
  19. import java.io.DataOutput;
  20. import java.io.IOException;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. public class Pairs implements DataSerializable {
  24. private List<KeyValue> lsKeyValues = null;
  25. public Pairs() {
  26. }
  27. public Pairs(int size) {
  28. lsKeyValues = new ArrayList<KeyValue>(size);
  29. }
  30. public void addKeyValue(KeyValue keyValue) {
  31. if (getKeyValues() == null) {
  32. setKeyValues(new ArrayList<KeyValue>());
  33. }
  34. getKeyValues().add(keyValue);
  35. }
  36. public void writeData(DataOutput out) throws IOException {
  37. int size = (getKeyValues() == null) ? 0 : getKeyValues().size();
  38. out.writeInt(size);
  39. for (int i = 0; i < size; i++) {
  40. getKeyValues().get(i).writeData(out);
  41. }
  42. }
  43. public void readData(DataInput in) throws IOException {
  44. int size = in.readInt();
  45. for (int i = 0; i < size; i++) {
  46. if (getKeyValues() == null) {
  47. setKeyValues(new ArrayList<KeyValue>());
  48. }
  49. KeyValue kv = new KeyValue();
  50. kv.readData(in);
  51. getKeyValues().add(kv);
  52. }
  53. }
  54. public int size() {
  55. return (getKeyValues() == null) ? 0 : getKeyValues().size();
  56. }
  57. public KeyValue getEntry(int i) {
  58. return (getKeyValues() == null) ? null : getKeyValues().get(i);
  59. }
  60. /**
  61. * @param lsKeyValues the lsKeyValues to set
  62. */
  63. public void setKeyValues(List<KeyValue> lsKeyValues) {
  64. this.lsKeyValues = lsKeyValues;
  65. }
  66. /**
  67. * @return the lsKeyValues
  68. */
  69. public List<KeyValue> getKeyValues() {
  70. return lsKeyValues;
  71. }
  72. @Override
  73. public String toString() {
  74. return "Pairs{" +
  75. "lsKeyValues=" + ((lsKeyValues == null) ? 0 : lsKeyValues.size()) +
  76. '}';
  77. }
  78. }