29import java.util.ArrayList;
30import java.util.Collection;
31import java.util.HashMap;
32import java.util.Iterator;
34import java.util.ListIterator;
67 implements Cloneable, List<E>
78 private final HashMap<E,E> map;
79 private final ArrayList<E> data;
80 private final boolean supportNullValue;
91 public ArrayHashSet(
final boolean supportNullValue,
final int initialCapacity,
final float loadFactor) {
92 this.map =
new HashMap<E,E>(initialCapacity, loadFactor);
93 this.data =
new ArrayList<E>(initialCapacity);
94 this.supportNullValue = supportNullValue;
101 map =
new HashMap<E, E>(o.map);
102 data =
new ArrayList<E>(o.data);
103 supportNullValue = o.supportNullValue;
129 public final ArrayList<E>
getData() {
return data; }
131 public final HashMap<E,E>
getMap() {
return map; }
134 public final String
toString() {
return data.toString(); }
159 public final boolean add(
final E element)
throws NullPointerException {
160 if( !supportNullValue ) {
163 if( !map.containsKey(element) ) {
165 if(
null != map.put(element, element)) {
167 throw new InternalError(
"Already existing, but checked before: "+element);
169 if(!data.add(element)) {
170 throw new InternalError(
"Couldn't add element: "+element);
191 public final boolean remove(
final Object element)
throws NullPointerException {
192 if( supportNullValue ) {
193 if( map.containsKey(element) ) {
196 if ( !data.remove(element) ) {
197 throw new InternalError(
"Couldn't remove prev mapped element: "+element);
203 if (
null != map.remove(element) ) {
205 if ( !data.remove(element) ) {
206 throw new InternalError(
"Couldn't remove prev mapped element: "+element);
226 public final boolean addAll(
final Collection<? extends E> c) {
228 for (
final E o : c) {
246 public final boolean contains(
final Object element) {
247 return map.containsKey(element);
263 for (
final Object o : c) {
285 for (
final Object o : c) {
286 mod |= this.
remove(o);
306 for (
final Object o : c) {
307 if (!c.contains(o)) {
308 mod |= this.
remove(o);
324 public final boolean equals(
final Object arrayHashSet) {
342 return data.hashCode();
347 return data.isEmpty();
352 return data.iterator();
362 return data.toArray();
367 return data.toArray(a);
375 public final E
get(
final int index) {
376 return data.get(index);
380 public final int indexOf(
final Object element) {
381 return data.indexOf(element);
396 public final void add(
final int index,
final E element)
throws IllegalArgumentException, NullPointerException {
397 if( !supportNullValue ) {
400 if ( map.containsKey(element) ) {
401 throw new IllegalArgumentException(
"Element "+element+
" is already contained");
403 if(
null != map.put(element, element)) {
405 throw new InternalError(
"Already existing, but checked before: "+element);
408 data.add(index, element);
418 public final boolean addAll(
final int index,
final Collection<? extends E> c)
throws UnsupportedOperationException {
419 throw new UnsupportedOperationException(
"Not supported yet.");
428 public final E
set(
final int index,
final E element) {
429 final E old =
remove(index);
447 public final E
remove(
final int index) {
448 final E o =
get(index);
449 if(
null!=o &&
remove(o) ) {
472 return data.listIterator();
477 return data.listIterator(index);
481 public final List<E>
subList(
final int fromIndex,
final int toIndex) {
482 return data.subList(fromIndex, toIndex);
493 return new ArrayList<E>(data);
505 public final E
get(
final Object element) {
506 return map.get(element);
520 public final E
getOrAdd(
final E element)
throws NullPointerException {
521 if( supportNullValue ) {
522 if( map.containsKey(element) ) {
524 return map.get(element);
528 final E identity = map.get(element);
529 if(
null != identity) {
535 if(!this.
add(element)) {
536 throw new InternalError(
"Element not mapped, but contained in list: "+element);
554 return data.contains(element);
557 private static final void checkNull(
final Object element)
throws NullPointerException {
558 if(
null == element ) {
559 throw new NullPointerException(
"Null element not supported");
Hashed ArrayList implementation of the List and Collection interface.
final boolean equals(final Object arrayHashSet)
This is an O(n) operation.
final Iterator< E > iterator()
final ListIterator< E > listIterator(final int index)
static final float DEFAULT_LOAD_FACTOR
Default load factor: {@value}.
final ListIterator< E > listIterator()
final boolean addAll(final Collection<? extends E > c)
Add all elements of given java.util.Collection at the end of this list.
final boolean containsAll(final Collection<?> c)
Test for containment of given java.util.Collection This is an O(n) operation, over the given Collec...
final int indexOf(final Object element)
final ArrayList< E > toArrayList()
final< T > T[] toArray(final T[] a)
final HashMap< E, E > getMap()
Returns this object hash map.
final boolean removeAll(final Collection<?> c)
Remove all elements of given java.util.Collection from this list.
final boolean add(final E element)
Add element at the end of this list, if it is not contained yet.
static final int DEFAULT_INITIAL_CAPACITY
The default initial capacity: {@value}.
ArrayHashSet(final boolean supportNullValue, final int initialCapacity, final float loadFactor)
final ArrayList< E > getData()
Returns this object ordered ArrayList.
final int lastIndexOf(final Object o)
Since this list is unique, equivalent to indexOf(java.lang.Object).
final boolean addAll(final int index, final Collection<? extends E > c)
final List< E > subList(final int fromIndex, final int toIndex)
final void add(final int index, final E element)
Add element at the given index in this list, if it is not contained yet.
final boolean supportsNullValue()
Returns true for default behavior, i.e.
final int hashCode()
This is an O(n) operation over the size of this list.
ArrayHashSet(final ArrayHashSet< E > o)
final boolean containsSafe(final Object element)
Test for containment This is an O(n) operation, using equals operation over the list.
final E getOrAdd(final E element)
Identity method allowing to get the identical object, using the internal hash map.
final boolean retainAll(final Collection<?> c)
Retain all elements of the given java.util.Collection c, ie remove all elements not contained by the ...
final boolean contains(final Object element)
Test for containment This is an O(1) operation.