001: package com.mockrunner.gen.proc;
002:
003: import java.util.ArrayList;
004: import java.util.Collections;
005: import java.util.HashMap;
006: import java.util.Iterator;
007: import java.util.List;
008: import java.util.Map;
009: import java.util.Set;
010: import java.util.TreeSet;
011:
012: public class PackageImportSorter {
013: private String[] order;
014:
015: public PackageImportSorter() {
016: order = new String[4];
017: order[0] = "java";
018: order[1] = "javax";
019: order[2] = "org";
020: order[3] = "com";
021: }
022:
023: public List sortBlocks(List imports) {
024: Map groups = new HashMap();
025: Set sortedGroups = new TreeSet();
026: List resultList = new ArrayList();
027: initializeGroups(imports, groups, sortedGroups);
028: if (groups.isEmpty()) {
029: prepareSingleBlock(imports, resultList);
030: return resultList;
031: }
032: classifyImports(groups, sortedGroups);
033: prepareResultList(groups, resultList);
034: return resultList;
035: }
036:
037: private void prepareSingleBlock(List imports, List resultList) {
038: Set block = new TreeSet(imports);
039: resultList.add(block);
040: }
041:
042: private List getGroupsAsSortedList() {
043: Set allGroupSet = new TreeSet();
044: for (int ii = 0; ii < order.length; ii++) {
045: allGroupSet.add(order[ii]);
046: }
047: List allGroups = new ArrayList(allGroupSet);
048: Collections.reverse(allGroups);
049: return allGroups;
050: }
051:
052: private void prepareResultList(Map groups, List resultList) {
053: for (int ii = 0; ii < order.length; ii++) {
054: Group currentGroup = (Group) groups.get(order[ii]);
055: if (null != currentGroup) {
056: addIfNotEmpty(resultList, currentGroup.getBeforeGroup());
057: addIfNotEmpty(resultList, currentGroup.getActualGroup());
058: addIfNotEmpty(resultList, currentGroup.getAfterGroup());
059: }
060: }
061: }
062:
063: private void addIfNotEmpty(List list, Set set) {
064: if (null == set || set.isEmpty())
065: return;
066: list.add(set);
067: }
068:
069: private void initializeGroups(List imports, Map groups,
070: Set sortedGroups) {
071: List allGroups = getGroupsAsSortedList();
072: sortedGroups.addAll(imports);
073: for (int ii = 0; ii < imports.size(); ii++) {
074: String currentImport = (String) imports.get(ii);
075: createGroupIfMatching(allGroups, groups, sortedGroups,
076: currentImport);
077: }
078: sortedGroups.addAll(groups.keySet());
079: }
080:
081: private void createGroupIfMatching(List allGroups, Map groups,
082: Set sortedGroups, String currentImport) {
083: for (int ii = 0; ii < allGroups.size(); ii++) {
084: String groupName = (String) allGroups.get(ii);
085: if (currentImport.startsWith(groupName)) {
086: Group group = getGroupByName(groups, groupName);
087: group.addToActualGroup(currentImport);
088: sortedGroups.remove(currentImport);
089: return;
090: }
091: }
092: }
093:
094: private Group getGroupByName(Map groups, String groupName) {
095: Group group = (Group) groups.get(groupName);
096: if (null == group) {
097: group = new Group(groupName);
098: groups.put(groupName, group);
099: }
100: return group;
101: }
102:
103: private void classifyImports(Map groups, Set sortedGroups) {
104: Group currentGroup = null;
105: Set tempBeforeGroup = new TreeSet();
106: Iterator iterator = sortedGroups.iterator();
107: while (iterator.hasNext()) {
108: String currentImport = (String) iterator.next();
109: Group tempGroup = (Group) groups.get(currentImport);
110: currentGroup = handleTempGroup(currentGroup,
111: tempBeforeGroup, currentImport, tempGroup);
112: }
113: }
114:
115: private Group handleTempGroup(Group currentGroup,
116: Set tempBeforeGroup, String currentImport, Group tempGroup) {
117: if (null != tempGroup) {
118: if (null == currentGroup) {
119: tempGroup.addAllToBeforeGroup(tempBeforeGroup);
120: }
121: currentGroup = tempGroup;
122: } else {
123: if (null == currentGroup) {
124: tempBeforeGroup.add(currentImport);
125: } else {
126: currentGroup.addToAfterGroup(currentImport);
127: }
128: }
129: return currentGroup;
130: }
131:
132: private class Group {
133: private String groupName;
134: private Set beforeGroup;
135: private Set actualGroup;
136: private Set afterGroup;
137:
138: public Group(String groupName) {
139: this .groupName = groupName;
140: beforeGroup = new TreeSet();
141: actualGroup = new TreeSet();
142: afterGroup = new TreeSet();
143: }
144:
145: public String getGroupName() {
146: return groupName;
147: }
148:
149: public Set getActualGroup() {
150: return actualGroup;
151: }
152:
153: public Set getAfterGroup() {
154: return afterGroup;
155: }
156:
157: public Set getBeforeGroup() {
158: return beforeGroup;
159: }
160:
161: public void addAllToBeforeGroup(Set importSet) {
162: beforeGroup.addAll(importSet);
163: }
164:
165: public void addAllToActualGroup(Set importSet) {
166: actualGroup.addAll(importSet);
167: }
168:
169: public void addAllToAfterGroup(Set importSet) {
170: afterGroup.addAll(importSet);
171: }
172:
173: public void addToBeforeGroup(String importString) {
174: beforeGroup.add(importString);
175: }
176:
177: public void addToActualGroup(String importString) {
178: actualGroup.add(importString);
179: }
180:
181: public void addToAfterGroup(String importString) {
182: afterGroup.add(importString);
183: }
184: }
185: }
|