/*
 * Copyright (C) 2002-2017 Sebastiano Vigna
 *
 * Licensed 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 PACKAGE;

import java.util.Collection;

import it.unimi.dsi.fastutil.objects.ObjectArrays;


/** A class providing static methods and objects that do useful things with type-specific collections.
 *
 * @see java.util.Collections
 */

public final class COLLECTIONS {

	private COLLECTIONS() {}

	/** An immutable class representing an empty type-specific collection.
	 *
	 * <p>This class may be useful to implement your own in case you subclass
	 * a type-specific collection.
	 */

	public abstract static class EmptyCollection KEY_GENERIC extends ABSTRACT_COLLECTION KEY_GENERIC {
		protected EmptyCollection() {}

		@Override
		public boolean contains(KEY_TYPE k) { return false; }

		@Override
		public Object[] toArray() { return ObjectArrays.EMPTY_ARRAY; }

		@Override
		SUPPRESS_WARNINGS_KEY_UNCHECKED
		public KEY_BIDI_ITERATOR KEY_GENERIC iterator() { return ITERATORS.EMPTY_ITERATOR; }

		@Override
		public int size() { return 0; }

		@Override
		public void clear() {}

		@Override
		public int hashCode() { return 0; }

		@Override
		public boolean equals(Object o) {
			if (o == this) return true;
			if (! (o instanceof Collection)) return false;
			return ((Collection<?>)o).isEmpty();
		}

		@Override
		public boolean addAll(final Collection<? extends KEY_GENERIC_CLASS> c) { throw new UnsupportedOperationException(); }

		@Override
		public boolean removeAll(final Collection<?> c) { throw new UnsupportedOperationException(); }

		@Override
		public boolean retainAll(final Collection<?> c) { throw new UnsupportedOperationException(); }

#if KEYS_PRIMITIVE

		@Override
		public boolean addAll(final COLLECTION c) { throw new UnsupportedOperationException(); }

		@Override
		public boolean removeAll(final COLLECTION c) { throw new UnsupportedOperationException(); }

		@Override
		public boolean retainAll(final COLLECTION c) { throw new UnsupportedOperationException(); }

#endif

	}


	/** A synchronized wrapper class for collections. */

	public static class SynchronizedCollection KEY_GENERIC implements COLLECTION KEY_GENERIC, java.io.Serializable {

		private static final long serialVersionUID = -7046029254386353129L;

		protected final COLLECTION KEY_GENERIC collection;
		protected final Object sync;

		protected SynchronizedCollection(final COLLECTION KEY_GENERIC c, final Object sync) {
			if (c == null) throw new NullPointerException();
			this.collection = c;
			this.sync = sync;
		}

		protected SynchronizedCollection(final COLLECTION KEY_GENERIC c) {
			if (c == null) throw new NullPointerException();
			this.collection = c;
			this.sync = this;
		}

		@Override
		public boolean add(final KEY_GENERIC_TYPE k) { synchronized(sync) { return collection.add(k); } }

		@Override
		public boolean contains(final KEY_TYPE k) { synchronized(sync) { return collection.contains(k); } }

		@Override
		public boolean REMOVE(final KEY_TYPE k) { synchronized(sync) { return collection.REMOVE(k); } }

		@Override
		public int size() { synchronized(sync) { return collection.size(); } }

		@Override
		public boolean isEmpty() { synchronized(sync) { return collection.isEmpty(); } }

		@Override
		public KEY_TYPE[] TO_KEY_ARRAY() { synchronized(sync) { return collection.TO_KEY_ARRAY(); } }

#if KEYS_PRIMITIVE
		@Override
		public Object[] toArray() { synchronized(sync) { return collection.toArray(); } }

		/* {@inheritDoc}
		 * @deprecated Please use {@code toArray()} instead&mdash;this method is redundant and will be removed in the future.
		 */
		@Deprecated
		@Override
		public KEY_TYPE[] TO_KEY_ARRAY(final KEY_TYPE[] a) { return toArray(a); }

		@Override
		public KEY_TYPE[] toArray(final KEY_TYPE[] a) { synchronized(sync) { return collection.toArray(a); } }

		@Override
		public boolean addAll(final COLLECTION c) { synchronized(sync) { return collection.addAll(c); } }

