You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2015/11/22 06:21:19 UTC

[2/2] mina-sshd git commit: Moved some code to NumberUtils

Moved some code to NumberUtils


Project: http://git-wip-us.apache.org/repos/asf/mina-sshd/repo
Commit: http://git-wip-us.apache.org/repos/asf/mina-sshd/commit/82b8c928
Tree: http://git-wip-us.apache.org/repos/asf/mina-sshd/tree/82b8c928
Diff: http://git-wip-us.apache.org/repos/asf/mina-sshd/diff/82b8c928

Branch: refs/heads/master
Commit: 82b8c9288fbdc3afbac21308d15a688250152028
Parents: d966cb6
Author: Lyor Goldstein <lg...@vmware.com>
Authored: Sun Nov 22 07:21:05 2015 +0200
Committer: Lyor Goldstein <lg...@vmware.com>
Committed: Sun Nov 22 07:21:05 2015 +0200

----------------------------------------------------------------------
 .../openssh/OpenSSHStatExtensionInfo.java       | 17 ++---
 .../extensions/SpaceAvailableExtensionInfo.java | 11 ++-
 .../sshd/common/util/buffer/BufferUtils.java    | 14 +---
 .../sshd/common/util/NumberUtilsTest.java       | 74 ++++++++++++++++++++
 4 files changed, 85 insertions(+), 31 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/82b8c928/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/extensions/openssh/OpenSSHStatExtensionInfo.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/extensions/openssh/OpenSSHStatExtensionInfo.java b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/extensions/openssh/OpenSSHStatExtensionInfo.java
index f831533..a9cd944 100644
--- a/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/extensions/openssh/OpenSSHStatExtensionInfo.java
+++ b/sshd-core/src/main/java/org/apache/sshd/client/subsystem/sftp/extensions/openssh/OpenSSHStatExtensionInfo.java
@@ -19,7 +19,7 @@
 
 package org.apache.sshd.client.subsystem.sftp.extensions.openssh;
 
