/*
 * Java Genetic Algorithm Library (@__identifier__@).
 * Copyright (c) @__year__@ Franz Wilhelmstötter
 *
 * 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.
 *
 * Author:
 *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
 */
package org.jenetics.internal.util;

import static java.util.Arrays.stream;
import static java.util.stream.Stream.concat;

import java.util.Optional;
import java.util.stream.Stream;

import org.jenetics.util.ISeq;
import org.jenetics.util.StaticObject;

/**
 * Helper methods concerning Java reflection.
 *
 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
 * @since 1.6
 * @version 3.0 &mdash; <em>$Date: 2014-06-29 $</em>
 */
public class reflect extends StaticObject {
	private reflect() {}

	/**
	 * Return all declared classes of the given class, with arbitrary nested
	 * level.
	 *
	 * @param cls the class for which the declared classes are retrieved.
	 * @return all nested classes
	 */
	public static Stream<Class<?>> innerClasses(final Class<?> cls) {
		return concat(
			stream(cls.getDeclaredClasses()).flatMap(reflect::innerClasses),
			stream(cls.getDeclaredClasses())
		);
	}

	/**
	 * Return the class of the given value or the value if it is already from
	 * the type {@code Class}.
	 *
	 * @param value the value to get the class from.
	 * @return the class from the given value, or {@code value} if it is already
	 *         a {@code Class}.
	 */
	public static Class<?> classOf(final Object value) {
		return value instanceof Class<?> ? (Class<?>)value : value.getClass();
	}

	@SuppressWarnings("unchecked")
	public static <T> Class<T> typeOf(final T value) {
		return (Class<T>)value.getClass();
	}

	@SuppressWarnings("unchecked")
	public static <T> Optional<T> newInstance(final Class<?> type) {
		try {
			return Optional.of((T)type.newInstance());
		} catch (InstantiationException | IllegalAccessException e) {
			return Optional.empty();
		}
	}

	public static <A> Optional<A> cast(final Class<A> type, final Object object) {
		return Optional.ofNullable(object)
			.filter(o -> type.isAssignableFrom(o.getClass()))
			.map(type::cast);
	}

	@SuppressWarnings("unchecked")
	public static <A, B extends A> ISeq<A> cast(final ISeq<B> seq) {
		return (ISeq<A>)seq;
	}
}

@@ -1182,12 +1182,12 @@
 14-0
-6-29
+7-01
  $%3C/
@@ -2068,133 +2068,8 @@
 %09%7D%0A%0A
-%09@SuppressWarnings(%22unchecked%22)%0A%09public static %3CT%3E Class%3CT%3E typeOf(final T value) %7B%0A%09%09return (Class%3CT%3E)value.getClass();%0A%09%7D%0A%0A
 %09@Su
@@ -2320,203 +2320,8 @@
 %09%7D%0A%0A
-%09public static %3CA%3E Optional%3CA%3E cast(final Class%3CA%3E type, final Object object) %7B%0A%09%09return Optional.ofNullable(object)%0A%09%09%09.filter(o -%3E type.isAssignableFrom(o.getClass()))%0A%09%09%09.map(type::cast);%0A%09%7D%0A%0A
 %09@Su

Simplify class.