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

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 183 lines · 140 code · 28 blank · 15 comment · 27 complexity · b0ffa8d5feb68a04a60fcd80f17d1829 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.impl.concurrentmap.ValueHolder;
  18. import com.hazelcast.nio.Data;
  19. import com.hazelcast.nio.DataSerializable;
  20. import java.io.DataInput;
  21. import java.io.DataOutput;
  22. import java.io.IOException;
  23. import java.util.ArrayList;
  24. import java.util.Collection;
  25. import java.util.Iterator;
  26. import static com.hazelcast.nio.IOUtil.toObject;
  27. public class Values implements Collection, DataSerializable {
  28. Collection<Data> lsValues = null;
  29. public Values() {
  30. }
  31. public Values(Collection<ValueHolder> values) {
  32. super();
  33. if (values != null) {
  34. this.lsValues = new ArrayList<Data>(values.size());
  35. for (ValueHolder valueHolder : values) {
  36. if (valueHolder != null) {
  37. lsValues.add(valueHolder.getData());
  38. }
  39. }
  40. }
  41. }
  42. public boolean add(Object o) {
  43. throw new UnsupportedOperationException();
  44. }
  45. public boolean addAll(Collection c) {
  46. throw new UnsupportedOperationException();
  47. }
  48. public void clear() {
  49. throw new UnsupportedOperationException();
  50. }
  51. public boolean contains(Object o) {
  52. if (o == null) {
  53. throw new IllegalArgumentException("Contains cannot have null argument.");
  54. }
  55. Iterator it = iterator();
  56. while (it.hasNext()) {
  57. Object v = it.next();
  58. if (o.equals(v)) {
  59. return true;
  60. }
  61. }
  62. return false;
  63. }
  64. public boolean containsAll(Collection c) {
  65. throw new UnsupportedOperationException();
  66. }
  67. public boolean isEmpty() {
  68. return (size() == 0);
  69. }
  70. public Iterator iterator() {
  71. return new ValueIterator(lsValues.iterator());
  72. }
  73. class ValueIterator implements Iterator {
  74. final Iterator<Data> it;
  75. public ValueIterator(Iterator<Data> it) {
  76. super();
  77. this.it = it;
  78. }
  79. public boolean hasNext() {
  80. return it.hasNext();
  81. }
  82. public Object next() {
  83. Data value = it.next();
  84. return toObject(value);
  85. }
  86. public void remove() {
  87. it.remove();
  88. }
  89. }
  90. public boolean remove(Object o) {
  91. throw new UnsupportedOperationException();
  92. }
  93. public boolean removeAll(Collection c) {
  94. throw new UnsupportedOperationException();
  95. }
  96. public boolean retainAll(Collection c) {
  97. throw new UnsupportedOperationException();
  98. }
  99. public int size() {
  100. return (lsValues == null) ? 0 : lsValues.size();
  101. }
  102. public Object[] toArray() {
  103. if (size() == 0) {
  104. return null;
  105. }
  106. return toArray(new Object[size()]);
  107. }
  108. public Object[] toArray(Object[] a) {
  109. int size = size();
  110. if (size == 0) {
  111. return null;
  112. }
  113. if (a == null || a.length < size) {
  114. a = new Object[size];
  115. }
  116. Iterator<Data> it = lsValues.iterator();
  117. int index = 0;
  118. while (it.hasNext()) {
  119. a[index++] = toObject(it.next());
  120. }
  121. return a;
  122. }
  123. public void readData(DataInput in) throws IOException {
  124. int size = in.readInt();
  125. lsValues = new ArrayList<Data>(size);
  126. for (int i = 0; i < size; i++) {
  127. Data data = new Data();
  128. data.readData(in);
  129. lsValues.add(data);
  130. }
  131. }
  132. public void writeData(DataOutput out) throws IOException {
  133. int size = (lsValues == null) ? 0 : lsValues.size();
  134. out.writeInt(size);
  135. if (size > 0) {
  136. for (Data data : lsValues) {
  137. data.writeData(out);
  138. }
  139. }
  140. }
  141. public String toString() {
  142. Iterator i = iterator();
  143. if (!i.hasNext())
  144. return "[]";
  145. StringBuilder sb = new StringBuilder();
  146. sb.append('[');
  147. for (; ; ) {
  148. Object e = i.next();
  149. sb.append(e == this ? "(this Collection)" : e);
  150. if (!i.hasNext())
  151. return sb.append(']').toString();
  152. sb.append(", ");
  153. }
  154. }
  155. }