<commit_before>/*
 * #%L
 * ELK Reasoner
 * 
 * $Id$
 * $HeadURL$
 * %%
 * Copyright (C) 2011 Department of Computer Science, University of Oxford
 * %%
 * 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.
 * #L%
 */
package org.semanticweb.elk.util.collections;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;

public class Operations {

	@SuppressWarnings("unchecked")
	public static <T> Iterable<T> concat(final Iterable<? extends T> a,
			final Iterable<? extends T> b) {
		return concat(Arrays.asList(a, b));
	}

	public static <T> Iterable<T> singleton(final T element) {
		return new Iterable<T>() {
			public Iterator<T> iterator() {
				return new Iterator<T>() {
					boolean hasNext = true;

					public boolean hasNext() {
						return hasNext;
					}

					public T next() {
						hasNext = false;
						return element;
					}

					public void remove() {
						throw new UnsupportedOperationException();
					}
				};
			}
		};
	}

	public static <T> Iterable<T> concat(
			final Iterable<? extends Iterable<? extends T>> input) {
		assert input != null;

		return new Iterable<T>() {

			public Iterator<T> iterator() {

				return new Iterator<T>() {
					Iterator<? extends Iterable<? extends T>> outer = input
							.iterator();

					Iterator<? extends T> inner;
					boolean hasNext = advance();

					public boolean hasNext() {
						return hasNext;
					}

					public T next() {
						if (hasNext) {
							T result = inner.next();
							hasNext = advance();
							return result;
						}
						throw new NoSuchElementException();
					}

					public void remove() {
						throw new UnsupportedOperationException();
					}

					boolean advance() {
						while (true) {
							if (inner != null && inner.hasNext())
								return true;

							if (outer.hasNext())
								inner = outer.next().iterator();
							else
								return false;
						}
					}
				};
			}
		};
	}

	/**
	 * An interface for boolean conditions over some type.
	 * 
	 */
	public interface Condition<T> {
		/**
		 * Checks if the condition holds for an element
		 * 
		 * @param element
		 *            the element for which to check the condition
		 * @return <tt>true</tt> if the condition holds for the element and
		 *         otherwise <tt>false</tt>
		 */
		public boolean holds(T element);
	}

	/**
	 * 
	 * @param input
	 * @param condition 
	 * @return
	 */
	public static <T> Iterable<T> filter(final Iterable<T> input, final Condition<? super T> condition) {
		assert input != null;

		return new Iterable<T>() {

			public Iterator<T> iterator() {

				return new Iterator<T>() {
					Iterator<T> i = input.iterator();
					T next;
					boolean hasNext = advance();

					public boolean hasNext() {
						return hasNext;
					}

					public T next() {
						if (hasNext) {
							T result = next;
							hasNext = advance();
							return result;
						}
						throw new NoSuchElementException();
					}

					public void remove() {
						i.remove();
					}

					boolean advance() {
						while (i.hasNext()) {
							next = i.next();
							if (condition.holds(next))
								return true;
						}
						return false;
					}
				};
			}
		};
	}

	@SuppressWarnings("unchecked")
	public static <T, S> Iterable<T> filter(final Iterable<S> input, final Class<T> type) {

		return (Iterable<T>) filter(input, new Condition<S>() {
			public boolean holds(S element) {
				return type.isInstance(element);
			}
		});
	}

