30
30
import java .util .concurrent .Future ;
31
31
32
32
/**
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>
34
40
*/
35
41
public class Topic extends TopicInfo {
36
42
@@ -39,6 +45,9 @@ public class Topic extends TopicInfo {
39
45
private final PubSubOptions options ;
40
46
private transient PubSub pubsub ;
41
47
48
+ /**
49
+ * A builder for {@code Topic} objects.
50
+ */
42
51
public static final class Builder extends TopicInfo .Builder {
43
52
44
53
private final PubSub pubsub ;
@@ -73,70 +82,173 @@ public Builder toBuilder() {
73
82
}
74
83
75
84
@ Override
76
- public int hashCode () {
85
+ public final int hashCode () {
77
86
return Objects .hash (options , super .hashCode ());
78
87
}
79
88
80
89
@ Override
81
- public boolean equals (Object obj ) {
90
+ public final boolean equals (Object obj ) {
82
91
if (this == obj ) {
83
92
return true ;
84
93
}
85
- if (obj == null || getClass () != obj .getClass ()) {
94
+ if (obj == null || ! obj .getClass (). equals ( Topic . class )) {
86
95
return false ;
87
96
}
88
97
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 );
90
99
}
91
100
101
+ /**
102
+ * Returns the topic's {@code PubSub} object used to issue requests.
103
+ */
92
104
public PubSub pubSub () {
93
105
return pubsub ;
94
106
}
95
107
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
+ */
96
114
public boolean delete () {
97
115
return pubsub .deleteTopic (name ());
98
116
}
99
117
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
+ */
100
125
public Future <Boolean > deleteAsync () {
101
126
return pubsub .deleteTopicAsync (name ());
102
127
}
103
128
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
+ */
104
135
public Topic reload () {
105
136
return pubsub .getTopic (name ());
106
137
}
107
138
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
+ */
108
146
public Future <Topic > reloadAsync () {
109
147
return pubsub .getTopicAsync (name ());
110
148
}
111
149
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
+ */
112
159
public String publish (Message message ) {
113
160
return pubsub .publish (name (), message );
114
161
}
115
162
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
+ */
116
172
public Future <String > publishAsync (Message message ) {
117
173
return pubsub .publishAsync (name (), message );
118
174
}
119
175
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
+ */
120
187
public List <String > publish (Message message , Message ... messages ) {
121
188
return pubsub .publish (name (), message , messages );
122
189
}
123
190
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
+ */
124
202
public Future <List <String >> publishAsync (Message message , Message ... messages ) {
125
203
return pubsub .publishAsync (name (), message , messages );
126
204
}
127
205
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
+ */
128
216
public List <String > publish (Iterable <Message > messages ) {
129
217
return pubsub .publish (name (), messages );
130
218
}
131
219
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
+ */
132
230
public Future <List <String >> publishAsync (Iterable <Message > messages ) {
133
231
return pubsub .publishAsync (name (), messages );
134
232
}
135
233
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
+ */
136
241
public Page <SubscriptionId > listSubscriptions (ListOption ... options ) {
137
242
return pubsub .listSubscriptions (name (), options );
138
243
}
139
244
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
+ */
140
252
public Future <AsyncPage <SubscriptionId >> listSubscriptionsAsync (ListOption ... options ) {
141
253
return pubsub .listSubscriptionsAsync (name (), options );
142
254
}
@@ -146,9 +258,9 @@ private void readObject(ObjectInputStream input) throws IOException, ClassNotFou
146
258
this .pubsub = options .service ();
147
259
}
148
260
149
- static Topic fromPb (PubSub storage , com .google .pubsub .v1 .Topic topicPb ) {
261
+ static Topic fromPb (PubSub pubsub , com .google .pubsub .v1 .Topic topicPb ) {
150
262
TopicInfo topicInfo = TopicInfo .fromPb (topicPb );
151
- return new Topic (storage , new BuilderImpl (topicInfo ));
263
+ return new Topic (pubsub , new BuilderImpl (topicInfo ));
152
264
}
153
265
154
266
static Function <com .google .pubsub .v1 .Topic , Topic > fromPbFunction (final PubSub pubsub ) {
0 commit comments