~ubuntu-branches/ubuntu/wily/gs-collections/wily

« back to all changes in this revision

Viewing changes to collections/src/main/java/com/gs/collections/impl/collection/mutable/CollectionAdapter.java

  • Committer: Package Import Robot
  • Author(s): Emmanuel Bourg
  • Date: 2015-07-23 12:42:30 UTC
  • Revision ID: package-import@ubuntu.com-20150723124230-2rjvfv6elyn2m7d4
Tags: upstream-5.1.0
ImportĀ upstreamĀ versionĀ 5.1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright 2011 Goldman Sachs.
 
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
 
 
17
package com.gs.collections.impl.collection.mutable;
 
18
 
 
19
import java.io.Serializable;
 
20
import java.util.ArrayList;
 
21
import java.util.Collection;
 
22
import java.util.List;
 
23
import java.util.RandomAccess;
 
24
import java.util.Set;
 
25
 
 
26
import com.gs.collections.api.collection.ImmutableCollection;
 
27
import com.gs.collections.api.collection.MutableCollection;
 
28
import com.gs.collections.api.list.MutableList;
 
29
import com.gs.collections.api.set.MutableSet;
 
30
import com.gs.collections.impl.block.factory.Comparators;
 
31
import com.gs.collections.impl.block.procedure.CollectionAddProcedure;
 
32
import com.gs.collections.impl.block.procedure.CollectionRemoveProcedure;
 
33
import com.gs.collections.impl.factory.Lists;
 
34
import com.gs.collections.impl.factory.Sets;
 
35
import com.gs.collections.impl.list.mutable.ArrayListAdapter;
 
36
import com.gs.collections.impl.list.mutable.FastList;
 
37
import com.gs.collections.impl.list.mutable.ListAdapter;
 
38
import com.gs.collections.impl.list.mutable.RandomAccessListAdapter;
 
39
import com.gs.collections.impl.set.mutable.SetAdapter;
 
40
import com.gs.collections.impl.set.mutable.UnifiedSet;
 
41
import com.gs.collections.impl.utility.ArrayIterate;
 
42
import com.gs.collections.impl.utility.Iterate;
 
43
 
 
44
/**
 
45
 * This class provides a MutableCollection interface wrapper around a JDK Collections Collection interface instance.  All
 
46
 * of the MutableCollection interface methods are supported in addition to the JDK Collection interface methods.
 
47
 * <p/>
 
48
 * To create a new instance that wraps a collection with the MutableSet interface, use the {@link #wrapSet(Iterable)}
 
49
 * factory method.  To create a new instance that wraps a collection with the MutableList interface, use the
 
50
 * {@link #wrapList(Iterable)} factory method.  To wrap a collection with the MutableCollection interface alone, use
 
51
 * the {@link #adapt(Collection)} factory method.
 
52
 */
 
53
public final class CollectionAdapter<T>
 
54
        extends AbstractCollectionAdapter<T>
 
55
        implements Serializable
 
