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

com.github.bloodshura.ignitium.cfg.json.mapping.CollectionInstantiator Maven / Gradle / Ivy

Go to download

A collection of configuration and serialization readers and writers, like JSON, internationalization (I18n), and CSV.

There is a newer version: 1.0.1
Show newest version
package com.github.bloodshura.ignitium.cfg.json.mapping;

import com.github.bloodshura.ignitium.collection.list.XList;
import com.github.bloodshura.ignitium.collection.list.impl.XArrayList;
import com.github.bloodshura.ignitium.collection.map.XMap;
import com.github.bloodshura.ignitium.collection.map.impl.XLinkedMap;
import com.github.bloodshura.ignitium.collection.set.XSet;
import com.github.bloodshura.ignitium.collection.set.impl.XArraySet;
import com.github.bloodshura.ignitium.collection.store.XStore;
import com.github.bloodshura.ignitium.collection.view.XBasicView;
import com.github.bloodshura.ignitium.collection.view.XView;

import javax.annotation.Nonnull;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

final class CollectionInstantiator {
	@Nonnull
	public static Constructor getViewConstructor(@Nonnull Class type) throws JsonMappingException {
		try {
			return type.getDeclaredConstructor(XStore.class);
		} catch (NoSuchMethodException exception) {
			try {
				return type.getDeclaredConstructor(XList.class);
			} catch (NoSuchMethodException exception2) {
				try {
					return type.getDeclaredConstructor(XArrayList.class);
				} catch (NoSuchMethodException exception3) {
					throw new JsonMappingException(type.getName() + " is a XView subclass but does not implement a constructor which parameter type is XStore, or XList, or XArrayList");
				}
			}
		}
	}

	@Nonnull
	public static List instantiateList(@Nonnull Class type, int initialCapacity) throws ClassCastException, IllegalAccessException, InstantiationException {
		List list;

		if (Modifier.isAbstract(type.getModifiers()) || Modifier.isInterface(type.getModifiers())) {
			list = new ArrayList<>(initialCapacity);
		} else {
			list = (List) type.newInstance();

			if (list instanceof ArrayList) {
				ArrayList arrayList = (ArrayList) list;

				arrayList.ensureCapacity(initialCapacity);
			}
		}

		return list;
	}

	@Nonnull
	public static Map instantiateMap(@Nonnull Class type, int initialCapacity) throws ClassCastException, IllegalAccessException, InstantiationException {
		Map map;

		if (Modifier.isAbstract(type.getModifiers()) || Modifier.isInterface(type.getModifiers())) {
			map = new HashMap<>();
		} else {
			map = (Map) type.newInstance();
		}

		return map;
	}

	@Nonnull
	public static Set instantiateSet(@Nonnull Class type, int initialCapacity) throws ClassCastException, IllegalAccessException, InstantiationException {
		Set set;

		if (Modifier.isAbstract(type.getModifiers()) || Modifier.isInterface(type.getModifiers())) {
			set = new HashSet<>(initialCapacity);
		} else {
			set = (Set) type.newInstance();
		}

		return set;
	}

	@Nonnull
	public static XList instantiateXList(@Nonnull Class type, int initialCapacity) throws ClassCastException, IllegalAccessException, InstantiationException {
		XList list;

		if (Modifier.isAbstract(type.getModifiers()) || Modifier.isInterface(type.getModifiers())) {
			list = new XArrayList<>(initialCapacity);
		} else {
			list = (XList) type.newInstance();

			if (list instanceof XArrayList) {
				XArrayList arrayList = (XArrayList) list;

				arrayList.ensureCapacity(initialCapacity);
			}
		}

		return list;
	}

	@Nonnull
	public static XMap instantiateXMap(@Nonnull Class type, int initialCapacity) throws ClassCastException, IllegalAccessException, InstantiationException {
		XMap map;

		if (Modifier.isAbstract(type.getModifiers()) || Modifier.isInterface(type.getModifiers())) {
			map = new XLinkedMap();
		} else {
			map = (XMap) type.newInstance();
		}

		return map;
	}

	@Nonnull
	public static XSet instantiateXSet(@Nonnull Class type, int initialCapacity) throws ClassCastException, IllegalAccessException, InstantiationException {
		XSet set;

		if (Modifier.isAbstract(type.getModifiers()) || Modifier.isInterface(type.getModifiers())) {
			set = new XArraySet<>(initialCapacity);
		} else {
			set = (XSet) type.newInstance();

			if (set instanceof XArraySet) {
				XArraySet arraySet = (XArraySet) set;

				arraySet.ensureCapacity(initialCapacity);
			}
		}

		return set;
	}

	@Nonnull
	public static XView instantiateXView(@Nonnull Class type, @Nonnull XList content) throws ClassCastException, IllegalAccessException, IllegalArgumentException, InstantiationException, InvocationTargetException {
		XView view;

		if (Modifier.isAbstract(type.getModifiers()) || type.isInterface()) {
			view = new XBasicView<>(content);
		} else {
			Constructor constructor = getViewConstructor(type);

			view = (XView) constructor.newInstance(content);
		}

		return view;
	}
}