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

com.softicar.platform.common.code.java.JavaImports Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.code.java;

import com.softicar.platform.common.core.java.classes.name.JavaClassName;
import com.softicar.platform.common.core.java.packages.name.JavaPackageName;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeSet;

/**
 * This class manages imports for a Java class file.
 *
 * @author Oliver Richers
 */
public class JavaImports implements Iterable {

	private final static JavaPackageName JAVA_LANGUAGE_PACKAGE = new JavaPackageName("java.lang");
	private final Set imports = new TreeSet<>();

	/**
	 * Returns true if the set of imports is empty.
	 *
	 * @return true no imports exist
	 */
	public boolean isEmpty() {

		return imports.isEmpty();
	}

	@Override
	public Iterator iterator() {

		return getImports().iterator();
	}

	/**
	 * Returns all imports as a set of strings.
	 *
	 * @return all imports
	 */
	public Set getImports() {

		return Collections.unmodifiableSet(imports);
	}

	public void addImport(JavaClass javaClass) {

		addImport(javaClass.getName());
		addImports(javaClass.getImports());
	}

	/**
	 * Adds an import for the given class.
	 * 

* Imports for primitive types and array types will be ignored. * * @param javaClass * the class to import */ public void addImport(Class javaClass) { // ignore classes than cannot be imported if (javaClass.isPrimitive() || javaClass.isArray()) { return; } addImport(new JavaClassName(javaClass)); } /** * Adds the given imports using the {@link #addImport(Class)} method. * * @param classImports * iterable of class imports */ public void addImports(Collection> classImports) { for (Class classImport: classImports) { addImport(classImport); } } /** * Adds an import for the given class. *

* Imports for arrays and imports from the package java.lang will be * ignored. * * @param className * the class name to all */ public void addImport(JavaClassName className) { // ignore imports for arrays String simpleName = className.getSimpleName(); if (simpleName.startsWith("[") || simpleName.endsWith("[]")) { return; } // ignore imports for default package JavaPackageName packageName = className.getPackageName(); if (packageName == null) { return; } // ignore imports for Java language package if (packageName.equals(JAVA_LANGUAGE_PACKAGE)) { return; } doAddImport(className); } /** * Adds the given imports using the {@link #addImport(JavaClassName)} * method. * * @param classNames * iterable of class names */ public void addImports(Iterable classNames) { for (JavaClassName className: classNames) { addImport(className); } } /** * Adds the given imports using the {@link #addImport(JavaClassName)} * method. *

* The second set specifies a set of simple class names, for filtering the * class imports. Only those class imports will be added, which are * referencing a class in the second set. * * @param classNames * set of class names * @param simpleNames * a set of simple class names used for filtering the imports */ public void addImportsFor(Set classNames, Set simpleNames) { for (JavaClassName className: classNames) { String simpleName = className.getSimpleName(); if (simpleNames.contains(simpleName)) { addImport(className); } } } /** * This method can be overridden to add filtering or mapping of imports. * * @param className * the {@link JavaClassName} to all (never null) */ protected void doAddImport(JavaClassName className) { imports.add(className); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy