PageRenderTime 53ms CodeModel.GetById 15ms app.highlight 31ms RepoModel.GetById 1ms app.codeStats 0ms

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