001: package net.sourceforge.importscrubber;
002:
003: import java.util.*;
004:
005: public class StatementFormat {
006: public static final int BREAK_EACH_PACKAGE = 0;
007: public static final int BREAK_NONE = 1;
008:
009: private static List _formats = new ArrayList(2);
010:
011: static {
012: ResourceBundle res = ResourceBundle
013: .getBundle("net.sourceforge.importscrubber.Resources");
014: _formats.add(res.getString(Resources.BREAK_EACH_PACKAGE));
015: _formats.add(res.getString(Resources.BREAK_NONE));
016: }
017:
018: public static Object keyToValue(int key) {
019: return _formats.get(key);
020: }
021:
022: public static int valueToKey(Object value) {
023: return _formats.indexOf(value);
024: }
025:
026: public static Iterator formatIterator() {
027: return _formats.iterator();
028: }
029:
030: // get a description of args understood by getFormat
031: public static String getUsage() {
032: return "-sortJavaLibsHigh: place 'import java...' statements at the top [default: off]\n"
033: + "-combineThreshold <integer>: import * from a package when MORE THAN this number of imports are found [default: 0 = off]\n"
034: + "-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"
035: + "-breakEachPackage: linebreak after the imports from each package [default: off]\n";
036: }
037:
038: // arg parsing done here to make it easier for others to add new features
039: public static StatementFormat getFormat(String[] args) {
040: int breakStyle = BREAK_NONE;
041: int combineThreshold = 0;
042: boolean sortJavaLibsHigh = ImportScrubber.argExists(
043: "sortjavalibshigh", args);
044: boolean thresholdStandardOnly = ImportScrubber.argExists(
045: "thresholdStandardOnly", args);
046: if (ImportScrubber.argExists("combineThreshold", args)) {
047: combineThreshold = new Integer(ImportScrubber.findArg(
048: "combineThreshold", args)).intValue();
049: }
050: if (ImportScrubber.argExists("breakEachPackage", args)) {
051: breakStyle = BREAK_EACH_PACKAGE;
052: }
053:
054: return new StatementFormat(sortJavaLibsHigh, breakStyle,
055: combineThreshold, thresholdStandardOnly);
056: }
057:
058: //
059: // the actual format stuff
060:
061: private boolean _sortJavaLibsHigh;
062: private int _breakStyle;
063: private int _combineThreshold;
064: private boolean _thresholdStandardOnly; // true if threshold should only apply to standard libraries
065: private List identical; // for applyFormat -- stored here so we avoid re-allocating each time
066:
067: public StatementFormat(boolean sortJavaLibsHigh, int breakStyle,
068: int combineThreshold, boolean thresholdStandardOnly) {
069: _sortJavaLibsHigh = sortJavaLibsHigh;
070: _breakStyle = breakStyle;
071: _combineThreshold = combineThreshold;
072: _thresholdStandardOnly = thresholdStandardOnly;
073: identical = new ArrayList(_combineThreshold);
074: }
075:
076: public StringBuffer applyFormat(List list) {
077: Collections.sort(list, new ImportStatementComparator(
078: _sortJavaLibsHigh));
079:
080: // pre-process the threshold requirement
081: if (_combineThreshold > 0) {
082: List oldList = list;
083: list = new ArrayList();
084: String lastPackage = null;
085: identical.clear();
086: for (Iterator i = oldList.iterator(); i.hasNext();) {
087: ImportStatement stmt = (ImportStatement) i.next();
088: if (lastPackage == null
089: || lastPackage.compareTo(stmt.getPackage()) != 0) {
090: if (identical.size() > _combineThreshold) {
091: list
092: .add(new ImportStatement(lastPackage
093: + ".*"));
094: } else {
095: list.addAll(identical);
096: }
097: identical.clear();
098: lastPackage = stmt.getPackage();
099: }
100: if (_thresholdStandardOnly
101: && !(stmt.isInStdJavaLibrary() || stmt
102: .isInStdJavaExtensionLibrary())) {
103: list.add(stmt);
104: } else {
105: identical.add(stmt);
106: }
107: }
108: if (identical.size() > _combineThreshold) {
109: list.add(new ImportStatement(lastPackage + ".*"));
110: } else {
111: list.addAll(identical);
112: }
113: }
114:
115: StringBuffer result = new StringBuffer();
116: ImportStatement last = null;
117: for (Iterator i = list.iterator(); i.hasNext();) {
118: ImportStatement next = (ImportStatement) i.next();
119: if (_breakStyle == BREAK_EACH_PACKAGE) {
120: if (last != null
121: && !last.getPackage().equals(next.getPackage())) {
122: result.append(ImportScrubber.LINE_SEPARATOR);
123: }
124: }
125: last = next;
126: result.append(next.getFormattedStmt());
127: }
128: return result;
129: }
130: }
|