package org.jcodegen.java;

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

import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

/**
 * ...
 */
public class ImportsManager {

    private Set<String> imports = new HashSet<String>();

    private static final Pattern importValidationPattern;

    static {
        try {
            importValidationPattern = Pattern.compile("([_a-zA-Z][_\\w]+\\.)+[_a-zA-Z][_\\w]+(\\.\\*)?");
        } catch (final PatternSyntaxException pse) {
            throw new RuntimeException(pse);
        }
    }


    public void addImport(final String importStr) {
        final Matcher matcher = importValidationPattern.matcher(importStr);
        if (!matcher.matches())
            throw new IllegalArgumentException("Illegal character in import name: " + importStr);

        imports.add(importStr);
    }

    public void clear() {
        imports.clear();
    }

    public int getCount() {
        return imports.size();
    }

    public String getImports() {
        final List<String> javaImports = new ArrayList<String>();
        final List<String> otherImports = new ArrayList<String>();

        for (String importStr: imports) {
            if (importStr.startsWith("java.") || importStr.startsWith("javax."))
                javaImports.add(importStr);
            else
                otherImports.add(importStr);
        }

        final StringBuilder buf = new StringBuilder();
        if (javaImports.size() > 0) {
            Collections.sort(javaImports);
            addImports(buf, javaImports);
            buf.append("\n");
        }
        if (otherImports.size() > 0) {
            Collections.sort(otherImports);
            addImports(buf, otherImports);
            buf.append("\n");
        }

        return buf.toString();
    }

    private void addImports(final StringBuilder buf, final List<String> imports) {
        String lastSubPackageName = "";

        for (String importStr: imports) {
            final String subPackageName = getPackageName(importStr);

            if (!subPackageName.equals(lastSubPackageName) && !lastSubPackageName.equals(""))
                buf.append("\n");

            buf.append("import ");
            buf.append(importStr);
            buf.append(";\n");

            lastSubPackageName = subPackageName;
        }
    }

    private String getPackageName(final String importStr) {
        return importStr.substring(0, importStr.lastIndexOf("."));
    }
}

@@ -321,16 +321,22 @@
 private 
+final 
 Set%3CStri
@@ -361,22 +361,79 @@
 HashSet%3C
-String
+%3E();%0A    private final Set%3CString%3E staticImports = new HashSet%3C
 %3E();%0A%0A  
@@ -639,22 +639,16 @@
  catch (
-final 
 PatternS
@@ -758,22 +758,16 @@
 dImport(
-final 
 String i
@@ -786,22 +786,16 @@
         
-final 
 Matcher 
@@ -1010,32 +1010,330 @@
 ortStr);%0A    %7D%0A%0A
+    public void addStaticImport(String importStr) %7B%0A        Matcher matcher = importValidationPattern.matcher(importStr);%0A        if (!matcher.matches())%0A            throw new IllegalArgumentException(%22Illegal character in import name: %22 + importStr);%0A%0A        staticImports.add(importStr);%0A    %7D%0A%0A
     public void 
@@ -1363,24 +1363,55 @@
 ts.clear();%0A
+        staticImports.clear();%0A
     %7D%0A%0A    p
@@ -1463,32 +1463,116 @@
 .size();%0A    %7D%0A%0A
+    public int getStaticImportCount() %7B%0A        return staticImports.size();%0A    %7D%0A%0A
     public Strin
@@ -1588,38 +1588,32 @@
 rts() %7B%0A        
-final 
 List%3CString%3E jav
@@ -1633,30 +1633,24 @@
 w ArrayList%3C
-String
 %3E();%0A       
@@ -1650,22 +1650,16 @@
         
-final 
 List%3CStr
@@ -1692,22 +1692,16 @@
 rayList%3C
-String
 %3E();%0A%0A  
@@ -1942,30 +1942,24 @@
  %7D%0A%0A        
-final 
 StringBuilde
@@ -2309,32 +2309,148 @@
 n%22);%0A        %7D%0A%0A
+        if (!staticImports.isEmpty()) %7B%0A            addStaticImports(buf);%0A            buf.append(%22%5Cn%22);%0A        %7D%0A%0A
         return b
@@ -2499,22 +2499,16 @@
 Imports(
-final 
 StringBu
@@ -2517,22 +2517,16 @@
 der buf,
- final
  List%3CSt
@@ -2637,22 +2637,16 @@
         
-final 
 String s
@@ -2978,32 +2978,602 @@
        %7D%0A    %7D%0A%0A
+    private void addStaticImports(StringBuilder buf) %7B%0A        List%3CString%3E imports = new ArrayList%3C%3E(staticImports);%0A        Collections.sort(imports);%0A%0A        String lastClassName = %22%22;%0A%0A        for (String importStr: imports) %7B%0A            String className = getPackageName(importStr);%0A%0A            if (!className.equals(lastClassName) && !lastClassName.equals(%22%22))%0A                buf.append(%22%5Cn%22);%0A%0A            buf.append(%22import static %22);%0A            buf.append(importStr);%0A            buf.append(%22;%5Cn%22);%0A%0A            lastClassName = className;%0A        %7D%0A    %7D%0A%0A
     private Stri
@@ -3590,22 +3590,16 @@
 ageName(
-final 
 String i
@@ -3683,10 +3683,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

Add support for static imports