|
Commons Collections example source code file (SetUniqueList.java)
The Commons Collections SetUniqueList.java source code/*
* 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.collections.list;
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.collections.iterators.AbstractIteratorDecorator;
import org.apache.commons.collections.iterators.AbstractListIteratorDecorator;
import org.apache.commons.collections.set.UnmodifiableSet;
/**
* Decorates a <code>List to ensure that no duplicates are present
* much like a <code>Set.
* <p>
* The <code>List interface makes certain assumptions/requirements.
* This implementation breaks these in certain ways, but this is merely the
* result of rejecting duplicates.
* Each violation is explained in the method, but it should not affect you.
* Bear in mind that Sets require immutable objects to function correctly.
* <p>
* The {@link org.apache.commons.collections.set.ListOrderedSet ListOrderedSet}
* class provides an alternative approach, by wrapping an existing Set and
* retaining insertion order in the iterator.
* <p>
* This class is Serializable from Commons Collections 3.1.
*
* @since Commons Collections 3.0
* @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $
*
* @author Matthew Hawthorne
* @author Stephen Colebourne
* @author Tom Dunham
*/
public class SetUniqueList extends AbstractSerializableListDecorator {
/** Serialization version */
private static final long serialVersionUID = 7196982186153478694L;
/**
* Internal Set to maintain uniqueness.
*/
protected final Set set;
/**
* Factory method to create a SetList using the supplied list to retain order.
* <p>
* If the list contains duplicates, these are removed (first indexed one kept).
* A <code>HashSet is used for the set behaviour.
*
* @param list the list to decorate, must not be null
* @throws IllegalArgumentException if list is null
*/
public static SetUniqueList decorate(List list) {
if (list == null) {
throw new IllegalArgumentException("List must not be null");
}
if (list.isEmpty()) {
return new SetUniqueList(list, new HashSet());
} else {
List temp = new ArrayList(list);
list.clear();
SetUniqueList sl = new SetUniqueList(list, new HashSet());
sl.addAll(temp);
return sl;
}
}
//-----------------------------------------------------------------------
/**
* Constructor that wraps (not copies) the List and specifies the set to use.
* <p>
* 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 SetUniqueList(List list, Set set) {
super(list);
if (set == null) {
throw new IllegalArgumentException("Set must not be null");
}
this.set = set;
}
//-----------------------------------------------------------------------
/**
* Gets an unmodifiable view as a Set.
*
* @return an unmodifiable set view
*/
public Set asSet() {
return UnmodifiableSet.decorate(set);
}
//-----------------------------------------------------------------------
/**
* Adds an element to the list if it is not already present.
* <p>
* <i>(Violation)
* The <code>List interface requires that this method returns
* <code>true always. However this class may return
Other Commons Collections examples (source code examples)Here is a short list of links related to this Commons Collections SetUniqueList.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.