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.configuration2;
019
020import java.util.Iterator;
021
022/**
023 * Strict comparator for configurations.
024 *
025 * @since 1.0
026 *
027 */
028public class StrictConfigurationComparator implements ConfigurationComparator {
029    /**
030     * Create a new strict comparator.
031     */
032    public StrictConfigurationComparator() {
033    }
034
035    /**
036     * Compare two configuration objects.
037     *
038     * @param a the first configuration
039     * @param b the second configuration
040     * @return true if keys from a are found in b and keys from b are found in a and for each key in a, the corresponding
041     *         value is the sale in for the same key in b
042     */
043    @Override
044    public boolean compare(final Configuration a, final Configuration b) {
045        if (a == null && b == null) {
046            return true;
047        }
048        if (a == null || b == null) {
049            return false;
050        }
051
052        for (final Iterator<String> keys = a.getKeys(); keys.hasNext();) {
053            final String key = keys.next();
054            final Object value = a.getProperty(key);
055            if (!value.equals(b.getProperty(key))) {
056                return false;
057            }
058        }
059
060        for (final Iterator<String> keys = b.getKeys(); keys.hasNext();) {
061            final String key = keys.next();
062            final Object value = b.getProperty(key);
063            if (!value.equals(a.getProperty(key))) {
064                return false;
065            }
066        }
067
068        return true;
069    }
070}