com.groupbyinc.common.util.collections4.set.ListOrderedSet Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.collections4.set;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import org.apache.commons.collections4.OrderedIterator;
import org.apache.commons.collections4.iterators.AbstractIteratorDecorator;
import org.apache.commons.collections4.list.UnmodifiableList;
/**
* Decorates another Set
to ensure that the order of addition is
* retained and used by the iterator.
*
* If an object is added to the set for a second time, it will remain in the
* original position in the iteration. The order can be observed from the set
* via the iterator or toArray methods.
*
* The ListOrderedSet also has various useful direct methods. These include many
* from List
, such as get(int)
,
* remove(int)
and indexOf(int)
. An unmodifiable
* List
view of the set can be obtained via asList()
.
*
* This class cannot implement the List
interface directly as
* various interface methods (notably equals/hashCode) are incompatible with a
* set.
*
* This class is Serializable from Commons Collections 3.1.
*
* @since 3.0
* @version $Id: ListOrderedSet.java 1543167 2013-11-18 21:21:32Z ggregory $
*/
public class ListOrderedSet
extends AbstractSerializableSetDecorator {
/** Serialization version */
private static final long serialVersionUID = -228664372470420141L;
/** Internal list to hold the sequence of objects */
private final List setOrder;
/**
* Factory method to create an ordered set specifying the list and set to
* use.
*
* The list and set must both be empty.
*
* @param the element type
* @param set the set to decorate, must be empty and not null
* @param list the list to decorate, must be empty and not null
* @return a new ordered set
* @throws IllegalArgumentException if set or list is null
* @throws IllegalArgumentException if either the set or list is not empty
* @since 4.0
*/
public static ListOrderedSet listOrderedSet(final Set set, final List list) {
if (set == null) {
throw new IllegalArgumentException("Set must not be null");
}
if (list == null) {
throw new IllegalArgumentException("List must not be null");
}
if (set.size() > 0 || list.size() > 0) {
throw new IllegalArgumentException("Set and List must be empty");
}
return new ListOrderedSet(set, list);
}
/**
* Factory method to create an ordered set.
*
* An ArrayList
is used to retain order.
*
* @param the element type
* @param set the set to decorate, must not be null
* @return a new ordered set
* @throws IllegalArgumentException if set is null
* @since 4.0
*/
public static ListOrderedSet listOrderedSet(final Set set) {
return new ListOrderedSet(set);
}
/**
* Factory method to create an ordered set using the supplied list to retain
* order.
*
* A HashSet
is used for the set behaviour.
*
* NOTE: If the list contains duplicates, the duplicates are removed,
* altering the specified list.
*
* @param the element type
* @param list the list to decorate, must not be null
* @return a new ordered set
* @throws IllegalArgumentException if list is null
* @since 4.0
*/
public static ListOrderedSet listOrderedSet(final List list) {
if (list == null) {
throw new IllegalArgumentException("List must not be null");
}
final Set set = new HashSet(list);
list.retainAll(set);
return new ListOrderedSet(set, list);
}
// -----------------------------------------------------------------------
/**
* Constructs a new empty ListOrderedSet
using a
* HashSet
and an ArrayList
internally.
*
* @since 3.1
*/
public ListOrderedSet() {
super(new HashSet());
setOrder = new ArrayList();
}
/**
* Constructor that wraps (not copies).
*
* @param set the set to decorate, must not be null
* @throws IllegalArgumentException if set is null
*/
protected ListOrderedSet(final Set set) {
super(set);
setOrder = new ArrayList(set);
}
/**
* Constructor that wraps (not copies) the Set and specifies the list to
* use.
*
* The set and list must both be correctly initialised to the same elements.
*
* @param set the set to decorate, must not be null
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if set or list is null
*/
protected ListOrderedSet(final Set set, final List list) {
super(set);
if (list == null) {
throw new IllegalArgumentException("List must not be null");
}
setOrder = list;
}
// -----------------------------------------------------------------------
/**
* Gets an unmodifiable view of the order of the Set.
*
* @return an unmodifiable list view
*/
public List asList() {
return UnmodifiableList.unmodifiableList(setOrder);
}
// -----------------------------------------------------------------------
@Override
public void clear() {
decorated().clear();
setOrder.clear();
}
@Override
public OrderedIterator iterator() {
return new OrderedSetIterator(setOrder.listIterator(), decorated());
}
@Override
public boolean add(final E object) {
if (decorated().add(object)) {
setOrder.add(object);
return true;
}
return false;
}
@Override
public boolean addAll(final Collection coll) {
boolean result = false;
for (final E e : coll) {
result |= add(e);
}
return result;
}
@Override
public boolean remove(final Object object) {
final boolean result = decorated().remove(object);
if (result) {
setOrder.remove(object);
}
return result;
}
@Override
public boolean removeAll(final Collection coll) {
boolean result = false;
for (final Object name : coll) {
result |= remove(name);
}
return result;
}
@Override
public boolean retainAll(final Collection coll) {
final Set