KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > importscrubber > StatementFormat


1 package net.sourceforge.importscrubber;
2
3 import java.util.*;
4
5 public class StatementFormat
6 {
7     public static final int BREAK_EACH_PACKAGE = 0;
8     public static final int BREAK_NONE = 1;
9
10     private static List _formats = new ArrayList(2);
11
12     static {
13         ResourceBundle res = ResourceBundle.getBundle("net.sourceforge.importscrubber.Resources");
14         _formats.add(res.getString(Resources.BREAK_EACH_PACKAGE));
15         _formats.add(res.getString(Resources.BREAK_NONE));
16     }
17
18     public static Object JavaDoc keyToValue(int key)
19     {
20         return _formats.get(key);
21     }
22     public static int valueToKey(Object JavaDoc value)
23     {
24         return _formats.indexOf(value);
25     }
26
27     public static Iterator formatIterator()
28     {
29         return _formats.iterator();
30     }
31
32     // get a description of args understood by getFormat
33
public static String JavaDoc getUsage()
34     {
35         return "-sortJavaLibsHigh: place 'import java...' statements at the top [default: off]\n"
36                + "-combineThreshold <integer>: import * from a package when MORE THAN this number of imports are found [default: 0 = off]\n"
37                + "-thresholdStandardOnly: apply combineThreshold only to java standard library; other packages will continue to import individual classes no matter how many are used [default: off]\n"
38                + "-breakEachPackage: linebreak after the imports from each package [default: off]\n";
39     }
40
41     // arg parsing done here to make it easier for others to add new features
42
public static StatementFormat getFormat(String JavaDoc[] args)
43     {
44         int breakStyle = BREAK_NONE;
45         int combineThreshold = 0;
46         boolean sortJavaLibsHigh = ImportScrubber.argExists("sortjavalibshigh", args);
47         boolean thresholdStandardOnly = ImportScrubber.argExists("thresholdStandardOnly", args);
48         if (ImportScrubber.argExists("combineThreshold", args)) {
49             combineThreshold = new Integer JavaDoc(ImportScrubber.findArg("combineThreshold", args)).intValue();
50         }
51         if (ImportScrubber.argExists("breakEachPackage", args)) {
52             breakStyle = BREAK_EACH_PACKAGE;
53         }
54
55         return new StatementFormat(sortJavaLibsHigh, breakStyle, combineThreshold, thresholdStandardOnly);
56     }
57
58     //
59
// the actual format stuff
60

61     private boolean _sortJavaLibsHigh;
62     private int _breakStyle;
63     private int _combineThreshold;
64     private boolean _thresholdStandardOnly; // true if threshold should only apply to standard libraries
65
private List identical; // for applyFormat -- stored here so we avoid re-allocating each time
66

67     public StatementFormat(boolean sortJavaLibsHigh, int breakStyle, int combineThreshold, boolean thresholdStandardOnly)
68     {
69         _sortJavaLibsHigh = sortJavaLibsHigh;
70         _breakStyle = breakStyle;
71         _combineThreshold = combineThreshold;
72         _thresholdStandardOnly = thresholdStandardOnly;
73         identical = new ArrayList(_combineThreshold);
74     }
75
76     public StringBuffer JavaDoc applyFormat(List list)
77     {
78         Collections.sort(list, new ImportStatementComparator(_sortJavaLibsHigh));
79
80         // pre-process the threshold requirement
81
if (_combineThreshold > 0) {
82             List oldList = list;
83             list = new ArrayList();
84             String JavaDoc lastPackage = null;
85             identical.clear();
86             for (Iterator i = oldList.iterator(); i.hasNext(); ) {
87                 ImportStatement stmt = (ImportStatement)i.next();
88                 if (lastPackage == null || lastPackage.compareTo(stmt.getPackage()) != 0) {
89                     if (identical.size() > _combineThreshold) {
90                         list.add(new ImportStatement(lastPackage + ".*"));
91                     } else {
92                         list.addAll(identical);
93                     }
94                     identical.clear();
95                     lastPackage = stmt.getPackage();
96                 }
97                 if (_thresholdStandardOnly
98                         && !(stmt.isInStdJavaLibrary() || stmt.isInStdJavaExtensionLibrary())) {
99                     list.add(stmt);
100                 } else {
101                     identical.add(stmt);
102                 }
103             }
104             if (identical.size() > _combineThreshold) {
105                 list.add(new ImportStatement(lastPackage + ".*"));
106             } else {
107                 list.addAll(identical);
108             }
109         }
110
111         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
112         ImportStatement last = null;
113         for (Iterator i = list.iterator(); i.hasNext(); ) {
114             ImportStatement next = (ImportStatement)i.next();
115             if (_breakStyle == BREAK_EACH_PACKAGE) {
116                 if (last != null && !last.getPackage().equals(next.getPackage())) {
117                     result.append(ImportScrubber.LINE_SEPARATOR);
118                 }
119             }
120             last = next;
121             result.append(next.getFormattedStmt());
122         }
123         return result;
124     }
125 }
126
Popular Tags