<commit_before>/**
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package jlibs.core.annotation.processing;

import jlibs.core.lang.model.ModelUtil;

import javax.lang.model.element.TypeElement;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author <NAME>
 */
public class Printer{
    public TypeElement clazz;
    public String generatedQName;
    public String generatedPakage;
    public String generatedClazz;

    public int indent;
    PrintWriter ps;

    public Printer(PrintWriter ps){
        this.ps = ps;
    }

    private boolean doIndent = true;
    private void indent(){
        if(doIndent){
            if(emptyLine){
                println();
                emptyLine = false;
            }
            for(int i=0; i<indent; i++)
                ps.print("    ");
        }
    }
    public void println(String str){
        indent();
        ps.println(str);
        doIndent = true;
    }

    public void print(String str){
        indent();
        ps.print(str);
        doIndent = false;
    }

    private boolean emptyLine;
    public void emptyLine(boolean reqd){
        emptyLine = reqd;
    }

    public void println(){
        ps.println();
        doIndent = true;
    }

    public void close(){
        ps.close();
    }
    
    private static Map<TypeElement, Printer> registry = new HashMap<TypeElement, Printer>();
    public static Printer get(TypeElement clazz, Class annotation, String format) throws IOException{
        Printer printer = registry.get(clazz);
        if(printer==null){
            String str[] = ModelUtil.findClass(clazz, format);
            if(ModelUtil.exists(str[1], str[2]+".java"))
                throw new AnnotationError(clazz, ModelUtil.getAnnotationMirror(clazz, annotation), "Class "+str[0]+" already exists in source path");
            PrintWriter writer = new PrintWriter(Environment.get().getFiler().createSourceFile(str[0]).openWriter());
            printer=new Printer(writer);
            printer.clazz = clazz;
            printer.generatedQName = str[0];
            printer.generatedPakage = str[1];
            printer.generatedClazz = str[2];
            registry.put(clazz, printer);
        }
        return printer;
    }

    /*-------------------------------------------------[ Helpers ]---------------------------------------------------*/

    public void printPackage(){
        if(generatedPakage !=null && generatedPakage.length()>0){
            println("package "+generatedPakage +";");
            emptyLine(true);
        }
    }

    public void printClassDoc(){
        println("/**");<commit_msg>added following handy methods for better code readablity:     importClass(clazz)    importPackage(clazz)    println(tokens)    printlns(lines)<commit_after>/**
 * JLibs: Common Utilities for Java
 * Copyright (C) 2009  <NAME> <<EMAIL>>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

package jlibs.core.annotation.processing;

import jlibs.core.lang.model.ModelUtil;

import javax.lang.model.element.TypeElement;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author <NAME>
 */
public class Printer{
    public static final String PLUS = "indent++";
    public static final String MINUS = "indent--";
    
    public TypeElement clazz;
    public String generatedQName;
    public String generatedPakage;
    public String generatedClazz;

    public int indent;
    PrintWriter ps;

    public Printer(PrintWriter ps){
        this.ps = ps;
    }

    private boolean doIndent = true;
    private void indent(){
        if(doIndent){
            if(emptyLine){
                println();
                emptyLine = false;
            }
            for(int i=0; i<indent; i++)
                ps.print("    ");
        }
    }
    public void println(String str){
        indent();
        ps.println(str);
        doIndent = true;
    }

    public void println(String... tokens){
        for(String token: tokens)
            print(token);
        println();
    }
    
    @SuppressWarnings({"StringEquality"})
    public void printlns(String... lines){
        for(String line: lines){
            if(line==PLUS)
                indent++;
            else if(line==MINUS)
                indent--;
            else
                println(line);
        }
    }

    public void print(String str){
        indent();
        ps.print(str);
        doIndent = false;
    }

    private boolean emptyLine;
    public void emptyLine(boolean reqd){
        emptyLine = reqd;
    }

    public void println(){
        ps.println();
        doIndent = true;
    }

    public void close(){
        ps.close();
    }
    
    private static Map<TypeElement, Printer> registry = new HashMap<TypeElement, Printer>();
    public static Printer get(TypeElement clazz, Class annotation, String format) throws IOException{
        Printer printer = registry.get(clazz);
        if(printer==null){
            String str[] = ModelUtil.findClass(clazz, format);
            if(ModelUtil.exists(str[1], str[2]+".java"))
                throw new AnnotationError(clazz, ModelUtil.getAnnotationMirror(clazz, annotation), "Class "+str[0]+" already exists in source path");
            PrintWriter writer = new PrintWriter(Environment.get().getFiler().createSourceFile(str[0]).openWriter());
            printer=new Printer(writer);
            printer.clazz = clazz;
            printer.generatedQName = str[0];
            printer.generatedPakage = str[1];
            printer.generatedClazz = str[2];
            registry.put(clazz, printer);
        }
        return printer;
    }

    /*-------------------------------------------------[ Helpers ]---------------------------------------------------*/

    public void printPackage(){
        if(generatedPakage !=null && generatedPakage.length()>0){
            println("package "+generatedPakage +";");
            emptyLine(true);
        }
    }

    public void importClass(Class clazz){
        println("import "+ clazz.getName()+';');
    }

    public void importPackage(Class clazz){
        println("import "+ clazz.getPackage().getName()+".*;");
    }

    public void printClassDoc(){
        println("/**");
{'fraction_non_alphanumeric': 0.1331877729257642, 'fraction_numerical': 0.004093886462882096, 'mean_word_length': 2.176853055916775, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3315103', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1928, 'n_words': 574}