1 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 33 public final class AlgebraTools { 34 35 private static final String RCSRevision = "$Revision: 1.13 $"; 36 private static final String RCSName = "$Name: $"; 37 38 private static Log log = LogFactory.getLog(AlgebraTools.class); 39 40 public static List clone(Collection collection) throws CloneNotSupportedException { 41 return cloneCopy(collection, new ArrayList()); 42 } 43 44 public static List cloneCopy(Collection source, List dest) throws CloneNotSupportedException { 45 Object 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 ) { 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 ("Error in cloning ArrayList" + obj); 63 } 64 } 65 return dest; 66 } 67 68 public static List cloneCopyExpressionsWithoutDoubles(Collection source, List dest) throws CloneNotSupportedException { 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 { 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 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 ) { 97 retVal.add(new String ((String ) obj)); 98 } else { 99 log.error("object to be cloned is not Expression" + obj); 100 throw new CloneNotSupportedException ("Error in cloning ArrayList" + obj); 101 } 102 } 103 104 return retVal; 105 } 106 107 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 Expression expr; 115 ListIterator it; 116 117 118 for (int i = 0; ret && i < len1; i++) { 119 expr = (Expression) exprList1.get(i); 120 it = optList2.listIterator(); 121 found = false; 122 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 |