KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > algebra > AlgebraTools


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor.algebra;
24
25 import java.util.*;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 /**
31  * Static utilities for relational attributes
32  */

33 public final class AlgebraTools {
34
35     private static final String JavaDoc RCSRevision = "$Revision: 1.13 $";
36     private static final String JavaDoc RCSName = "$Name: $";
37
38     private static Log log = LogFactory.getLog(AlgebraTools.class);
39
40     public static List clone(Collection collection) throws CloneNotSupportedException JavaDoc {
41         return cloneCopy(collection, new ArrayList());
42     }
43
44     public static List cloneCopy(Collection source, List dest) throws CloneNotSupportedException JavaDoc {
45         Object JavaDoc obj = null;
46         Iterator it = source.iterator();
47         while (it.hasNext()) {
48             obj = it.next();
49
50             if (obj instanceof Expression) {
51                 dest.add(((Expression) obj).clone());
52             } else if (obj instanceof MapItem) {
53                 dest.add(((MapItem) obj).clone());
54             } else if (obj instanceof SqlType) {
55                 dest.add(((SqlType) obj).clone());
56             } else if (obj instanceof String JavaDoc) {
57                 dest.add(obj);
58             } else if (null == obj) {
59                 dest.add(null);
60             } else {
61                 log.error("object to be cloned is not Expression" + obj);
62                 throw new CloneNotSupportedException JavaDoc("Error in cloning ArrayList" + obj);
63             }
64         }
65         return dest;
66     }
67
68     public static List cloneCopyExpressionsWithoutDoubles(Collection source, List dest) throws CloneNotSupportedException JavaDoc {
69         Expression expr = null;
70         Iterator it = source.iterator();
71         while (it.hasNext()) {
72             expr = (Expression) it.next();
73
74             if (!UnOpProject.hasNameConflict(expr, dest))
75                 dest.add(((Expression) expr).clone());
76         }
77
78         return dest;
79     }
80
81     public static List clone(List list, Map clonedExprs) throws CloneNotSupportedException JavaDoc {
82         if (null == list) {
83             return null;
84         }
85         List retVal = new ArrayList();
86
87         for (int i = 0; i < list.size(); i++) {
88             Object JavaDoc obj = list.get(i);
89         
90             if (obj instanceof Expression) {
91                 retVal.add(((Expression) obj).clone(clonedExprs));
92             } else if (obj instanceof MapItem) {
93                 retVal.add(((MapItem) obj).clone());
94             } else if (obj instanceof SqlType) {
95                 retVal.add(((SqlType) obj).clone());
96             } else if (obj instanceof String JavaDoc) {
97                 retVal.add(new String JavaDoc((String JavaDoc) obj));
98             } else {
99                 log.error("object to be cloned is not Expression" + obj);
100                 throw new CloneNotSupportedException JavaDoc("Error in cloning ArrayList" + obj);
101             }
102         }
103
104         return retVal;
105     }
106
107     /* Compares deeply expression without considering order */
108     public static boolean areExprListEquivalent(List exprList1, List exprList2) {
109         int len1 = exprList1.size(), len2 = exprList2.size();
110         boolean ret = (len1 == len2), found;
111         LinkedList optList2 = new LinkedList(exprList2);
112         // clone is performed for optimization
113
// and matching expression are removed (linked list is more efficient)
114
Expression expr;
115         ListIterator it;
116
117         /* Browse the reference list */
118         for (int i = 0; ret && i < len1; i++) {
119             expr = (Expression) exprList1.get(i);
120             it = optList2.listIterator();
121             found = false;
122             /* browse the compared cloned list and remove item that matches */
123             while (it.hasNext()) {
124                 if (expr.deepEquals(it.next())) {
125                     it.remove();
126                     found = true;
127                 }
128             }
129             ret &= found;
130         }
131         return ret;
132     }
133
134     public static void applyVisitorOnCollection(AlgebraVisitor visitor, List exprList) {
135         applyVisitorOnCollection(visitor, exprList, true);
136     }
137
138     public static void applyVisitorOnCollection(AlgebraVisitor visitor, List exprList, boolean preserve) {
139         if (exprList != null) {
140             ListIterator it = exprList.listIterator();
141             while (it.hasNext()) {
142                 ((Expression) it.next()).accept(visitor);
143                 if (!preserve)
144                     it.set(visitor.getVisitedExpression());
145             }
146         }
147     }
148 }
149
Popular Tags