-import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.common.util.NumberUtils;
 import org.apache.sshd.common.util.buffer.Buffer;
 
 /**
@@ -58,18 +58,9 @@ public class OpenSSHStatExtensionInfo implements Cloneable {
 
     @Override
     public int hashCode() {
-        int result =  GenericUtils.hashCode(this.f_bsize);
-        result = 31 * result + GenericUtils.hashCode(this.f_frsize);
-        result = 31 * result + GenericUtils.hashCode(this.f_blocks);
-        result = 31 * result + GenericUtils.hashCode(this.f_bfree);
-        result = 31 * result + GenericUtils.hashCode(this.f_bavail);
-        result = 31 * result + GenericUtils.hashCode(this.f_files);
-        result = 31 * result + GenericUtils.hashCode(this.f_ffree);
-        result = 31 * result + GenericUtils.hashCode(this.f_favail);
-        result = 31 * result + GenericUtils.hashCode(this.f_fsid);
-        result = 31 * result + GenericUtils.hashCode(this.f_flag);
-        result = 31 * result + GenericUtils.hashCode(this.f_namemax);
-        return result;
+        return NumberUtils.hashCode(this.f_bsize, this.f_frsize, this.f_blocks,
+                this.f_bfree, this.f_bavail, this.f_files, this.f_ffree,
+                this.f_favail, this.f_fsid, this.f_flag, this.f_namemax);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/82b8c928/sshd-core/src/main/java/org/apache/sshd/common/subsystem/sftp/extensions/SpaceAvailableExtensionInfo.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/subsystem/sftp/extensions/SpaceAvailableExtensionInfo.java b/sshd-core/src/main/java/org/apache/sshd/common/subsystem/sftp/extensions/SpaceAvailableExtensionInfo.java
index 83dc914..16dc184 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/subsystem/sftp/extensions/SpaceAvailableExtensionInfo.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/subsystem/sftp/extensions/SpaceAvailableExtensionInfo.java
@@ -22,7 +22,7 @@ package org.apache.sshd.common.subsystem.sftp.extensions;
 import java.io.IOException;
 import java.nio.file.FileStore;
 
-import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.common.util.NumberUtils;
 import org.apache.sshd.common.util.buffer.Buffer;
 
 /**
@@ -58,12 +58,9 @@ public class SpaceAvailableExtensionInfo implements Cloneable {
 
     @Override
     public int hashCode() {
-        int result = GenericUtils.hashCode(bytesOnDevice);
-        result = 31 * result + GenericUtils.hashCode(unusedBytesOnDevice);
-        result = 31 * result + GenericUtils.hashCode(bytesAvailableToUser);
-        result = 31 * result + GenericUtils.hashCode(unusedBytesAvailableToUser);
-        result = 31 * result + bytesPerAllocationUnit;
-        return result;
+        return NumberUtils.hashCode(bytesOnDevice, unusedBytesOnDevice,
+                    bytesAvailableToUser, unusedBytesAvailableToUser,
+                    bytesPerAllocationUnit);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/82b8c928/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java
index d0143ab..676549f 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/util/buffer/BufferUtils.java
@@ -25,6 +25,7 @@ import java.io.StreamCorruptedException;
 
 import org.apache.sshd.common.util.GenericUtils;
 import org.apache.sshd.common.util.Int2IntFunction;
+import org.apache.sshd.common.util.NumberUtils;
 import org.apache.sshd.common.util.ValidateUtils;
 import org.apache.sshd.common.util.io.IoUtils;
 
@@ -310,18 +311,9 @@ public final class BufferUtils {
         }
         return true;
     }
-
-    public static int getNextPowerOf2(int i) {
+    public static int getNextPowerOf2(int value) {
         // for 0-7 return 8
-        if (i < Byte.SIZE) {
-            return Byte.SIZE;
-        }
-
-        int j = 1;
-        while (j < i) {
-            j <<= 1;
-        }
-        return j;
+        return (value < Byte.SIZE) ? Byte.SIZE : NumberUtils.getNextPowerOf2(value);
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/mina-sshd/blob/82b8c928/sshd-core/src/test/java/org/apache/sshd/common/util/NumberUtilsTest.java
----------------------------------------------------------------------
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/util/NumberUtilsTest.java b/sshd-core/src/test/java/org/apache/sshd/common/util/NumberUtilsTest.java
new file mode 100644
index 0000000..ed42e02
--- /dev/null
+++ b/sshd-core/src/test/java/org/apache/sshd/common/util/NumberUtilsTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.sshd.common.util;
+
+import org.apache.sshd.util.test.BaseTestSupport;
+import org.junit.FixMethodOrder;
+import org.junit.Test;
+import org.junit.runners.MethodSorters;
+
+/**
+ * @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
+ */
+@FixMethodOrder(MethodSorters.NAME_ASCENDING)
+public class NumberUtilsTest extends BaseTestSupport {
+    public NumberUtilsTest() {
+        super();
+    }
+
+    @Test
+    public void testPowersOf2List() {
+        assertEquals("Mismatched values size for " + NumberUtils.POWERS_OF_TWO, Long.SIZE, GenericUtils.size(NumberUtils.POWERS_OF_TWO));
+        long expected = 1L;
+        for (int index = 0; index < Long.SIZE; index++, expected <<= 1) {
+            Long actual = NumberUtils.POWERS_OF_TWO.get(index);
+            assertEquals("Mismatched value at index=" + index, Long.toHexString(expected), Long.toHexString(actual.longValue()));
+        }
+    }
+
+    @Test
+    public void testNextPowerOf2() {
+        for (Long v : NumberUtils.POWERS_OF_TWO) {
+            long expected = v.longValue();
+            if (expected > 2L) {
+                assertEquals("Mismatched lower bound value", expected, NumberUtils.getNextPowerOf2(expected - 1L));
+            }
+
+            if (expected > 0L) {    // avoid the negative value
+                assertEquals("Mismatched exact value", expected, NumberUtils.getNextPowerOf2(expected));
+            }
+        }
+    }
+
+    @Test
+    public void testToInteger() {
+        assertNull("Unexpected null value", NumberUtils.toInteger(null));
+        for (Number n : new Number[]{
+                Byte.valueOf(Byte.MAX_VALUE), Short.valueOf(Short.MIN_VALUE),
+                Integer.valueOf(Short.MAX_VALUE), Long.valueOf(82007160L)}) {
+            Integer i = NumberUtils.toInteger(n);
+            if (n instanceof Integer) {
+                assertSame("Unexpected conversion", n, i);
+            } else {
+                assertEquals("Mismatched values", n.intValue(), i.intValue());
+            }
+        }
+    }
+}