18
18
19
19
import static com .google .common .base .Preconditions .checkNotNull ;
20
20
21
- import com .google .cloud .pubsub .spi .v1 .PublisherApi ;
22
21
import com .google .cloud .pubsub .spi .v1 .SubscriberApi ;
23
22
import com .google .common .base .MoreObjects ;
24
23
@@ -53,7 +52,7 @@ public class SubscriptionInfo implements Serializable {
53
52
private static final long serialVersionUID = 1860057426574127128L ;
54
53
55
54
private final String name ;
56
- private final String topic ;
55
+ private final TopicId topic ;
57
56
private final PushConfig pushConfig ;
58
57
private final int ackDeadlineSeconds ;
59
58
@@ -72,9 +71,22 @@ public abstract static class Builder {
72
71
public abstract Builder name (String name );
73
72
74
73
/**
75
- * Sets the name of the topic the subscription refers to.
74
+ * Sets the topic the subscription refers to, given the topic name. The topic is assumed to
75
+ * reside in the {@link PubSubOptions#projectId()} project.
76
76
*/
77
- public abstract Builder topic (String name );
77
+ public abstract Builder topic (String topic );
78
+
79
+ /**
80
+ * Sets the topic the subscription refers to, given the project and topic names.
81
+ */
82
+ public abstract Builder topic (String project , String topic );
83
+
84
+ /**
85
+ * Sets the topic the subscription refers to, given the topic identity. If
86
+ * {@code topic.project()} is {@code null} the topic is assumed to reside in the
87
+ * {@link PubSubOptions#projectId()} project.
88
+ */
89
+ public abstract Builder topic (TopicId topic );
78
90
79
91
/**
80
92
* Sets the push configuration for the subscription. If set, the subscription will be in
@@ -104,11 +116,11 @@ public abstract static class Builder {
104
116
static final class BuilderImpl extends Builder {
105
117
106
118
private String name ;
107
- private String topic ;
119
+ private TopicId topic ;
108
120
private PushConfig pushConfig ;
109
121
private int ackDeadlineSeconds ;
110
122
111
- private BuilderImpl (String topic , String name ) {
123
+ private BuilderImpl (TopicId topic , String name ) {
112
124
this .topic = checkNotNull (topic );
113
125
this .name = checkNotNull (name );
114
126
}
@@ -126,8 +138,18 @@ public Builder name(String name) {
126
138
return this ;
127
139
}
128
140
141
+ @ Override
142
+ public Builder topic (String project , String topic ) {
143
+ return topic (TopicId .of (checkNotNull (project ), topic ));
144
+ }
145
+
129
146
@ Override
130
147
public Builder topic (String topic ) {
148
+ return topic (TopicId .of (topic ));
149
+ }
150
+
151
+ @ Override
152
+ public Builder topic (TopicId topic ) {
131
153
this .topic = checkNotNull (topic );
132
154
return this ;
133
155
}
@@ -158,9 +180,12 @@ public SubscriptionInfo build() {
158
180
}
159
181
160
182
/**
161
- * Returns the name of the topic this subscription refers to.
183
+ * Returns the identity of the topic this subscription refers to. If {@link TopicId#project()} is
184
+ * {@code null} the topic is assumed to reside in the {@link PubSubOptions#projectId()} project.
185
+ * After a topic is deleted, existing subscriptions to that topic are not deleted, but their topic
186
+ * field is set to {@link TopicId#deletedTopic()}.
162
187
*/
163
- public String topic () {
188
+ public TopicId topic () {
164
189
return topic ;
165
190
}
166
191
@@ -231,7 +256,7 @@ public String toString() {
231
256
com .google .pubsub .v1 .Subscription toPb (String projectId ) {
232
257
com .google .pubsub .v1 .Subscription .Builder builder =
233
258
com .google .pubsub .v1 .Subscription .newBuilder ();
234
- builder .setTopic (PublisherApi . formatTopicName (projectId , topic ));
259
+ builder .setTopic (topic . toPb (projectId ));
235
260
builder .setName (SubscriberApi .formatSubscriptionName (projectId , name ));
236
261
builder .setAckDeadlineSeconds (ackDeadlineSeconds );
237
262
if (pushConfig != null ) {
@@ -241,7 +266,7 @@ com.google.pubsub.v1.Subscription toPb(String projectId) {
241
266
}
242
267
243
268
static SubscriptionInfo fromPb (com .google .pubsub .v1 .Subscription subscription ) {
244
- Builder builder = builder (PublisherApi . parseTopicFromTopicName (subscription .getTopic ()),
269
+ Builder builder = builder (TopicId . fromPb (subscription .getTopic ()),
245
270
SubscriberApi .parseSubscriptionFromSubscriptionName (subscription .getName ()));
246
271
builder .ackDeadLineSeconds (subscription .getAckDeadlineSeconds ());
247
272
// A subscription with an "empty" push config is a pull subscription
@@ -261,48 +286,100 @@ public Builder toBuilder() {
261
286
262
287
/**
263
288
* Creates a pull {@code SubscriptionInfo} object given the name of the topic and the name of the
264
- * subscription.
289
+ * subscription. The topic is assumed to reside in the {@link PubSubOptions#projectId()} project.
265
290
*
266
291
* @param topic the name of the topic the subscription refers to
267
292
* @param name the name of the subscription. The name must start with a letter, and contain only
268
293
* letters ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores
269
294
* ({@code _}), periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs
270
295
* ({@code %}). It must be between 3 and 255 characters in length and cannot begin with the
271
- * string {@code goog}
296
+ * string {@code goog}.
272
297
*/
273
298
public static SubscriptionInfo of (String topic , String name ) {
274
299
return builder (topic , name ).build ();
275
300
}
276
301
302
+ /**
303
+ * Creates a pull {@code SubscriptionInfo} object given the identity of the topic and the name of
304
+ * the subscription. If {@code topic.project()} is {@code null} the topic is assumed to reside in
305
+ * the {@link PubSubOptions#projectId()} project.
306
+ *
307
+ * @param topic the identity of the topic the subscription refers to
308
+ * @param name the name of the subscription. The name must start with a letter, and contain only
309
+ * letters ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores
310
+ * ({@code _}), periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs
311
+ * ({@code %}). It must be between 3 and 255 characters in length and cannot begin with the
312
+ * string {@code goog}.
313
+ */
314
+ public static SubscriptionInfo of (TopicId topic , String name ) {
315
+ return builder (topic , name ).build ();
316
+ }
317
+
277
318
/**
278
319
* Creates a push {@code SubscriptionInfo} object given the name of the topic, the name of the
279
- * subscription and the push endpoint.
320
+ * subscription and the push endpoint. The topic is assumed to reside in the
321
+ * {@link PubSubOptions#projectId()} project.
280
322
*
281
323
* @param topic the name of the topic the subscription refers to
282
324
* @param name the name of the subscription. The name must start with a letter, and contain only
283
325
* letters ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores
284
326
* ({@code _}), periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs
285
327
* ({@code %}). It must be between 3 and 255 characters in length and cannot begin with the
286
- * string {@code goog}
328
+ * string {@code goog}.
287
329
* @param endpoint a URL locating the endpoint to which messages should be pushed. For example,
288
330
* an endpoint might use {@code https://example.com/push}.
289
331
*/
290
332
public static SubscriptionInfo of (String topic , String name , String endpoint ) {
291
333
return builder (topic , name ).pushConfig (PushConfig .of (endpoint )).build ();
292
334
}
293
335
336
+ /**
337
+ * Creates a push {@code SubscriptionInfo} object given the identity of the topic, the name of the
338
+ * subscription and the push endpoint. If {@code topic.project()} is {@code null} the topic is
339
+ * assumed to reside in the {@link PubSubOptions#projectId()} project.
340
+ *
341
+ * @param topic the identity of the topic the subscription refers to
342
+ * @param name the name of the subscription. The name must start with a letter, and contain only
343
+ * letters ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores
344
+ * ({@code _}), periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs
345
+ * ({@code %}). It must be between 3 and 255 characters in length and cannot begin with the
346
+ * string {@code goog}.
347
+ * @param endpoint a URL locating the endpoint to which messages should be pushed. For example,
348
+ * an endpoint might use {@code https://example.com/push}.
349
+ */
350
+ public static SubscriptionInfo of (TopicId topic , String name , String endpoint ) {
351
+ return builder (topic , name ).pushConfig (PushConfig .of (endpoint )).build ();
352
+ }
353
+
294
354
/**
295
355
* Creates a builder for {@code SubscriptionInfo} objects given the name of the topic and the name
296
- * of the subscription.
356
+ * of the subscription. The topic is assumed to reside in the {@link PubSubOptions#projectId()}
357
+ * project.
297
358
*
298
359
* @param topic the name of the topic the subscription refers to
299
360
* @param name the name of the subscription. The name must start with a letter, and contain only
300
361
* letters ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores
301
362
* ({@code _}), periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs
302
363
* ({@code %}). It must be between 3 and 255 characters in length and cannot begin with the
303
- * string {@code goog}
364
+ * string {@code goog}.
304
365
*/
305
366
public static Builder builder (String topic , String name ) {
367
+ return builder (TopicId .of (topic ), name );
368
+ }
369
+
370
+ /**
371
+ * Creates a builder for {@code SubscriptionInfo} objects given the identity of the topic and the
372
+ * name of the subscription. If {@code topic.project()} is {@code null} the topic is assumed to
373
+ * reside in the {@link PubSubOptions#projectId()} project.
374
+ *
375
+ * @param topic the identity of the topic the subscription refers to
376
+ * @param name the name of the subscription. The name must start with a letter, and contain only
377
+ * letters ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores
378
+ * ({@code _}), periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs
379
+ * ({@code %}). It must be between 3 and 255 characters in length and cannot begin with the
380
+ * string {@code goog}.
381
+ */
382
+ public static Builder builder (TopicId topic , String name ) {
306
383
return new BuilderImpl (topic , name );
307
384
}
308
385
}
0 commit comments