001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: *
021: */
022: package org.enhydra.kelp.common.map;
023:
024: // Standard imports
025: import java.util.ArrayList;
026: import java.util.Arrays;
027:
028: //
029: public class MapUtil {
030: public static String[][] optimize(String[][] in) {
031: String[][] out = new String[0][0];
032: MapEntry[] entries = new MapEntry[0];
033:
034: entries = MapUtil.toEntryArray(in);
035: entries = MapUtil.optimize(entries);
036: out = MapUtil.toStringArray(entries);
037: return out;
038: }
039:
040: public static MapEntry[] optimize(MapEntry[] in) {
041: MapEntry[] out = new MapEntry[0];
042:
043: out = reduce(in);
044: out = sort(out);
045: return out;
046: }
047:
048: public static MapEntry[] combine(MapEntry[] setA, MapEntry[] setB) {
049: ArrayList listA = new ArrayList(Arrays.asList(setA));
050: MapEntry[] both = new MapEntry[0];
051:
052: for (int i = 0; i < setB.length; i++) {
053: if (!listA.contains(setB[i])) {
054: listA.add(setB[i]);
055: }
056: }
057: listA.trimToSize();
058: both = new MapEntry[listA.size()];
059: both = (MapEntry[]) listA.toArray(both);
060: both = MapUtil.optimize(both);
061: return both;
062: }
063:
064: public static MapEntry[] toEntryArray(String[][] map) {
065: MapEntry[] entries = new MapEntry[map.length];
066:
067: for (int i = 0; i < map.length; i++) {
068: entries[i] = new MapEntry(map[i][0], map[i][1]);
069: }
070: return entries;
071: }
072:
073: public static String[][] toStringArray(MapEntry[] entries) {
074: String[][] map = new String[entries.length][2];
075:
076: for (int i = 0; i < entries.length; i++) {
077: map[i][0] = entries[i].getFrom();
078: map[i][1] = entries[i].getTo();
079: }
080: return map;
081: }
082:
083: //
084: //
085: private static MapEntry[] sort(MapEntry[] in) {
086: MapEntry[] out = new MapEntry[0];
087: ArrayList inList = null;
088: ArrayList outList = null;
089:
090: inList = new ArrayList(Arrays.asList(in));
091: outList = new ArrayList();
092: while (inList.size() > 0) {
093: MapEntry max = (MapEntry) inList.get(0);
094:
095: for (int i = 0; i < inList.size(); i++) {
096: MapEntry cursor = null;
097:
098: cursor = (MapEntry) inList.get(i);
099: 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: }
|