Просмотр исходного кода

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 лет назад
Родитель
Сommit
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 SetDataResponse
                                || p.response instanceof SetACLResponse) {
                                || p.response instanceof SetACLResponse) {
                         StatCallback cb = (StatCallback) p.cb;
                         StatCallback cb = (StatCallback) p.cb;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             if (p.response instanceof ExistsResponse) {
                             if (p.response instanceof ExistsResponse) {
                                 cb.processResult(rc, clientPath, p.ctx, ((ExistsResponse) p.response).getStat());
                                 cb.processResult(rc, clientPath, p.ctx, ((ExistsResponse) p.response).getStat());
                             } else if (p.response instanceof SetDataResponse) {
                             } else if (p.response instanceof SetDataResponse) {
@@ -632,7 +632,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetDataResponse) {
                     } else if (p.response instanceof GetDataResponse) {
                         DataCallback cb = (DataCallback) p.cb;
                         DataCallback cb = (DataCallback) p.cb;
                         GetDataResponse rsp = (GetDataResponse) p.response;
                         GetDataResponse rsp = (GetDataResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, clientPath, p.ctx, rsp.getData(), rsp.getStat());
                             cb.processResult(rc, clientPath, p.ctx, rsp.getData(), rsp.getStat());
                         } else {
                         } else {
                             cb.processResult(rc, clientPath, p.ctx, null, null);
                             cb.processResult(rc, clientPath, p.ctx, null, null);
@@ -640,7 +640,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetACLResponse) {
                     } else if (p.response instanceof GetACLResponse) {
                         ACLCallback cb = (ACLCallback) p.cb;
                         ACLCallback cb = (ACLCallback) p.cb;
                         GetACLResponse rsp = (GetACLResponse) p.response;
                         GetACLResponse rsp = (GetACLResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, clientPath, p.ctx, rsp.getAcl(), rsp.getStat());
                             cb.processResult(rc, clientPath, p.ctx, rsp.getAcl(), rsp.getStat());
                         } else {
                         } else {
                             cb.processResult(rc, clientPath, p.ctx, null, null);
                             cb.processResult(rc, clientPath, p.ctx, null, null);
@@ -648,7 +648,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetChildrenResponse) {
                     } else if (p.response instanceof GetChildrenResponse) {
                         ChildrenCallback cb = (ChildrenCallback) p.cb;
                         ChildrenCallback cb = (ChildrenCallback) p.cb;
                         GetChildrenResponse rsp = (GetChildrenResponse) p.response;
                         GetChildrenResponse rsp = (GetChildrenResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, clientPath, p.ctx, rsp.getChildren());
                             cb.processResult(rc, clientPath, p.ctx, rsp.getChildren());
                         } else {
                         } else {
                             cb.processResult(rc, clientPath, p.ctx, null);
                             cb.processResult(rc, clientPath, p.ctx, null);
@@ -656,7 +656,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetAllChildrenNumberResponse) {
                     } else if (p.response instanceof GetAllChildrenNumberResponse) {
                         AllChildrenNumberCallback cb = (AllChildrenNumberCallback) p.cb;
                         AllChildrenNumberCallback cb = (AllChildrenNumberCallback) p.cb;
                         GetAllChildrenNumberResponse rsp = (GetAllChildrenNumberResponse) p.response;
                         GetAllChildrenNumberResponse rsp = (GetAllChildrenNumberResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, clientPath, p.ctx, rsp.getTotalNumber());
                             cb.processResult(rc, clientPath, p.ctx, rsp.getTotalNumber());
                         } else {
                         } else {
                             cb.processResult(rc, clientPath, p.ctx, -1);
                             cb.processResult(rc, clientPath, p.ctx, -1);
@@ -664,7 +664,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetChildren2Response) {
                     } else if (p.response instanceof GetChildren2Response) {
                         Children2Callback cb = (Children2Callback) p.cb;
                         Children2Callback cb = (Children2Callback) p.cb;
                         GetChildren2Response rsp = (GetChildren2Response) p.response;
                         GetChildren2Response rsp = (GetChildren2Response) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, clientPath, p.ctx, rsp.getChildren(), rsp.getStat());
                             cb.processResult(rc, clientPath, p.ctx, rsp.getChildren(), rsp.getStat());
                         } else {
                         } else {
                             cb.processResult(rc, clientPath, p.ctx, null, null);
                             cb.processResult(rc, clientPath, p.ctx, null, null);
@@ -672,7 +672,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof CreateResponse) {
                     } else if (p.response instanceof CreateResponse) {
                         StringCallback cb = (StringCallback) p.cb;
                         StringCallback cb = (StringCallback) p.cb;
                         CreateResponse rsp = (CreateResponse) p.response;
                         CreateResponse rsp = (CreateResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(
                             cb.processResult(
                                 rc,
                                 rc,
                                 clientPath,
                                 clientPath,
@@ -686,7 +686,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof Create2Response) {
                     } else if (p.response instanceof Create2Response) {
                         Create2Callback cb = (Create2Callback) p.cb;
                         Create2Callback cb = (Create2Callback) p.cb;
                         Create2Response rsp = (Create2Response) p.response;
                         Create2Response rsp = (Create2Response) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(
                             cb.processResult(
                                     rc,
                                     rc,
                                     clientPath,
                                     clientPath,
@@ -701,7 +701,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof MultiResponse) {
                     } else if (p.response instanceof MultiResponse) {
                         MultiCallback cb = (MultiCallback) p.cb;
                         MultiCallback cb = (MultiCallback) p.cb;
                         MultiResponse rsp = (MultiResponse) p.response;
                         MultiResponse rsp = (MultiResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             List<OpResult> results = rsp.getResultList();
                             List<OpResult> results = rsp.getResultList();
                             int newRc = rc;
                             int newRc = rc;
                             for (OpResult result : results) {
                             for (OpResult result : results) {
@@ -718,7 +718,7 @@ public class ClientCnxn {
                     } else if (p.response instanceof GetEphemeralsResponse) {
                     } else if (p.response instanceof GetEphemeralsResponse) {
                         EphemeralsCallback cb = (EphemeralsCallback) p.cb;
                         EphemeralsCallback cb = (EphemeralsCallback) p.cb;
                         GetEphemeralsResponse rsp = (GetEphemeralsResponse) p.response;
                         GetEphemeralsResponse rsp = (GetEphemeralsResponse) p.response;
-                        if (rc == 0) {
+                        if (rc == Code.OK.intValue()) {
                             cb.processResult(rc, p.ctx, rsp.getEphemerals());
                             cb.processResult(rc, p.ctx, rsp.getEphemerals());
                         } else {
                         } else {
                             cb.processResult(rc, p.ctx, null);
                             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
          * @return true if the watch should be added, otw false
          */
          */
         protected boolean shouldAddWatch(int rc) {
         protected boolean shouldAddWatch(int rc) {
-            return rc == 0;
+            return rc == KeeperException.Code.OK.intValue();
         }
         }
 
 
     }
     }
@@ -315,12 +315,13 @@ public class ZooKeeper implements AutoCloseable {
 
 
         @Override
         @Override
         protected Map<String, Set<Watcher>> getWatches(int rc) {
         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
         @Override
         protected boolean shouldAddWatch(int rc) {
         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
         @Override
         protected boolean shouldAddWatch(int rc) {
         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() {
                             zk.getData(path, null, new DataCallback() {
                                 @Override
                                 @Override
                                 public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                                 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);
                                         successResponse.addAndGet(1);
                                     } else {
                                     } else {
                                         LOG.info("failed response is {}", rc);
                                         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) {
     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);
             lastValue.put(path, (byte[]) ctx);
         }
         }
         decOutstanding();
         decOutstanding();
@@ -202,7 +202,7 @@ public class IntegrityCheck implements StatCallback, DataCallback {
 
 
     public void processResult(
     public void processResult(
             int rc, String path, Object ctx, byte[] data, Stat stat) {
             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 string = new String(data);
             String lastString = null;
             String lastString = null;
             byte[] v = lastValue.get(path);
             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)) {
         try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
             final CountDownLatch latch = new CountDownLatch(1);
             final CountDownLatch latch = new CountDownLatch(1);
             AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
             AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
-                if (rc == 0) {
+                if (rc == KeeperException.Code.OK.intValue()) {
                     latch.countDown();
                     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)) {
         try (ZooKeeper zk = createClient(watcher, hostPort)) {
             final CountDownLatch latch = new CountDownLatch(1);
             final CountDownLatch latch = new CountDownLatch(1);
             AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
             AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
-                if (rc == 0) {
+                if (rc == KeeperException.Code.OK.intValue()) {
                     latch.countDown();
                     latch.countDown();
                 }
                 }
             };
             };
@@ -109,7 +109,7 @@ public class PersistentWatcherTest extends ClientBase {
         try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
         try (ZooKeeper zk = createClient(new CountdownWatcher(), hostPort)) {
             final CountDownLatch latch = new CountDownLatch(1);
             final CountDownLatch latch = new CountDownLatch(1);
             AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
             AsyncCallback.VoidCallback cb = (rc, path, ctx) -> {
-                if (rc == 0) {
+                if (rc == KeeperException.Code.OK.intValue()) {
                     latch.countDown();
                     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);
             assertEquals(1, watches[i].events.size(), "For " + i);
         }
         }
         for (int i = COUNT / 2; i < COUNT; 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);
                 assertEquals(1, watches[i].events.size(), "For " + i);
             } else {
             } else {
                 assertEquals(0, watches[i].events.size(), "For " + i);
                 assertEquals(0, watches[i].events.size(), "For " + i);