2
* Copyright 2011 Goldman Sachs.
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
8
* http://www.apache.org/licenses/LICENSE-2.0
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.
17
package com.gs.collections.impl.collection.mutable;
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;
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;
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.
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.
53
public final class CollectionAdapter<T>
54
extends AbstractCollectionAdapter<T>
55
implements Serializable
57
private static final long serialVersionUID = 1L;
58
private final Collection<T> delegate;
60
public CollectionAdapter(Collection<T> newDelegate)
62
if (newDelegate == null)
64
throw new NullPointerException("CollectionAdapter may not wrap null");
66
this.delegate = newDelegate;
70
protected Collection<T> getDelegate()
75
public MutableCollection<T> asUnmodifiable()
77
return UnmodifiableMutableCollection.of(this);
80
public MutableCollection<T> asSynchronized()
82
return SynchronizedMutableCollection.of(this);
85
public ImmutableCollection<T> toImmutable()
87
return this.delegate instanceof Set
88
? Sets.immutable.ofAll(this.delegate)
89
: Lists.immutable.ofAll(this.delegate);
92
public static <E> MutableSet<E> wrapSet(Iterable<E> iterable)
94
if (iterable instanceof MutableSet)
96
return (MutableSet<E>) iterable;
98
if (iterable instanceof Set)
100
return SetAdapter.adapt((Set<E>) iterable);
102
return UnifiedSet.newSet(iterable);
105
public static <E> MutableList<E> wrapList(Iterable<E> iterable)
107
if (iterable instanceof MutableList)
109
return (MutableList<E>) iterable;
111
if (iterable instanceof ArrayList)
113
return ArrayListAdapter.adapt((ArrayList<E>) iterable);
115
if (iterable instanceof RandomAccess)
117
return RandomAccessListAdapter.adapt((List<E>) iterable);
119
if (iterable instanceof List)
121
return ListAdapter.adapt((List<E>) iterable);
123
return FastList.newList(iterable);
126
public static <E> MutableCollection<E> adapt(Collection<E> collection)
128
if (collection instanceof MutableCollection)
130
return (MutableCollection<E>) collection;
132
if (collection instanceof List)
134
return CollectionAdapter.wrapList(collection);
136
if (collection instanceof Set)
138
return SetAdapter.adapt((Set<E>) collection);
140
return new CollectionAdapter<E>(collection);
144
public boolean equals(Object o)
150
if (o == null || this.getClass() != o.getClass())
155
CollectionAdapter<?> that = (CollectionAdapter<?>) o;
156
return Comparators.nullSafeEquals(this.delegate, that.delegate);
160
public int hashCode()
162
return this.delegate.hashCode();
165
public CollectionAdapter<T> with(T... elements)
167
ArrayIterate.forEach(elements, new CollectionAddProcedure<T>(this.delegate));
171
public CollectionAdapter<T> with(T element)
173
this.delegate.add(element);
177
public CollectionAdapter<T> without(T element)
179
this.delegate.remove(element);
183
public CollectionAdapter<T> withAll(Iterable<? extends T> elements)
185
Iterate.forEach(elements, new CollectionAddProcedure<T>(this.delegate));
189
public CollectionAdapter<T> withoutAll(Iterable<? extends T> elements)
191
Iterate.forEach(elements, new CollectionRemoveProcedure<T>(this.delegate));
196
* @deprecated use {@link FastList#newList()} or {@link UnifiedSet#newSet()} instead
199
public MutableCollection<T> newEmpty()
201
if (this.delegate instanceof Set)
203
return UnifiedSet.newSet();
205
return Lists.mutable.of();