Bläddra i källkod

ZOOKEEPER-3926: Remove hardcoded comparisons to more maintainable an…

…d readable comparisons.

There are many more such instances in some tests etc but didn't change them here as the coding guidelines suggested to only make changes related to what is asked in the jira. Can take those up in a new Jira if this is accepted by the maintainers as worthwhile.

Author: Ghatage <ghatageanup@gmail.com>

Reviewers: Enrico Olivelli <eolivelli@apache.org>, Damien Diederen <ddiederen@apache.org>

Closes #1497 from Ghatage/ZOOKEEPER-3926
Anup Ghatage 4 år sedan
förälder
incheckning
b59bf6b40d

+ 10 - 10
zookeeper-server/src/main/java/org/apache/zookeeper/ClientCnxn.java

@@ -618,7 +618,7 @@ public class ClientCnxn {
                                || p.response instanceof SetDataResponse
                                || p.response instanceof SetACLResponse) {
                         StatCallback cb = (StatCallback) p.cb;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             if (p.response instanceof ExistsResponse) {
                                 cb.processResult(rc, clientPath, p.ctx, ((ExistsResponse) p.response).getStat());
                             } else if (p.response instanceof SetDataResponse) {
@@ -632,7 +632,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetDataResponse) {
                         DataCallback cb = (DataCallback) p.cb;
                         GetDataResponse rsp = (GetDataResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, clientPath, p.ctx, rsp.getData(), rsp.getStat());
                         } else {
                             cb.processResult(rc, clientPath, p.ctx, null, null);
@@ -640,7 +640,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetACLResponse) {
                         ACLCallback cb = (ACLCallback) p.cb;
                         GetACLResponse rsp = (GetACLResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, clientPath, p.ctx, rsp.getAcl(), rsp.getStat());
                         } else {
                             cb.processResult(rc, clientPath, p.ctx, null, null);
@@ -648,7 +648,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetChildrenResponse) {
                         ChildrenCallback cb = (ChildrenCallback) p.cb;
                         GetChildrenResponse rsp = (GetChildrenResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, clientPath, p.ctx, rsp.getChildren());
                         } else {
                             cb.processResult(rc, clientPath, p.ctx, null);
@@ -656,7 +656,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetAllChildrenNumberResponse) {
                         AllChildrenNumberCallback cb = (AllChildrenNumberCallback) p.cb;
                         GetAllChildrenNumberResponse rsp = (GetAllChildrenNumberResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, clientPath, p.ctx, rsp.getTotalNumber());
                         } else {
                             cb.processResult(rc, clientPath, p.ctx, -1);
@@ -664,7 +664,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetChildren2Response) {
                         Children2Callback cb = (Children2Callback) p.cb;
                         GetChildren2Response rsp = (GetChildren2Response) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, clientPath, p.ctx, rsp.getChildren(), rsp.getStat());
                         } else {
                             cb.processResult(rc, clientPath, p.ctx, null, null);
@@ -672,7 +672,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof CreateResponse) {
                         StringCallback cb = (StringCallback) p.cb;
                         CreateResponse rsp = (CreateResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(
                                 rc,
                                 clientPath,
@@ -686,7 +686,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof Create2Response) {
                         Create2Callback cb = (Create2Callback) p.cb;
                         Create2Response rsp = (Create2Response) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(
                                     rc,
                                     clientPath,
@@ -701,7 +701,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof MultiResponse) {
                         MultiCallback cb = (MultiCallback) p.cb;
                         MultiResponse rsp = (MultiResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             List<OpResult> results = rsp.getResultList();
                             int newRc = rc;
                             for (OpResult result : results) {
@@ -718,7 +718,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetEphemeralsResponse) {
                         EphemeralsCallback cb = (EphemeralsCallback) p.cb;
                         GetEphemeralsResponse rsp = (GetEphemeralsResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, p.ctx, rsp.getEphemerals());
                         } else {
                             cb.processResult(rc, p.ctx, null);

+ 5 - 4
zookeeper-server/src/main/java/org/apache/zookeeper/ZooKeeper.java

@@ -299,7 +299,7 @@ public class ZooKeeper implements AutoCloseable {
          * @return true if the watch should be added, otw false
          */
         protected boolean shouldAddWatch(int rc) {
-            return rc == 0;
+            return rc == KeeperException.Code.OK.intValue();
         }
 
     }
@@ -315,12 +315,13 @@ public class ZooKeeper implements AutoCloseable {
 
         @Override
         protected Map<String, Set<Watcher>> getWatches(int rc) {
-            return rc == 0 ? getWatchManager().getDataWatches() : getWatchManager().getExistWatches();
+            return rc == KeeperException.Code.OK.intValue()
+                    ? getWatchManager().getDataWatches() : getWatchManager().getExistWatches();
         }
 
         @Override
         protected boolean shouldAddWatch(int rc) {
-            return rc == 0 || rc == KeeperException.Code.NONODE.intValue();
+            return rc == KeeperException.Code.OK.intValue() || rc == KeeperException.Code.NONODE.intValue();
         }
 
     }
@@ -372,7 +373,7 @@ public class ZooKeeper implements AutoCloseable {
 
         @Override
         protected boolean shouldAddWatch(int rc) {
-            return rc == 0 || rc == KeeperException.Code.NONODE.intValue();
+            return rc == KeeperException.Code.OK.intValue() || rc == KeeperException.Code.NONODE.intValue();
         }
     }
 

+ 1 - 1
zookeeper-server/src/test/java/org/apache/zookeeper/server/NettyServerCnxnTest.java

@@ -302,7 +302,7 @@ public class NettyServerCnxnTest extends ClientBase {
                             zk.getData(path, null, new DataCallback() {
                                 @Override
                                 public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
-                                    if (rc == 0) {
+                                    if (rc == KeeperException.Code.OK.intValue()) {
                                         successResponse.addAndGet(1);
                                     } else {
                                         LOG.info("failed response is {}", rc);

+ 2 - 2
zookeeper-server/src/test/java/org/apache/zookeeper/test/IntegrityCheck.java

@@ -194,7 +194,7 @@ public class IntegrityCheck implements StatCallback, DataCallback {
     }
 
     public void processResult(int rc, String path, Object ctx, Stat stat) {
-        if (rc == 0) {
+        if (rc == KeeperException.Code.OK.intValue()) {
             lastValue.put(path, (byte[]) ctx);
         }
         decOutstanding();
@@ -202,7 +202,7 @@ public class IntegrityCheck implements StatCallback, DataCallback {
 
     public void processResult(
             int rc, String path, Object ctx, byte[] data, Stat stat) {
-        if (rc == 0) {
+        if (rc == KeeperException.Code.OK.intValue()) {
             String string = new String(data);
             String lastString = null;
             byte[] v = lastValue.get(path);

+ 1 - 1
zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentRecursiveWatcherTest.java

@@ -68,7 +68,7 @@ public class PersistentRecursiveWatcherTest extends ClientBase {
         try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
             final CountDownLatch latch = new CountDownLatch(1);
             AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
-                if (rc == 0) {
+                if (rc == KeeperException.Code.OK.intValue()) {
                     latch.countDown();
                 }
             };

+ 2 - 2
zookeeper-server/src/test/java/org/apache/zookeeper/test/PersistentWatcherTest.java

@@ -92,7 +92,7 @@ public class PersistentWatcherTest extends ClientBase {
         try (ZooKeeper zk = createClient(watcher, hostPort)) {
             final CountDownLatch latch = new CountDownLatch(1);
             AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
-                if (rc == 0) {
+                if (rc == KeeperException.Code.OK.intValue()) {
                     latch.countDown();
                 }
             };
@@ -109,7 +109,7 @@ public class PersistentWatcherTest extends ClientBase {
         try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
             final CountDownLatch latch = new CountDownLatch(1);
             AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
-                if (rc == 0) {
+                if (rc == KeeperException.Code.OK.intValue()) {
                     latch.countDown();
                 }
             };

+ 1 - 1
zookeeper-server/src/test/java/org/apache/zookeeper/test/WatcherTest.java

@@ -258,7 +258,7 @@ public class WatcherTest extends ClientBase {
             assertEquals(1, watches[i].events.size(), "For " + i);
         }
         for (int i = COUNT / 2; i < COUNT; i++) {
-            if (cbs[i].rc == 0) {
+            if (cbs[i].rc == KeeperException.Code.OK.intValue()) {
                 assertEquals(1, watches[i].events.size(), "For " + i);
             } else {
                 assertEquals(0, watches[i].events.size(), "For " + i);