Skip to content

Commit f4055bd

Browse files
committed
Add javadoc and tests for functional Topic class (#1021)
1 parent b872399 commit f4055bd

File tree

3 files changed

+448
-15
lines changed

3 files changed

+448
-15
lines changed

gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/Topic.java

Lines changed: 120 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@
3030
import java.util.concurrent.Future;
3131

3232
/**
33-
* PubSub Topic.
33+
* A Google Cloud Pub/Sub topic. A topic is a named resource to which messages are sent by
34+
* publishers. Subscribers can receive messages sent to a topic by creating subscriptions.
35+
* {@code Topic} adds a layer of service-related functionality over {@link TopicInfo}. Objects of
36+
* this class are immutable. To get a {@code Topic} object with the most recent information use
37+
* {@link #reload} or {@link #reloadAsync}.
38+
*
39+
* @see <a href="https://cloud.google.com/pubsub/overview#data_model">Pub/Sub Data Model</a>
3440
*/
3541
public class Topic extends TopicInfo {
3642

@@ -39,6 +45,9 @@ public class Topic extends TopicInfo {
3945
private final PubSubOptions options;
4046
private transient PubSub pubsub;
4147

48+
/**
49+
* A builder for {@code Topic} objects.
50+
*/
4251
public static final class Builder extends TopicInfo.Builder {
4352

4453
private final PubSub pubsub;
@@ -73,70 +82,173 @@ public Builder toBuilder() {
7382
}
7483

7584
@Override
76-
public int hashCode() {
85+
public final int hashCode() {
7786
return Objects.hash(options, super.hashCode());
7887
}
7988

8089
@Override
81-
public boolean equals(Object obj) {
82-
if (this == obj) {
90+
public final boolean equals(Object obj) {
91+
if (obj == this) {
8392
return true;
8493
}
85-
if (obj == null || getClass() != obj.getClass()) {
94+
if (obj == null || !obj.getClass().equals(Topic.class)) {
8695
return false;
8796
}
8897
Topic other = (Topic) obj;
89-
return Objects.equals(name(), other.name()) && Objects.equals(options, other.options);
98+
return baseEquals(other) && Objects.equals(options, other.options);
9099
}
91100

101+
/**
102+
* Returns the topic's {@code PubSub} object used to issue requests.
103+
*/
92104
public PubSub pubSub() {
93105
return pubsub;
94106
}
95107

108+
/**
109+
* Deletes this topic.
110+
*
111+
* @return {@code true} if the topic was deleted, {@code false} if it was not found
112+
* @throws PubSubException upon failure
113+
*/
96114
public boolean delete() {
97115
return pubsub.deleteTopic(name());
98116
}
99117

118+
/**
119+
* Sends a request for deleting this topic. This method returns a {@code Future} object to consume
120+
* the result. {@link Future#get()} returns {@code true} if the topic was deleted, {@code false}
121+
* if it was not found.
122+
*
123+
* @throws PubSubException upon failure
124+
*/
100125
public Future<Boolean> deleteAsync() {
101126
return pubsub.deleteTopicAsync(name());
102127
}
103128

129+
/**
130+
* Fetches current topic's latest information. Returns {@code null} if the topic does not exist.
131+
*
132+
* @return a {@code Topic} object with latest information or {@code null} if not found
133+
* @throws PubSubException upon failure
134+
*/
104135
public Topic reload() {
105136
return pubsub.getTopic(name());
106137
}
107138

139+
/**
140+
* Sends a request to fetch current topic's latest information. This method returns a
141+
* {@code Future} object to consume the result. {@link Future#get()} returns a {@code Topic}
142+
* object with latest information or {@code null} if not found.
143+
*
144+
* @throws PubSubException upon failure
145+
*/
108146
public Future<Topic> reloadAsync() {
109147
return pubsub.getTopicAsync(name());
110148
}
111149

150+
/**
151+
* Publishes a message to this topic. This method returns a service-generated id for the published
152+
* message. Service-generated ids are guaranteed to be unique within the topic.
153+
*
154+
* @param message the message to publish
155+
* @return a unique service-generated id for the message
156+
* @throws PubSubException upon failure, if the topic does not exist or if the message has empty
157+
* payload and no attributes
158+
*/
112159
public String publish(Message message) {
113160
return pubsub.publish(name(), message);
114161
}
115162

163+
/**
164+
* Sends a request for publishing a message to the this topic. This method returns a
165+
* {@code Future} object to consume the result. {@link Future#get()} returns a service-generated
166+
* id for the published message. Service-generated ids are guaranteed to be unique within the
167+
* topic.
168+
*
169+
* @param message the message to publish
170+
* @return a {@code Future} for the unique service-generated id for the message
171+
*/
116172
public Future<String> publishAsync(Message message) {
117173
return pubsub.publishAsync(name(), message);
118174
}
119175

176+
/**
177+
* Publishes a number of messages to this topic. This method returns a list of service-generated
178+
* ids for the published messages. Service-generated ids are guaranteed to be unique within the
179+
* topic.
180+
*
181+
* @param message the first message to publish
182+
* @param messages other messages to publish
183+
* @return a list of unique, service-generated, ids. Ids are in the same order as the messages.
184+
* @throws PubSubException upon failure, if the topic does not exist or if one of the messages has
185+
* empty payload and no attributes
186+
*/
120187
public List<String> publish(Message message, Message... messages) {
121188
return pubsub.publish(name(), message, messages);
122189
}
123190

191+
/**
192+
* Sends a request to publish a number of messages to this topic. This method returns a
193+
* {@code Future} object to consume the result. {@link Future#get()} returns a list of
194+
* service-generated ids for the published messages. Service-generated ids are guaranteed to be
195+
* unique within the topic.
196+
*
197+
* @param message the first message to publish
198+
* @param messages other messages to publish
199+
* @return a {@code Future} for the unique, service-generated ids. Ids are in the same order as
200+
* the messages.
201+
*/
124202
public Future<List<String>> publishAsync(Message message, Message... messages) {
125203
return pubsub.publishAsync(name(), message, messages);
126204
}
127205

206+
/**
207+
* Publishes a number of messages to this topic. This method returns a list ofservice-generated
208+
* ids for the published messages. Service-generated ids are guaranteed to be unique within the
209+
* topic.
210+
*
211+
* @param messages the messages to publish
212+
* @return a list of unique, service-generated, ids. Ids are in the same order as the messages.
213+
* @throws PubSubException upon failure, if the topic does not exist or if one of the messages has
214+
* empty payload and no attributes
215+
*/
128216
public List<String> publish(Iterable<Message> messages) {
129217
return pubsub.publish(name(), messages);
130218
}
131219

220+
/**
221+
* Sends a request to publish a number of messages to this topic. This method returns a
222+
* {@code Future} object to consume the result. {@link Future#get()} returns a list of
223+
* service-generated ids for the published messages. Service-generated ids are guaranteed to be
224+
* unique within the topic.
225+
*
226+
* @param messages the messages to publish
227+
* @return a {@code Future} for the unique, service-generated ids. Ids are in the same order as
228+
* the messages.
229+
*/
132230
public Future<List<String>> publishAsync(Iterable<Message> messages) {
133231
return pubsub.publishAsync(name(), messages);
134232
}
135233

234+
/**
235+
* Lists the identities of the subscriptions for this topic. This method returns a {@link Page}
236+
* object that can be used to consume paginated results. Use {@link ListOption} to specify the
237+
* page size or the page token from which to start listing subscriptions.
238+
*
239+
* @throws PubSubException upon failure
240+
*/
136241
public Page<SubscriptionId> listSubscriptions(ListOption... options) {
137242
return pubsub.listSubscriptions(name(), options);
138243
}
139244

245+
/**
246+
* Sends a request for listing the identities of subscriptions for this topic. This method returns
247+
* a {@code Future} object to consume the result. {@link Future#get()} returns an
248+
* {@link AsyncPage} object that can be used to asynchronously handle paginated results. Use
249+
* {@link ListOption} to specify the page size or the page token from which to start listing
250+
* subscriptions.
251+
*/
140252
public Future<AsyncPage<SubscriptionId>> listSubscriptionsAsync(ListOption... options) {
141253
return pubsub.listSubscriptionsAsync(name(), options);
142254
}
@@ -146,9 +258,9 @@ private void readObject(ObjectInputStream input) throws IOException, ClassNotFou
146258
this.pubsub = options.service();
147259
}
148260

149-
static Topic fromPb(PubSub storage, com.google.pubsub.v1.Topic topicPb) {
261+
static Topic fromPb(PubSub pubsub, com.google.pubsub.v1.Topic topicPb) {
150262
TopicInfo topicInfo = TopicInfo.fromPb(topicPb);
151-
return new Topic(storage, new BuilderImpl(topicInfo));
263+
return new Topic(pubsub, new BuilderImpl(topicInfo));
152264
}
153265

154266
static Function<com.google.pubsub.v1.Topic, Topic> fromPbFunction(final PubSub pubsub) {

gcloud-java-pubsub/src/main/java/com/google/cloud/pubsub/TopicInfo.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,15 +98,16 @@ public int hashCode() {
9898
return Objects.hash(name);
9999
}
100100

101+
final boolean baseEquals(TopicInfo topicInfo) {
102+
return Objects.equals(name, topicInfo.name);
103+
}
104+
101105
@Override
102106
public boolean equals(Object obj) {
103-
if (this == obj) {
104-
return true;
105-
}
106-
if (obj == null || !obj.getClass().equals(this.getClass())) {
107-
return false;
108-
}
109-
return Objects.equals(name, ((TopicInfo) obj).name);
107+
return obj == this
108+
|| obj != null
109+
&& obj.getClass().equals(TopicInfo.class)
110+
&& baseEquals((TopicInfo) obj);
110111
}
111112

112113
@Override

0 commit comments

Comments
 (0)