	/**
	 * Returns read-only view of the given set consisting of the elements
	 * satisfying a given condition, if the number of such elements is known
	 * 
	 * @param input
	 *            the given set to be filtered
	 * @param condition
	 *            the condition used for filtering the set. 
	 *            Must be consistent with equals() for T, that is: a.equals(b) must imply that holds(a) == holds(b)
	 * @param size
	 *            the number of elements in the filtered set
	 * @return the set consisting of the elements of the input set satisfying
	 *         the given condition
	 */
	public static <T> Set<T> filter(final Set<T> input, final Condition<? super T> condition, final int size) {
		return new Set<T>() {

			public int size() {
				return size;
			}

			public boolean isEmpty() {
				return size == 0;
			}

			public boolean contains(Object o) {
				
				if (!input.contains(o)) return false;
				
				try {
					@SuppressWarnings("unchecked")
					T elem = (T) o;
					//here's why the condition must be consistent with equals():
					//we check it on the passed element while we really need to check it on the element 
					//which is in the underlying set (and is equal to o according to equals()). 
					//However, as long as the condition is consistent, the result will be the same.
					return condition.holds(elem);
				} catch (ClassCastException cce) {
					return false;
				}
			}

			public Iterator<T> iterator() {
				return filter(input, condition).iterator();
			}<commit_msg>a minor tweak to not mask CCEs thrown out of holds(); the try-catch block should only intertcept CCEs from the type cast in contains()<commit_after>/*
 * #%L
 * ELK Reasoner
 * 
 * $Id$
 * $HeadURL$
 * %%
 * Copyright (C) 2011 Department of Computer Science, University of Oxford
 * %%
 * 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.
 * #L%
 */
package org.semanticweb.elk.util.collections;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Set;

public class Operations {

	@SuppressWarnings("unchecked")
	public static <T> Iterable<T> concat(final Iterable<? extends T> a,
			final Iterable<? extends T> b) {
		return concat(Arrays.asList(a, b));
	}

	public static <T> Iterable<T> singleton(final T element) {
		return new Iterable<T>() {
			public Iterator<T> iterator() {
				return new Iterator<T>() {
					boolean hasNext = true;

					public boolean hasNext() {
						return hasNext;
					}

					public T next() {
						hasNext = false;
						return element;
					}

					public void remove() {
						throw new UnsupportedOperationException();
					}
				};
			}
		};
	}

	public static <T> Iterable<T> concat(
			final Iterable<? extends Iterable<? extends T>> input) {
		assert input != null;

		return new Iterable<T>() {

			public Iterator<T> iterator() {

				return new Iterator<T>() {
					Iterator<? extends Iterable<? extends T>> outer = input
							.iterator();

					Iterator<? extends T> inner;
					boolean hasNext = advance();

					public boolean hasNext() {
						return hasNext;
					}

					public T next() {
						if (hasNext) {
							T result = inner.next();
							hasNext = advance();
							return result;
						}
						throw new NoSuchElementException();
					}

					public void remove() {
						throw new UnsupportedOperationException();
					}

					boolean advance() {
						while (true) {
							if (inner != null && inner.hasNext())
								return true;

							if (outer.hasNext())
								inner = outer.next().iterator();
							else
								return false;
						}
					}
				};
			}
		};
	}

	/**
	 * An interface for boolean conditions over some type.
	 * 
	 */
	public interface Condition<T> {
		/**
		 * Checks if the condition holds for an element
		 * 
		 * @param element
		 *            the element for which to check the condition
		 * @return <tt>true</tt> if the condition holds for the element and
		 *         otherwise <tt>false</tt>
		 */
		public boolean holds(T element);
	}

	/**
	 * 
	 * @param input
	 * @param condition 
	 * @return
	 */
	public static <T> Iterable<T> filter(final Iterable<T> input, final Condition<? super T> condition) {
		assert input != null;

		return new Iterable<T>() {

			public Iterator<T> iterator() {

				return new Iterator<T>() {
					Iterator<T> i = input.iterator();
					T next;
					boolean hasNext = advance();

					public boolean hasNext() {
						return hasNext;
					}

					public T next() {
						if (hasNext) {
							T result = next;
							hasNext = advance();
							return result;
						}
						throw new NoSuchElementException();
					}

					public void remove() {
						i.remove();
					}

					boolean advance() {
						while (i.hasNext()) {
							next = i.next();
							if (condition.holds(next))
								return true;
						}
						return false;
					}
				};
			}
		};
	}

	@SuppressWarnings("unchecked")
	public static <T, S> Iterable<T> filter(final Iterable<S> input, final Class<T> type) {

		return (Iterable<T>) filter(input, new Condition<S>() {
			public boolean holds(S element) {
				return type.isInstance(element);
			}
		});
	}

	/**
	 * Returns read-only view of the given set consisting of the elements
	 * satisfying a given condition, if the number of such elements is known
	 * 
	 * @param input
	 *            the given set to be filtered
	 * @param condition
	 *            the condition used for filtering the set. 
	 *            Must be consistent with equals() for T, that is: a.equals(b) must imply that holds(a) == holds(b)
	 * @param size
	 *            the number of elements in the filtered set
	 * @return the set consisting of the elements of the input set satisfying
	 *         the given condition
	 */
	public static <T> Set<T> filter(final Set<T> input, final Condition<? super T> condition, final int size) {
		return new Set<T>() {

			public int size() {
				return size;
			}

			public boolean isEmpty() {
				return size == 0;
			}

			@SuppressWarnings("unchecked")
			public boolean contains(Object o) {
				
				if (!input.contains(o)) return false;
				
				T elem = null;
				
				try {
					elem = (T) o;
				} catch (ClassCastException cce) {
					return false;
				}
				//here's why the condition must be consistent with equals():
				//we check it on the passed element while we really need to check it on the element 
				//which is in the underlying set (and is equal to o according to equals()). 
				//However, as long as the condition is consistent, the result will be the same.
				return condition.holds(elem);				
			}

			public Iterator<T> iterator() {
				return filter(input, condition).iterator();
			}
{'fraction_non_alphanumeric': 0.10931102011750045, 'fraction_numerical': 0.0016022787965105929, 'mean_word_length': 2.5109375, 'pattern_counts': {'":': 0, '<': 91, '<?xml version=': 0, '>': 91, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7665932', 'n_tokens_mistral': 4222, 'n_tokens_neox': 3176, 'n_words': 1181}