Java Properties differentKeys(Properties p1, Properties p2)

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

Description

Compare two Properties, return the set of keys whose values differ, including keys that don't exist in one or the other Properties).

License

Open Source License

Parameter

Parameter Description
p1 first Properties
p2 second Properties

Return

Set of keys whose values differ. Returns empty set if there are no differences.

Declaration

public static Set differentKeys(Properties p1, Properties p2) 

Method Source Code


//package com.java2s;
/*//from w  ww .j ava 2 s. 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, return the set of keys whose values differ,
     * including keys that don't exist in one or the other Properties).
     * @param p1 first Properties
     * @param p2 second Properties
     * @return Set of keys whose values differ.  Returns empty set if there
     * are no differences.
     */
    public static Set differentKeys(Properties p1, Properties p2) {
        if (p1 == null) {
            if (p2 == null) {
                return null;
            } else {
                return p2.keySet();
            }
        } else if (p2 == null) {
            return p1.keySet();
        }
        Set res = new HashSet();
        Set keys1 = p1.keySet();
        for (Iterator iter = keys1.iterator(); iter.hasNext();) {
            String k1 = (String) iter.next();
            if (!isKeySame(k1, p1, p2)) {
                res.add(k1);
            }
        }
        // add all the keys in p2 that don't appear in p1
        Set p2Only = new HashSet(p2.keySet());
        p2Only.removeAll(keys1);
        res.addAll(p2Only);
        return res;
    }

    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. equalProps(Properties p1, Properties p2)
  5. extractFromPropertiesAsList(String prefix, Properties properties)
  6. extractFromPropertiesAsMap(String prefix, Properties properties)
  7. isKeySame(String key, Properties p1, Properties p2)