01: package org.apache.ojb.broker.query;
02:
03: /* Copyright 2002-2005 The Apache Software Foundation
04: *
05: * Licensed under the Apache License, Version 2.0 (the "License");
06: * you may not use this file except in compliance with the License.
07: * You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: import java.util.*;
19:
20: /**
21: * This Class contains utility functions for Criterias.
22: *
23: * @author <a href="mailto:on@ibis.odessa.ua">Oleg Nitz</a>
24: * @version $Id: CriteriaUtils.java,v 1.5.2.1 2005/12/21 22:27:09 tomdz Exp $
25: */
26: public class CriteriaUtils {
27:
28: /**
29: * Disjunctive Normal Form: list of Criteria, which don't contain ORs,
30: * the elements of the list joined by ORs give the condition equivalent
31: * to the original Criteria.
32: */
33: public static List getDNF(Criteria crit) {
34: List dnf = new ArrayList();
35: Enumeration e = crit.getElements();
36: Criteria tmpCrit;
37:
38: while (e.hasMoreElements()) {
39: Object o = e.nextElement();
40: if (o instanceof Criteria) {
41: Criteria pc = (Criteria) o;
42: switch (pc.getType()) {
43: case (Criteria.OR): {
44: dnf.addAll(getDNF(pc));
45: break;
46: }
47: case (Criteria.AND): {
48: dnf = getDnfAndDnf(dnf, getDNF(pc));
49: break;
50: }
51: }
52: } else {
53: SelectionCriteria c = (SelectionCriteria) o;
54: tmpCrit = new Criteria();
55: tmpCrit.getCriteria().add(c);
56: if (dnf.isEmpty()) {
57: dnf.add(tmpCrit);
58: } else {
59: //#ifdef JDK13
60: dnf = getDnfAndDnf(dnf, Collections
61: .singletonList(tmpCrit));
62: //#else
63: /*
64: Vector singletonList = new Vector(1);
65: singletonList.add(tmpCrit);
66: dnf = getDnfAndDnf(dnf,singletonList);
67:
68: */
69: //#endif
70:
71: }
72: }
73: } // while
74:
75: return dnf;
76: }
77:
78: /**
79: * (a OR b) AND (c OR d) -> (a AND c) OR (a AND d) OR (b AND c) OR (b AND d)
80: */
81: private static List getDnfAndDnf(List dnf1, List dnf2) {
82: ArrayList dnf = new ArrayList();
83:
84: for (Iterator it1 = dnf1.iterator(); it1.hasNext();) {
85: Criteria crit1 = (Criteria) it1.next();
86:
87: for (Iterator it2 = dnf2.iterator(); it2.hasNext();) {
88: Criteria crit2 = (Criteria) it2.next();
89: Criteria crit = new Criteria();
90: crit.getCriteria().addAll(crit1.getCriteria());
91: crit.getCriteria().addAll(crit2.getCriteria());
92: dnf.add(crit);
93: }
94: }
95:
96: return dnf;
97: }
98: }
|