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

fr.lteconsulting.roaster.JavaClassText Maven / Gradle / Ivy

There is a newer version: 1.8
Show newest version
package fr.lteconsulting.roaster;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class JavaClassText
{
	private BlockImpl rootBlock;
	private Set imports;

	public JavaClassText( String pack )
	{
		imports = new HashSet<>();

		rootBlock = new BlockImpl();

		rootBlock.line( "package [{package name}];", pack );
		rootBlock.line( "" );

		rootBlock.addAny( this::renderImports );
	}

	public Block rootBlock()
	{
		return rootBlock;
	}

	public void render( StringBuilder sb )
	{
		rootBlock.render( sb );
	}

	private void renderImports( StringBuilder sb )
	{
		List list = new ArrayList<>( imports );
		Collections.sort( list );
		for( String imp : imports )
			sb.append( "import " + imp + ";\n" );
		if( !list.isEmpty() )
			sb.append( "\n" );
	}

	public void addImport( String imp )
	{
		imports.add( imp );
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy