Java Properties equalProps(Properties p1, Properties p2)

Here you can find the source of equalProps(Properties p1, Properties p2)

Description

Compare two Properties for equality (same set of properties with same (.equals) values.

License

Open Source License

Parameter

Parameter Description
p1 first Properties
p2 second Properties

Return

true iff Properties are equal

Declaration

public static boolean equalProps(Properties p1, Properties p2) 

Method Source Code


//package com.java2s;
/*//from  ww w  .j a  va  2s .c  o  m
    
Copyright (c) 2000-2003 Board of Trustees of Leland Stanford Jr. University,
all rights reserved.
    
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
    
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
    
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
STANFORD UNIVERSITY BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    
Except as contained in this notice, the name of Stanford University shall not
be used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Stanford University.
    
*/

import java.util.*;

public class Main {
    private static final String noProp = new String();

    /**
     * Compare two Properties for equality (same set of properties with
     * same (.equals) values.
     * @param p1 first Properties
     * @param p2 second Properties
     * @return true iff Properties are equal
     */
    public static boolean equalProps(Properties p1, Properties p2) {
        if (p1 == null || p2 == null || p1.size() != p2.size()) {
            return false;
        }
        Enumeration en = p1.keys();
        while (en.hasMoreElements()) {
            String k1 = (String) en.nextElement();
            if (!isKeySame(k1, p1, p2)) {
                return false;
            }
        }
        return true;
    }

    private static boolean isKeySame(String key, Properties p1, Properties p2) {
        Object o1 = p1.getProperty(key);
        Object o2 = p2.getProperty(key, noProp);
        return (o1 == o2) ||
        // o2 == noProp means p2 didn't have key that p1 has.
        // we know o1 != o2, so if o1 == null, o2 != null.
                !((o2 == noProp || o1 == null || !o1.equals(o2)));
    }
}

Related

  1. convertBundleToProperties(ResourceBundle rb)
  2. convertResourceBundleToProperties(ResourceBundle resource)
  3. createScriptBodyFromScriptSet(Properties scriptSet)
  4. differentKeys(Properties p1, Properties p2)
  5. extractFromPropertiesAsList(String prefix, Properties properties)
  6. extractFromPropertiesAsMap(String prefix, Properties properties)
  7. isKeySame(String key, Properties p1, Properties p2)
  8. isSupportedJVM(Map jdkProperties)