		@Override
		public boolean containsAll(final COLLECTION c) { synchronized(sync) { return collection.containsAll(c); } }

		@Override
		public boolean removeAll(final COLLECTION c) { synchronized(sync) { return collection.removeAll(c); } }

#ifdef JDK_PRIMITIVE_PREDICATE
		@Override
		public boolean removeIf(final JDK_PRIMITIVE_PREDICATE filter) { synchronized(sync) { return collection.removeIf(filter); } }
#endif

		@Override
		public boolean retainAll(final COLLECTION c) { synchronized(sync) { return collection.retainAll(c); } }

		@Override
		@Deprecated
		public boolean add(final KEY_GENERIC_CLASS k) { synchronized(sync) { return collection.add(k); } }

		@Override
		@Deprecated
		public boolean contains(final Object k) { synchronized(sync) { return collection.contains(k); } }

		@Override
		@Deprecated
		public boolean remove(final Object k) { synchronized(sync) { return collection.remove(k); } }
#endif

		@Override
		public <T> T[] toArray(final T[] a) { synchronized(sync) { return collection.toArray(a); } }

		@Override
		public KEY_ITERATOR KEY_GENERIC iterator() { return collection.iterator(); }

		@Override
		public boolean addAll(final Collection<? extends KEY_GENERIC_CLASS> c) { synchronized(sync) { return collection.addAll(c); } }

		@Override
		public boolean containsAll(final Collection<?> c) { synchronized(sync) { return collection.containsAll(c); } }

		@Override
		public boolean removeAll(final Collection<?> c) { synchronized(sync) { return collection.removeAll(c); } }

		@Override
		public boolean retainAll(final Collection<?> c) { synchronized(sync) { return collection.retainAll(c); } }

		@Override
		public void clear() { synchronized(sync) { collection.clear(); } }

		@Override
		public String toString() { synchronized(sync) { return collection.toString(); } }

		@Override
		public int hashCode() { synchronized(sync) { return collection.hashCode(); } }

		@Override
		public boolean equals(final Object o) { if (o == this) return true; synchronized(sync) { return collection.equals(o); } }

		private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException {
			synchronized(sync) { s.defaultWriteObject(); }
		}
	}


	/** Returns a synchronized collection backed by the specified collection.
	 *
	 * @param c the collection to be wrapped in a synchronized collection.
	 * @return a synchronized view of the specified collection.
	 * @see java.util.Collections#synchronizedCollection(Collection)
	 */
	public static KEY_GENERIC COLLECTION KEY_GENERIC synchronize(final COLLECTION KEY_GENERIC c) { return new SynchronizedCollection KEY_GENERIC_DIAMOND(c); }

	/** Returns a synchronized collection backed by the specified collection, using an assigned object to synchronize.
	 *
	 * @param c the collection to be wrapped in a synchronized collection.
	 * @param sync an object that will be used to synchronize the list access.
	 * @return a synchronized view of the specified collection.
	 * @see java.util.Collections#synchronizedCollection(Collection)
	 */

	public static KEY_GENERIC COLLECTION KEY_GENERIC synchronize(final COLLECTION KEY_GENERIC c, final Object sync) { return new SynchronizedCollection KEY_GENERIC_DIAMOND(c, sync); }


	/** An unmodifiable wrapper class for collections. */

	public static class UnmodifiableCollection KEY_GENERIC implements COLLECTION KEY_GENERIC, java.io.Serializable {

		private static final long serialVersionUID = -7046029254386353129L;

		protected final COLLECTION KEY_GENERIC collection;

		protected UnmodifiableCollection(final COLLECTION KEY_GENERIC c) {
			if (c == null) throw new NullPointerException();
			this.collection = c;
		}

		@Override
		public boolean add(final KEY_GENERIC_TYPE k) { throw new UnsupportedOperationException(); }

		@Override
		public boolean REMOVE(final KEY_TYPE k) { throw new UnsupportedOperationException(); }

		@Override
		public int size() { return collection.size(); }

		@Override
		public boolean isEmpty() { return collection.isEmpty(); }

		@Override
		public boolean contains(final KEY_TYPE o) { return collection.contains(o); }

		@Override
		public KEY_ITERATOR KEY_GENERIC iterator() { return ITERATORS.unmodifiable(collection.iterator()); }

