Author: joehni
Date: Tue Sep 22 13:14:58 2015
New Revision: 1704617
URL: http://svn.apache.org/viewvc?rev=1704617&view=rev
Log:
VFS-297: Fix FileSystemOptions.compareTo(). Implement equals() and hashCode().
Added:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileSystemOptionsTest.java
(with props)
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileSystemOptions.java
Modified:
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileSystemOptions.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileSystemOptions.java?rev=1704617&r1=1704616&r2=1704617&view=diff
==============================================================================
---
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileSystemOptions.java
(original)
+++
commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/FileSystemOptions.java
Tue Sep 22 13:14:58 2015
@@ -16,7 +16,10 @@
*/
package org.apache.commons.vfs2;
+import java.util.Arrays;
+import java.util.Iterator;
import java.util.Map;
+import java.util.SortedMap;
import java.util.TreeMap;
/**
@@ -167,8 +170,27 @@ public final class FileSystemOptions imp
return 0;
}
- final int hash = options.hashCode();
- final int hashFk = other.options.hashCode();
+ // ensure proper sequence of options
+ final SortedMap<FileSystemOptionKey, Object> myOptions =
+ options instanceof SortedMap
+ ? (SortedMap<FileSystemOptionKey, Object>)options
+ : new TreeMap<FileSystemOptionKey, Object>(options);
+ final SortedMap<FileSystemOptionKey, Object> theirOptions =
+ other.options instanceof SortedMap
+ ? (SortedMap<FileSystemOptionKey, Object>)other.options
+ : new TreeMap<FileSystemOptionKey, Object>(other.options);
+ final Iterator<FileSystemOptionKey> optKeysIter =
myOptions.keySet().iterator();
+ final Iterator<FileSystemOptionKey> otherKeysIter =
theirOptions.keySet().iterator();
+ while(optKeysIter.hasNext()) {
+ int comp = optKeysIter.next().compareTo(otherKeysIter.next());
+ if (comp != 0) {
+ return comp;
+ }
+ }
+
+ Object[] array = new Object[propsSz];
+ final int hash =
Arrays.deepHashCode(myOptions.values().toArray(array));
+ final int hashFk =
Arrays.deepHashCode(theirOptions.values().toArray(array));
if (hash < hashFk)
{
return -1;
@@ -178,10 +200,44 @@ public final class FileSystemOptions imp
return 1;
}
- // bad props not the same instance, but looks like the same
- // TODO: compare Entry by Entry
+ // TODO: compare Entry by Entry ??
return 0;
}
+
+ @Override
+ public int hashCode()
+ {
+ final int prime = 31;
+ int result = 1;
+ if (options == null) {
+ result = prime * result;
+ } else {
+ final SortedMap<FileSystemOptionKey, Object> myOptions =
+ options instanceof SortedMap
+ ? (SortedMap<FileSystemOptionKey, Object>)options
+ : new TreeMap<FileSystemOptionKey, Object>(options);
+ result = prime * result + myOptions.keySet().hashCode();
+ result = prime * result +
Arrays.deepHashCode(myOptions.values().toArray(new Object[options.size()]));
+ }
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj)
+ {
+ if (this == obj) {
+ return true;
+ }
+ if (obj == null) {
+ return false;
+ }
+ if (getClass() != obj.getClass()) {
+ return false;
+ }
+ FileSystemOptions other = (FileSystemOptions)obj;
+ return compareTo(other) == 0;
+ }
+
/**
* {@inheritDoc}
Added:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileSystemOptionsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileSystemOptionsTest.java?rev=1704617&view=auto
==============================================================================
---
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileSystemOptionsTest.java
(added)
+++
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileSystemOptionsTest.java
Tue Sep 22 13:14:58 2015
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.commons.vfs2;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Check FileSystemOptions.
+ *
+ * @since 2.1
+ */
+public class FileSystemOptionsTest {
+
+ @Test
+ public void testEqualsHashCodeAndCompareTo() {
+ final JUnitConfigBuilder builder = JUnitConfigBuilder.getInstance();
+ final FileSystemOptions expected = new FileSystemOptions();
+ builder.setId(expected, "Test");
+
+ final FileSystemOptions actual = new FileSystemOptions();
+ builder.setId(actual, "Test");
+
+ Assert.assertEquals(expected, actual);
+ Assert.assertEquals(0, actual.compareTo(expected));
+ Assert.assertEquals(expected.hashCode(), actual.hashCode());
+
+ builder.setNames(expected, new String[]{"A", "B", "C"});
+
+ Assert.assertNotEquals(expected, actual);
+ Assert.assertEquals(-1, actual.compareTo(expected));
+ Assert.assertNotEquals(expected.hashCode(), actual.hashCode());
+
+ builder.setNames(actual, new String[]{"A", "B", "C"});
+
+ Assert.assertEquals(expected, actual);
+ Assert.assertEquals(0, actual.compareTo(expected));
+ Assert.assertEquals(expected.hashCode(), actual.hashCode());
+ }
+
+ public static class JUnitConfigBuilder extends FileSystemConfigBuilder
+ {
+ private static final JUnitConfigBuilder BUILDER = new
JUnitConfigBuilder();
+
+ public static JUnitConfigBuilder getInstance()
+ {
+ return BUILDER;
+ }
+
+ public void setId(final FileSystemOptions opts, final String id)
+ {
+ setParam(opts, "id", id);
+ }
+
+ public void setNames(final FileSystemOptions opts, final String[]
names)
+ {
+ setParam(opts, "names", names);
+ }
+
+ @Override
+ protected Class<? extends FileSystem> getConfigClass()
+ {
+ return JUnitFS.class;
+ }
+
+ private abstract static class JUnitFS implements FileSystem
+ {
+ }
+ }
+
+}
Propchange:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileSystemOptionsTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/FileSystemOptionsTest.java
------------------------------------------------------------------------------
svn:keywords = Author Id HeadURL Revision