1 22 package org.enhydra.kelp.common.map; 23 24 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 28 public class MapUtil { 30 public static String [][] optimize(String [][] in) { 31 String [][] out = new String [0][0]; 32 MapEntry[] entries = new MapEntry[0]; 33 34 entries = MapUtil.toEntryArray(in); 35 entries = MapUtil.optimize(entries); 36 out = MapUtil.toStringArray(entries); 37 return out; 38 } 39 40 public static MapEntry[] optimize(MapEntry[] in) { 41 MapEntry[] out = new MapEntry[0]; 42 43 out = reduce(in); 44 out = sort(out); 45 return out; 46 } 47 48 public static MapEntry[] combine(MapEntry[] setA, MapEntry[] setB) { 49 ArrayList listA = new ArrayList (Arrays.asList(setA)); 50 MapEntry[] both = new MapEntry[0]; 51 52 for (int i = 0; i < setB.length; i++) { 53 if (!listA.contains(setB[i])) { 54 listA.add(setB[i]); 55 } 56 } 57 listA.trimToSize(); 58 both = new MapEntry[listA.size()]; 59 both = (MapEntry[]) listA.toArray(both); 60 both = MapUtil.optimize(both); 61 return both; 62 } 63 64 public static MapEntry[] toEntryArray(String [][] map) { 65 MapEntry[] entries = new MapEntry[map.length]; 66 67 for (int i = 0; i < map.length; i++) { 68 entries[i] = new MapEntry(map[i][0], map[i][1]); 69 } 70 return entries; 71 } 72 73 public static String [][] toStringArray(MapEntry[] entries) { 74 String [][] map = new String [entries.length][2]; 75 76 for (int i = 0; i < entries.length; i++) { 77 map[i][0] = entries[i].getFrom(); 78 map[i][1] = entries[i].getTo(); 79 } 80 return map; 81 } 82 83 private static MapEntry[] sort(MapEntry[] in) { 86 MapEntry[] out = new MapEntry[0]; 87 ArrayList inList = null; 88 ArrayList outList = null; 89 90 inList = new ArrayList (Arrays.asList(in)); 91 outList = new ArrayList (); 92 while (inList.size() > 0) { 93 MapEntry max = (MapEntry) inList.get(0); 94 95 for (int i = 0; i < inList.size(); i++) { 96 MapEntry cursor = null; 97 98 cursor = (MapEntry) inList.get(i); 99 if (max.getFrom().length() < cursor.getFrom().length()) { 100 max = cursor; 101 } 102 } 103 outList.add(max); 104 inList.remove(max); 105 } 106 inList.trimToSize(); 107 outList.trimToSize(); 108 out = new MapEntry[outList.size()]; 109 out = (MapEntry[]) outList.toArray(out); 110 outList.clear(); 111 return out; 112 } 113 114 private static MapEntry[] reduce(MapEntry[] in) { 115 MapEntry[] out = new MapEntry[0]; 116 ArrayList list = null; 117 118 list = new ArrayList (Arrays.asList(in)); 119 for (int i = 0; i < in.length; i++) { 120 MapEntry cursor = null; 121 122 cursor = in[i]; 123 for (int j = 0; j < in.length; j++) { 124 if (cursor.canReduce(in[j])) { 125 if (list.contains(in[j])) { 126 list.remove(in[j]); 127 } 128 } 129 } 130 } 131 list.trimToSize(); 132 out = new MapEntry[list.size()]; 133 out = (MapEntry[]) list.toArray(out); 134 list.clear(); 135 return out; 136 } 137 138 } 139 | Popular Tags |