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  
17  package org.apache.commons.configuration;
18  
19  import java.util.Iterator;
20  
21  /***
22   * Strict comparator for configurations.
23   *
24   * @since 1.0
25   *
26   * @author <a href="mailto:herve.quiroz@esil.univ-mrs.fr">Herve Quiroz</a>
27   * @author <a href="mailto:shapira@mpi.com">Yoav Shapira</a>
28   * @version $Revision$, $Date: 2005-02-26 13:56:39 +0100 (Sat, 26 Feb 2005) $
29   */
30  public class StrictConfigurationComparator implements ConfigurationComparator
31  {
32      /***
33       * Create a new strict comparator.
34       */
35      public StrictConfigurationComparator()
36      {
37      }
38  
39      /***
40       * Compare two configuration objects.
41       *
42       * @param a the first configuration
43       * @param b the second configuration
44       * @return true if keys from a are found in b and keys from b are
45       *         found in a and for each key in a, the corresponding value
46       *         is the sale in for the same key in b
47       */
48      public boolean compare(Configuration a, Configuration b)
49      {
50          if (a == null && b == null)
51          {
52              return true;
53          }
54          else if (a == null || b == null)
55          {
56              return false;
57          }
58  
59          for (Iterator keys = a.getKeys(); keys.hasNext();)
60          {
61              String key = (String) keys.next();
62              Object value = a.getProperty(key);
63              if (!value.equals(b.getProperty(key)))
64              {
65                  return false;
66              }
67          }
68  
69          for (Iterator keys = b.getKeys(); keys.hasNext();)
70          {
71              String key = (String) keys.next();
72              Object value = b.getProperty(key);
73              if (!value.equals(a.getProperty(key)))
74              {
75                  return false;
76              }
77          }
78  
79          return true;
80      }
81  }