View Javadoc

1   /*
2    * Copyright 2001-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.neethi.util;
17  
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import org.apache.neethi.All;
22  import org.apache.neethi.Assertion;
23  import org.apache.neethi.ExactlyOne;
24  import org.apache.neethi.Policy;
25  import org.apache.neethi.PolicyComponent;
26  
27  /**
28   * A Utility class that provides methods the check the equality of
29   * PolicyComponents.
30   * 
31   */
32  public class PolicyComparator {
33  
34      /**
35       * Returns <tt>true</tt> if the two policies have the same semantics
36       * 
37       * @param arg1
38       *            a Policy
39       * @param arg2
40       *            an another Policy
41       * @return <tt>true</tt> if both policies have the same semantics
42       */
43      public static boolean compare(Policy arg1, Policy arg2) {
44          return compare(arg1.getPolicyComponents(), arg2.getPolicyComponents());
45      }
46  
47      /**
48       * Returns <tt>true</tt> if the two PolicyComponents have the same
49       * semantics.
50       * 
51       * @param arg1
52       *            a PolicyComponent
53       * @param arg2
54       *            an another PolicyComponent
55       * @return <tt>true</tt> if both PolicyComponents have the same semantics
56       */
57      public static boolean compare(PolicyComponent arg1, PolicyComponent arg2) {
58          if (!arg1.getClass().equals(arg2.getClass())) {
59              return false;
60          }
61  
62          if (arg1 instanceof Policy) {
63              return compare((Policy) arg1, (Policy) arg2);
64  
65          } else if (arg1 instanceof All) {
66              return compare((All) arg1, (All) arg2);
67  
68          } else if (arg1 instanceof ExactlyOne) {
69              return compare((ExactlyOne) arg1, (ExactlyOne) arg2);
70  
71          } else if (arg1 instanceof Assertion) {
72              return compare((Assertion) arg1, (Assertion) arg2);
73  
74          } else {
75              // TODO should I throw an exception ..
76          }
77  
78          return false;
79      }
80  
81      public static boolean compare(All arg1, All arg2) {
82          return compare(arg1.getPolicyComponents(), arg2.getPolicyComponents());
83      }
84  
85      public static boolean compare(ExactlyOne arg1, ExactlyOne arg2) {
86          return compare(arg1.getPolicyComponents(), arg2.getPolicyComponents());
87      }
88  
89      public static boolean compare(Assertion arg1, Assertion arg2) {
90          if (!(arg1.getName().equals(arg2.getName()))) {
91              return false;
92          }
93          return true;
94      }
95  
96      private static boolean compare(List arg1, List arg2) {
97          if (arg1.size() != arg2.size()) {
98              return false;
99          }
100 
101         Iterator iterator = arg1.iterator();
102         PolicyComponent assertion1;
103 
104         while (iterator.hasNext()) {
105             assertion1 = (PolicyComponent) iterator.next();
106 
107             Iterator iterator2 = arg2.iterator();
108             boolean match = false;
109             PolicyComponent assertion2;
110 
111             while (iterator2.hasNext()) {
112                 assertion2 = (PolicyComponent) iterator2.next();
113                 if (compare(assertion1, assertion2)) {
114                     match = true;
115                     break;
116                 }
117             }
118 
119             if (!match) {
120                 return false;
121             }
122         }
123         return true;
124     }
125 }