beam-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From da...@apache.org
Subject [08/11] incubator-beam git commit: Fix validation in PubsubIO
Date Tue, 07 Jun 2016 16:59:53 GMT
Fix validation in PubsubIO


Project: http://git-wip-us.apache.org/repos/asf/incubator-beam/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-beam/commit/85659195
Tree: http://git-wip-us.apache.org/repos/asf/incubator-beam/tree/85659195
Diff: http://git-wip-us.apache.org/repos/asf/incubator-beam/diff/85659195

Branch: refs/heads/release-0.1.0-incubating
Commit: 8565919569bf182e85fd93d069b135ad1a4d27b2
Parents: 907bd18
Author: Kenneth Knowles <klk@google.com>
Authored: Mon Jun 6 13:07:35 2016 -0700
Committer: Davor Bonaci <davor@google.com>
Committed: Mon Jun 6 18:08:22 2016 -0700

----------------------------------------------------------------------
 .../org/apache/beam/sdk/util/PubsubClient.java  | 54 ++++++++++++--------
 1 file changed, 32 insertions(+), 22 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-beam/blob/85659195/sdks/java/core/src/main/java/org/apache/beam/sdk/util/PubsubClient.java
----------------------------------------------------------------------
diff --git a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/PubsubClient.java b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/PubsubClient.java
index 76bf03f..36c4a9f 100644
--- a/sdks/java/core/src/main/java/org/apache/beam/sdk/util/PubsubClient.java
+++ b/sdks/java/core/src/main/java/org/apache/beam/sdk/util/PubsubClient.java
@@ -123,20 +123,27 @@ public abstract class PubsubClient implements Closeable {
    * Path representing a cloud project id.
    */
   public static class ProjectPath implements Serializable {
-    private final String path;
+    private final String projectId;
 
+    /**
+     * Creates a {@link ProjectPath} from a {@link String} representation, which
+     * must be of the form {@code "projects/" + projectId}.
+     */
     ProjectPath(String path) {
-      this.path = path;
+      String[] splits = path.split("/");
+      checkArgument(
+          splits.length == 2 && splits[0].equals("projects"),
+          "Malformed project path \"%s\": must be of the form \"projects/\" + <project
id>",
+          path);
+      this.projectId = splits[1];
     }
 
     public String getPath() {
-      return path;
+      return String.format("projects/%s", projectId);
     }
 
     public String getId() {
-      String[] splits = path.split("/");
-      checkState(splits.length == 1, "Malformed project path %s", path);
-      return splits[1];
+      return projectId;
     }
 
     @Override
@@ -150,18 +157,17 @@ public abstract class PubsubClient implements Closeable {
 
       ProjectPath that = (ProjectPath) o;
 
-      return path.equals(that.path);
-
+      return projectId.equals(that.projectId);
     }
 
     @Override
     public int hashCode() {
-      return path.hashCode();
+      return projectId.hashCode();
     }
 
     @Override
     public String toString() {
-      return path;
+      return getPath();
     }
   }
 
@@ -177,26 +183,29 @@ public abstract class PubsubClient implements Closeable {
    * Path representing a Pubsub subscription.
    */
   public static class SubscriptionPath implements Serializable {
-    private final String path;
+    private final String projectId;
+    private final String subscriptionName;
 
     SubscriptionPath(String path) {
-      this.path = path;
+      String[] splits = path.split("/");
+      checkState(
+          splits.length == 4 && splits[0].equals("projects") && splits[2].equals("subscriptions"),
+          "Malformed subscription path %s: "
+          + "must be of the form \"projects/\" + <project id> + \"subscriptions\"",
path);
+      this.projectId = splits[1];
+      this.subscriptionName = splits[3];
     }
 
     public String getPath() {
-      return path;
+      return String.format("projects/%s/subscriptions/%s", projectId, subscriptionName);
     }
 
     public String getName() {
-      String[] splits = path.split("/");
-      checkState(splits.length == 4, "Malformed subscription path %s", path);
-      return splits[3];
+      return subscriptionName;
     }
 
     public String getV1Beta1Path() {
-      String[] splits = path.split("/");
-      checkState(splits.length == 4, "Malformed subscription path %s", path);
-      return String.format("/subscriptions/%s/%s", splits[1], splits[3]);
+      return String.format("/subscriptions/%s/%s", projectId, subscriptionName);
     }
 
     @Override
@@ -208,17 +217,18 @@ public abstract class PubsubClient implements Closeable {
         return false;
       }
       SubscriptionPath that = (SubscriptionPath) o;
-      return path.equals(that.path);
+      return this.subscriptionName.equals(that.subscriptionName)
+          && this.projectId.equals(that.projectId);
     }
 
     @Override
     public int hashCode() {
-      return path.hashCode();
+      return Objects.hashCode(projectId, subscriptionName);
     }
 
     @Override
     public String toString() {
-      return path;
+      return getPath();
     }
   }
 


Mime
View raw message