001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.configuration;
019
020import java.util.Iterator;
021
022/**
023 * Strict comparator for configurations.
024 *
025 * @since 1.0
026 *
027 * @author <a href="mailto:herve.quiroz@esil.univ-mrs.fr">Herve Quiroz</a>
028 * @author <a href="mailto:shapira@mpi.com">Yoav Shapira</a>
029 * @version $Id: StrictConfigurationComparator.java 1210177 2011-12-04 18:58:19Z oheger $
030 */
031public class StrictConfigurationComparator implements ConfigurationComparator
032{
033    /**
034     * Create a new strict comparator.
035     */
036    public StrictConfigurationComparator()
037    {
038    }
039
040    /**
041     * Compare two configuration objects.
042     *
043     * @param a the first configuration
044     * @param b the second configuration
045     * @return true if keys from a are found in b and keys from b are
046     *         found in a and for each key in a, the corresponding value
047     *         is the sale in for the same key in b
048     */
049    public boolean compare(Configuration a, Configuration b)
050    {
051        if (a == null && b == null)
052        {
053            return true;
054        }
055        else if (a == null || b == null)
056        {
057            return false;
058        }
059
060        for (Iterator<String> keys = a.getKeys(); keys.hasNext();)
061        {
062            String key = keys.next();
063            Object value = a.getProperty(key);
064            if (!value.equals(b.getProperty(key)))
065            {
066                return false;
067            }
068        }
069
070        for (Iterator<String> keys = b.getKeys(); keys.hasNext();)
071        {
072            String key = keys.next();
073            Object value = b.getProperty(key);
074            if (!value.equals(a.getProperty(key)))
075            {
076                return false;
077            }
078        }
079
080        return true;
081    }
082}