KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > map > MapUtil


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  */

22 package org.enhydra.kelp.common.map;
23
24 // Standard imports
25
import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27
28 //
29
public class MapUtil {
30     public static String JavaDoc[][] optimize(String JavaDoc[][] in) {
31         String JavaDoc[][] out = new String JavaDoc[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 JavaDoc listA = new ArrayList JavaDoc(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 JavaDoc[][] 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 JavaDoc[][] toStringArray(MapEntry[] entries) {
74         String JavaDoc[][] map = new String JavaDoc[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     //
84
//
85
private static MapEntry[] sort(MapEntry[] in) {
86         MapEntry[] out = new MapEntry[0];
87         ArrayList JavaDoc inList = null;
88         ArrayList JavaDoc outList = null;
89
90         inList = new ArrayList JavaDoc(Arrays.asList(in));
91         outList = new ArrayList JavaDoc();
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 JavaDoc list = null;
117
118         list = new ArrayList JavaDoc(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