56
{
 
57
    private static final long serialVersionUID = 1L;
 
58
    private final Collection<T> delegate;
 
59
 
 
60
    public CollectionAdapter(Collection<T> newDelegate)
 
61
    {
 
62
        if (newDelegate == null)
 
63
        {
 
64
            throw new NullPointerException("CollectionAdapter may not wrap null");
 
65
        }
 
66
        this.delegate = newDelegate;
 
67
    }
 
68
 
 
69
    @Override
 
70
    protected Collection<T> getDelegate()
 
71
    {
 
72
        return this.delegate;
 
73
    }
 
74
 
 
75
    public MutableCollection<T> asUnmodifiable()
 
76
    {
 
77
        return UnmodifiableMutableCollection.of(this);
 
78
    }
 
79
 
 
80
    public MutableCollection<T> asSynchronized()
 
81
    {
 
82
        return SynchronizedMutableCollection.of(this);
 
83
    }
 
84
 
 
85
    public ImmutableCollection<T> toImmutable()
 
86
    {
 
87
        return this.delegate instanceof Set
 
88
                ? Sets.immutable.ofAll(this.delegate)
 
89
                : Lists.immutable.ofAll(this.delegate);
 
90
    }
 
91
 
 
92
    public static <E> MutableSet<E> wrapSet(Iterable<E> iterable)
 
93
    {
 
94
        if (iterable instanceof MutableSet)
 
95
        {
 
96
            return (MutableSet<E>) iterable;
 
97
        }
 
98
        if (iterable instanceof Set)
 
99
        {
 
100
            return SetAdapter.adapt((Set<E>) iterable);
 
101
        }
 
102
        return UnifiedSet.newSet(iterable);
 
103
    }
 
104
 
 
105
    public static <E> MutableList<E> wrapList(Iterable<E> iterable)
 
106
    {
 
107
        if (iterable instanceof MutableList)
 
108
        {
 
109
            return (MutableList<E>) iterable;
 
110
        }
 
111
        if (iterable instanceof ArrayList)
 
112
        {
 
113
            return ArrayListAdapter.adapt((ArrayList<E>) iterable);
 
114
        }
 
115
        if (iterable instanceof RandomAccess)
 
116
        {
 
117
            return RandomAccessListAdapter.adapt((List<E>) iterable);
 
118
        }
 
119
        if (iterable instanceof List)
 
120
        {
 
121
            return ListAdapter.adapt((List<E>) iterable);
 
122
        }
 
123
        return FastList.newList(iterable);
 
124
    }
 
125
 
 
126
    public static <E> MutableCollection<E> adapt(Collection<E> collection)
 
127
    {
 
128
        if (collection instanceof MutableCollection)
 
129
        {
 
130
            return (MutableCollection<E>) collection;
 
131
        }
 
132
        if (collection instanceof List)
 
133
        {
 
134
            return CollectionAdapter.wrapList(collection);
 
135
        }
 
136
        if (collection instanceof Set)
 
137
        {
 
138
            return SetAdapter.adapt((Set<E>) collection);
 
139
        }
 
140
        return new CollectionAdapter<E>(collection);
 
141
    }
 
142
 
 
143
    @Override
 
144
    public boolean equals(Object o)
 
145
    {
 
146
        if (this == o)
 
147
        {
 
148
            return true;
 
149
        }
 
150
        if (o == null || this.getClass() != o.getClass())
 
151
        {
 
152
            return false;
 
153
        }
 
154
 
 
155
        CollectionAdapter<?> that = (CollectionAdapter<?>) o;
 
156
        return Comparators.nullSafeEquals(this.delegate, that.delegate);
 
157
    }
 
158
 
 
159
    @Override
 
160
    public int hashCode()
 
161
    {
 
162
        return this.delegate.hashCode();
 
163
    }
 
164
 
 
165
    public CollectionAdapter<T> with(T... elements)
 
166
    {
 
167
        ArrayIterate.forEach(elements, new CollectionAddProcedure<T>(this.delegate));
 
168
        return this;
 
169
    }
 
170
 
 
171
    public CollectionAdapter<T> with(T element)
 
172
    {
 
173
        this.delegate.add(element);
 
174
        return this;
 
175
    }
 
176
 
 
177
    public CollectionAdapter<T> without(T element)
 
178
    {
 
179
        this.delegate.remove(element);
 
180
        return this;
 
181
    }
 
182
 
 
183
    public CollectionAdapter<T> withAll(Iterable<? extends T> elements)
 
184
    {
 
185
        Iterate.forEach(elements, new CollectionAddProcedure<T>(this.delegate));
 
186
        return this;
 
187
    }
 
188
 
 
189
    public CollectionAdapter<T> withoutAll(Iterable<? extends T> elements)
 
190
    {
 
191
        Iterate.forEach(elements, new CollectionRemoveProcedure<T>(this.delegate));
 
192
        return this;
 
193
    }
 
194
 
 
195
    /**
 
196
     * @deprecated use {@link FastList#newList()} or {@link UnifiedSet#newSet()} instead
 
197
     */
 
198
    @Deprecated
 
199
    public MutableCollection<T> newEmpty()
 
200
    {
 
201
        if (this.delegate instanceof Set)
 
202
        {
 
203
            return UnifiedSet.newSet();
 
204
        }
 
205
        return Lists.mutable.of();
 
206
    }
 
207
}