		@Override
		public void clear() { throw new UnsupportedOperationException(); }

		@Override
		public <T> T[] toArray(final T[] a) { return collection.toArray(a); }

		@Override
		public Object[] toArray() { return collection.toArray(); }

		@Override
		public boolean containsAll(Collection<?> c) { return collection.containsAll(c); }

		@Override
		public boolean addAll(Collection<? extends KEY_GENERIC_CLASS> c) { throw new UnsupportedOperationException(); }

		@Override
		public boolean removeAll(Collection<?> c) { throw new UnsupportedOperationException(); }

		@Override
		public boolean retainAll(Collection<?> c) { throw new UnsupportedOperationException(); }

#if KEYS_PRIMITIVE
		@Override
		@Deprecated
		public boolean add(final KEY_GENERIC_CLASS k) { throw new UnsupportedOperationException(); }

		@Override
		@Deprecated
		public boolean contains(final Object k) { return collection.contains(k); }

		@Override
		@Deprecated
		public boolean remove(final Object k) { throw new UnsupportedOperationException(); }

		@Override
		public KEY_TYPE[] TO_KEY_ARRAY() { return collection.TO_KEY_ARRAY(); }

		/* {@inheritDoc}
		 * @deprecated Please use {@code toArray()} instead&mdash;this method is redundant.
		 */
		@Deprecated
		@Override
		public KEY_TYPE[] TO_KEY_ARRAY(final KEY_TYPE[] a) { return toArray(a); }

		@Override
		public KEY_TYPE[] toArray(final KEY_TYPE[] a) { return collection.toArray(a); }

		@Override
		public boolean containsAll(COLLECTION c) { return collection.containsAll(c); }

		@Override
		public boolean addAll(COLLECTION c) { throw new UnsupportedOperationException(); }

		@Override
		public boolean removeAll(COLLECTION c) { throw new UnsupportedOperationException(); }

		@Override
		public boolean retainAll(COLLECTION c) { throw new UnsupportedOperationException(); }
#endif
		@Override
		public String toString() { return collection.toString(); }

		@Override
		public int hashCode() { return collection.hashCode(); }

		@Override
		public boolean equals(final Object o) { if (o == this) return true; return collection.equals(o); }
	}


	/** Returns an unmodifiable collection backed by the specified collection.
	 *
	 * @param c the collection to be wrapped in an unmodifiable collection.
	 * @return an unmodifiable view of the specified collection.
	 * @see java.util.Collections#unmodifiableCollection(Collection)
	 */
	public static KEY_GENERIC COLLECTION KEY_GENERIC unmodifiable(final COLLECTION KEY_GENERIC c) { return new UnmodifiableCollection KEY_GENERIC_DIAMOND(c); }

	/** A collection wrapper class for iterables. */

	public static class IterableCollection KEY_GENERIC extends ABSTRACT_COLLECTION KEY_GENERIC implements java.io.Serializable {

		private static final long serialVersionUID = -7046029254386353129L;

		protected final KEY_ITERABLE KEY_GENERIC iterable;

		protected IterableCollection(final KEY_ITERABLE KEY_GENERIC iterable) {
			if (iterable == null) throw new NullPointerException();
			this.iterable = iterable;
		}

		@Override
		public int size() {
			int c = 0;
			final KEY_ITERATOR KEY_GENERIC iterator = iterator();
			while(iterator.hasNext()) {
				iterator.NEXT_KEY();
				c++;
			}

			return c;
		}

		@Override
		public boolean isEmpty() { return ! iterable.iterator().hasNext(); }

		@Override
		public KEY_ITERATOR KEY_GENERIC iterator() { return iterable.iterator(); }
	}


	/** Returns an unmodifiable collection backed by the specified iterable.
	 *
	 * @param iterable the iterable object to be wrapped in an unmodifiable collection.
	 * @return an unmodifiable collection view of the specified iterable.
	 */
	public static KEY_GENERIC COLLECTION KEY_GENERIC asCollection(final KEY_ITERABLE KEY_GENERIC iterable) {
		if (iterable instanceof COLLECTION) return (COLLECTION KEY_GENERIC)iterable;
		return new IterableCollection KEY_GENERIC_DIAMOND(iterable);
	}

}
