views:

46

answers:

3

Suppose you have 2 Properties objects. One contains master properties, the other one is a target. Your job is to compare the two.

masterValue = masterProperties.getProperty(masterKey);


for (Properties targetFileProperty : targetFileList) {
   if (targetFileProperty.containsKey(masterKey)) {
   targetValue = targetFileProperty.getProperty(masterKey);

   if (masterValue.equals(targetValue)) { //<---- this is where the problem is
      // do something clever
   } else {
      // do something clever

The problem i am facing in this example is this:

When master key is "A" and master value is "10" and target key is "A" and "target key is " 10 ", code above thinks that these are the same. In other words it is either trimming or ignoring white space.

Can you eithe point out an error in my logic or suggest a better way to assert that white space is not to be ignored? Thank you.

+3  A: 

java.util.Properties inherits its equals(Object) from Hashtable, which implements Map equality:

Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. More formally, two maps m1 and m2 represent the same mappings if m1.entrySet().equals(m2.entrySet()). This ensures that the equals method works properly across different implementations of the Map interface.

So if you want to compare if two Properties contain the same keys and values, you can just do props1.equals(props2).

As for your problem, I don't think you've identified the real problem. " 10 ".equals("10") is definitely false. The problem may be that the strings were trimmed before entered as values to the Properties (you can print the values that you're comparing to see if this is indeed the case).

If the whitespaces are significant, you must escape it in the properties file. Here's a snippet to show how it's done:

    Properties p = new Properties();
    p.put("X", "   ");
    p.store(System.out, "test");

This prints (I've substituted _ for space for clarity):

#test
#Mon Jun 21 22:20:04 ICT 2010
X=\___
polygenelubricants
+1  A: 

When reading properties using the getProperty() method white space is ignored. This might explain:

A natural line that contains only white space characters is considered blank and is ignored. A comment line has an ASCII '#' or '!' as its first non-white space character; comment lines are also ignored and do not encode key-element information. In addition to line terminators, this method considers the characters space (' ', '\u0020'), tab ('\t', '\u0009'), and form feed ('\f', '\u000C') to be white space.

This page has more details.

You may have to escape the leading and trailing spaces in the properties file if they are important to you.

Vincent Ramdhanie
A: 

Since the method getProperty returns a String there shouldn't be any caveats hidden in

masterValue.equals(targetValue)

I think that what is happening is that the property value is trimmed when it's added to the Properties map.

Did you check by calling targetFileProperty.getProperty("A") that the value returned by the obect is effectively " 10 " and not "10"? Because if it's "10" this means that the setProperty(String key, String value) has a trimmed implementation but I just checked it and it's defined as

public synchronized Object setProperty(String key, String value) {
    return put(key, value);
}

so that shouldn't be your problem..

are you sure that strings you are putting in different properties are different? This may sound trivial but I don't see any easy explaination..

Jack