All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.mostlyoriginal.api.utils.ClassHierarchy Maven / Gradle / Ivy

Go to download

Drop-in low throughput synchronous immediate delivery eventbus for artemis-odb.

There is a newer version: 2.5.0
Show newest version
package net.mostlyoriginal.api.utils;

import com.artemis.utils.Bag;

import java.util.ArrayList;
import java.util.IdentityHashMap;

/**
 * @author DaanVanYperen
 */
public class ClassHierarchy {

	IdentityHashMap, Class[]> hierarchyCache = new IdentityHashMap<>();

	/** Flatten hierarchy of class, front to back. */
	public Class[] of(Class origin)
	{
		Class[] hierarchy = hierarchyCache.get(origin);
		if ( hierarchy == null )
		{
			hierarchy = getFlatHierarchyUncached(origin);
			hierarchyCache.put(origin, hierarchy);
		}
		return hierarchy;
	}

	/** Flatten hierarchy of class, front to back. Excluding Object. */
	private Class[] getFlatHierarchyUncached(Class origin) {
		ArrayList> result = new ArrayList<>(4);
		Class clazz = origin;
		do {
			result.add(clazz);
		} while ((clazz = clazz.getSuperclass()) != Object.class);

		return result.toArray(new Class[result.size()]);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy