Skip to content

Commit 82b5032

Browse files
authored
Rename setters/getters/builders for PubSub classes to meet proto conventions (#1312)
* Rename setters/getters/builders for PubSub classes to meet proto conventions * Update PubSub examples, snippets and READMEs to use renamed getters/setters/builders * Make deprecated methods call renamed ones
1 parent f283895 commit 82b5032

33 files changed

Lines changed: 1050 additions & 311 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ try (PubSub pubsub = PubSubOptions.defaultInstance().service()) {
477477
MessageProcessor callback = new MessageProcessor() {
478478
@Override
479479
public void process(Message message) throws Exception {
480-
System.out.printf("Received message \"%s\"%n", message.payloadAsString());
480+
System.out.printf("Received message \"%s\"%n", message.getPayloadAsString());
481481
}
482482
};
483483
// Create a message consumer and pull messages (for 60 seconds)

TESTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ and `start` methods. This will bind a port for communication with the local Pub/
190190
2. Create and use a `PubSub` object with the options given by the `LocalPubSubHelper` instance. For
191191
example:
192192
```java
193-
PubSub localPubsub = helper.options().service();
193+
PubSub localPubsub = helper.getOptions().service();
194194
```
195195

196196
3. Run your tests.

google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/PubSubExample.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ private static class CreateSubscriptionAction extends PubSubAction<SubscriptionI
360360
@Override
361361
public void run(PubSub pubsub, SubscriptionInfo subscription) {
362362
pubsub.create(subscription);
363-
System.out.printf("Created subscription %s%n", subscription.name());
363+
System.out.printf("Created subscription %s%n", subscription.getName());
364364
}
365365

366366
@Override
@@ -371,9 +371,9 @@ SubscriptionInfo parse(String... args) throws Exception {
371371
} else if (args.length < 2) {
372372
message = "Missing required topic or subscription name";
373373
} else {
374-
SubscriptionInfo.Builder builder = SubscriptionInfo.builder(args[0], args[1]);
374+
SubscriptionInfo.Builder builder = SubscriptionInfo.newBuilder(args[0], args[1]);
375375
if (args.length == 3) {
376-
builder.pushConfig(PushConfig.of(args[2]));
376+
builder.setPushConfig(PushConfig.of(args[2]));
377377
}
378378
return builder.build();
379379
}

google-cloud-examples/src/main/java/com/google/cloud/examples/pubsub/snippets/CreateSubscriptionAndPullMessages.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static void main(String... args) throws Exception {
3737
MessageProcessor callback = new MessageProcessor() {
3838
@Override
3939
public void process(Message message) throws Exception {
40-
System.out.printf("Received message \"%s\"%n", message.payloadAsString());
40+
System.out.printf("Received message \"%s\"%n", message.getPayloadAsString());
4141
}
4242
};
4343
// Create a message consumer and pull messages (for 60 seconds)

google-cloud-pubsub/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ Then, to pull messages asynchronously, use the following code:
158158
MessageProcessor callback = new MessageProcessor() {
159159
@Override
160160
public void process(Message message) throws Exception {
161-
System.out.printf("Received message \"%s\"%n", message.payloadAsString());
161+
System.out.printf("Received message \"%s\"%n", message.getPayloadAsString());
162162
}
163163
};
164164
// Create a message consumer and pull messages (for 60 seconds)

google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/Message.java

Lines changed: 116 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -85,26 +85,47 @@ protected ByteString byteString() {
8585
*/
8686
public abstract static class Builder {
8787

88-
abstract Builder id(String id);
88+
abstract Builder setId(String id);
8989

9090
/**
9191
* Sets the message payload to the provided string. The string is enconded {@code UTF-8}.
9292
*/
93+
@Deprecated
9394
public abstract Builder payload(String payload);
9495

96+
/**
97+
* Sets the message payload to the provided string. The string is enconded {@code UTF-8}.
98+
*/
99+
public abstract Builder setPayload(String payload);
100+
95101
/**
96102
* Sets the message payload to the provided {@link ByteArray}.
97103
*/
104+
@Deprecated
98105
public abstract Builder payload(ByteArray payload);
99106

107+
/**
108+
* Sets the message payload to the provided {@link ByteArray}.
109+
*/
110+
public abstract Builder setPayload(ByteArray payload);
111+
100112
/**
101113
* Sets the message attributes to the provided map. Message attributes are key-value pairs that
102114
* a publisher can define for a message. For example, a key {@code iana.org/language_tag} and
103115
* value {@code en} could be added to messages to mark them as readable by an English-speaking
104116
* subscriber.
105117
*/
118+
@Deprecated
106119
public abstract Builder attributes(Map<String, String> attributes);
107120

121+
/**
122+
* Sets the message attributes to the provided map. Message attributes are key-value pairs that
123+
* a publisher can define for a message. For example, a key {@code iana.org/language_tag} and
124+
* value {@code en} could be added to messages to mark them as readable by an English-speaking
125+
* subscriber.
126+
*/
127+
public abstract Builder setAttributes(Map<String, String> attributes);
128+
108129
/**
109130
* Adds a new attribute to the message attributes. If an attribute with name {@code name} was
110131
* already set, its value is updated.
@@ -121,7 +142,7 @@ public abstract static class Builder {
121142
*/
122143
public abstract Builder clearAttributes();
123144

124-
abstract Builder publishTime(long publishTime);
145+
abstract Builder setPublishTime(long publishTime);
125146

126147
/**
127148
* Creates a message object.
@@ -146,18 +167,30 @@ private BuilderImpl() {}
146167
}
147168

148169
@Override
149-
BuilderImpl id(String id) {
170+
BuilderImpl setId(String id) {
150171
this.id = checkNotNull(id);
151172
return this;
152173
}
153174

154175
@Override
176+
@Deprecated
155177
public Builder payload(String payload) {
156-
return payload(ByteArray.copyFrom(payload));
178+
return setPayload(payload);
179+
}
180+
181+
@Override
182+
public Builder setPayload(String payload) {
183+
return setPayload(ByteArray.copyFrom(payload));
157184
}
158185

159186
@Override
187+
@Deprecated
160188
public Builder payload(ByteArray payload) {
189+
return setPayload(payload);
190+
}
191+
192+
@Override
193+
public Builder setPayload(ByteArray payload) {
161194
this.payload = payload;
162195
return this;
163196
}
@@ -169,7 +202,13 @@ public Builder addAttribute(String name, String value) {
169202
}
170203

171204
@Override
205+
@Deprecated
172206
public Builder attributes(Map<String, String> attributes) {
207+
return setAttributes(attributes);
208+
}
209+
210+
@Override
211+
public Builder setAttributes(Map<String, String> attributes) {
173212
this.attributes = new HashMap<>(attributes);
174213
return this;
175214
}
@@ -187,7 +226,7 @@ public Builder clearAttributes() {
187226
}
188227

189228
@Override
190-
Builder publishTime(long publishTime) {
229+
Builder setPublishTime(long publishTime) {
191230
this.publishTime = publishTime;
192231
return this;
193232
}
@@ -209,7 +248,16 @@ public Message build() {
209248
* Returns the time in milliseconds at which the message was published. This value is set by the
210249
* server when it receives the publish call. If not set, this method returns {@code null}.
211250
*/
251+
@Deprecated
212252
public Long publishTime() {
253+
return getPublishTime();
254+
}
255+
256+
/**
257+
* Returns the time in milliseconds at which the message was published. This value is set by the
258+
* server when it receives the publish call. If not set, this method returns {@code null}.
259+
*/
260+
public Long getPublishTime() {
213261
return publishTime;
214262
}
215263

@@ -218,7 +266,17 @@ public Long publishTime() {
218266
* define for a message. For example, a key {@code iana.org/language_tag} and value {@code en}
219267
* could be added to messages to mark them as readable by an English-speaking subscriber.
220268
*/
269+
@Deprecated
221270
public Map<String, String> attributes() {
271+
return getAttributes();
272+
}
273+
274+
/**
275+
* Returns the message attributes. Message attributes are key-value pairs that a publisher can
276+
* define for a message. For example, a key {@code iana.org/language_tag} and value {@code en}
277+
* could be added to messages to mark them as readable by an English-speaking subscriber.
278+
*/
279+
public Map<String, String> getAttributes() {
222280
return attributes;
223281
}
224282

@@ -228,21 +286,48 @@ public Map<String, String> attributes() {
228286
* a Pub/Sub message via a pull call or a push delivery. If not set, this method returns
229287
* {@code null}.
230288
*/
289+
@Deprecated
231290
public String id() {
291+
return getId();
292+
}
293+
294+
/**
295+
* Returns the id of this message, set by the server when the message is published. The id is
296+
* guaranteed to be unique within the topic. This value may be read by a subscriber that receives
297+
* a Pub/Sub message via a pull call or a push delivery. If not set, this method returns
298+
* {@code null}.
299+
*/
300+
public String getId() {
232301
return id;
233302
}
234303

235304
/**
236305
* Returns the message payload as a string, decoded using {@code UTF-8}.
237306
*/
307+
@Deprecated
238308
public String payloadAsString() {
309+
return getPayloadAsString();
310+
}
311+
312+
/**
313+
* Returns the message payload as a string, decoded using {@code UTF-8}.
314+
*/
315+
public String getPayloadAsString() {
239316
return payload.toStringUtf8();
240317
}
241318

242319
/**
243320
* Returns the message payload.
244321
*/
322+
@Deprecated
245323
public ByteArray payload() {
324+
return getPayload();
325+
}
326+
327+
/**
328+
* Returns the message payload.
329+
*/
330+
public ByteArray getPayload() {
246331
return payload;
247332
}
248333

@@ -293,18 +378,18 @@ PubsubMessage toPb() {
293378
}
294379

295380
static Message fromPb(PubsubMessage messagePb) {
296-
Builder builder = builder(new InternalByteArray(messagePb.getData()));
381+
Builder builder = newBuilder(new InternalByteArray(messagePb.getData()));
297382
if (messagePb.hasPublishTime()) {
298383
Timestamp ts = messagePb.getPublishTime();
299384
Long millis = ts.getSeconds() * MILLIS_PER_SECOND + ts.getNanos() / NANOS_PER_MILLISECOND;
300385
if (millis != 0) {
301-
builder.publishTime(millis);
386+
builder.setPublishTime(millis);
302387
}
303388
}
304389
if (!Objects.equals(messagePb.getMessageId(), "")) {
305-
builder.id(messagePb.getMessageId());
390+
builder.setId(messagePb.getMessageId());
306391
}
307-
for (Map.Entry<String, String> entry : messagePb.getAttributes().entrySet()) {
392+
for (Map.Entry<String, String> entry : messagePb.getAttributesMap().entrySet()) {
308393
builder.addAttribute(entry.getKey(), entry.getValue());
309394
}
310395
return builder.build();
@@ -322,30 +407,48 @@ public Builder toBuilder() {
322407
* {@code UTF-8}.
323408
*/
324409
public static Message of(String payload) {
325-
return builder(payload).build();
410+
return newBuilder(payload).build();
326411
}
327412

328413
/**
329414
* Creates a {@code Message} object given the payload as a {@link ByteArray}. To be published a
330415
* message must have a non-empty payload.
331416
*/
332417
public static Message of(ByteArray payload) {
333-
return builder(payload).build();
418+
return newBuilder(payload).build();
334419
}
335420

336421
/**
337422
* Creates a builder for {@code Message} objects given the payload as a string. The string is
338423
* enconded using {@code UTF-8}. To be published a message must have a non-empty payload.
339424
*/
425+
@Deprecated
340426
public static Builder builder(String payload) {
341-
return new BuilderImpl().payload(payload);
427+
return newBuilder(payload);
428+
}
429+
430+
/**
431+
* Creates a builder for {@code Message} objects given the payload as a string. The string is
432+
* enconded using {@code UTF-8}. To be published a message must have a non-empty payload.
433+
*/
434+
public static Builder newBuilder(String payload) {
435+
return new BuilderImpl().setPayload(payload);
342436
}
343437

344438
/**
345439
* Creates a builder for {@code Message} objects given the payload as a {@link ByteArray}. To be
346440
* published a message must have a non-empty payload, or at least one attribute.
347441
*/
442+
@Deprecated
348443
public static Builder builder(ByteArray payload) {
349-
return new BuilderImpl().payload(payload);
444+
return newBuilder(payload);
445+
}
446+
447+
/**
448+
* Creates a builder for {@code Message} objects given the payload as a {@link ByteArray}. To be
449+
* published a message must have a non-empty payload, or at least one attribute.
450+
*/
451+
public static Builder newBuilder(ByteArray payload) {
452+
return new BuilderImpl().setPayload(payload);
350453
}
351454
}

google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/Option.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@ interface OptionType {
4444
}
4545

4646
@SuppressWarnings("unchecked")
47-
<T extends OptionType> T optionType() {
47+
<T extends OptionType> T getOptionType() {
4848
return (T) optionType;
4949
}
5050

51-
Object value() {
51+
Object getValue() {
5252
return value;
5353
}
5454

google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PubSubFactory.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,4 @@
2121
/**
2222
* An interface for Pub/Sub factories.
2323
*/
24-
public interface PubSubFactory
25-
extends ServiceFactory<PubSub, PubSubOptions> {}
24+
public interface PubSubFactory extends ServiceFactory<PubSub, PubSubOptions> {}

google-cloud-pubsub/src/main/java/com/google/cloud/pubsub/PubSubImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ public List<Boolean> apply(TestIamPermissionsResponse response) {
714714
static <T extends Option.OptionType> Map<Option.OptionType, ?> optionMap(Option... options) {
715715
Map<Option.OptionType, Object> optionMap = Maps.newHashMap();
716716
for (Option option : options) {
717-
Object prev = optionMap.put(option.optionType(), option.value());
717+
Object prev = optionMap.put(option.getOptionType(), option.getValue());
718718
checkArgument(prev == null, "Duplicate option %s", option);
719719
}
720720
return optionMap;

0 commit comments

Comments
 (0)