18
18
19
19
import static com .google .common .base .Preconditions .checkNotNull ;
20
20
21
+ import com .google .cloud .pubsub .spi .v1 .PublisherApi ;
21
22
import com .google .common .base .MoreObjects ;
22
23
23
24
import java .io .Serializable ;
24
25
import java .util .Objects ;
25
26
26
27
/**
27
- * Pub/Sub Topic information.
28
+ * A Google Cloud Pub/Sub topic. A topic is a named resource to which messages are sent by
29
+ * publishers. Subscribers can receive messages sent to a topic by creating subscriptions.
30
+ *
31
+ * @see <a href="https://cloud.google.com/pubsub/overview#data_model">Pub/Sub Data Model</a>
28
32
*/
29
33
public class TopicInfo implements Serializable {
30
34
@@ -33,12 +37,21 @@ public class TopicInfo implements Serializable {
33
37
private final String name ;
34
38
35
39
/**
36
- * Builder for TopicInfo.
40
+ * Builder for {@code TopicInfo} objects .
37
41
*/
38
42
public abstract static class Builder {
39
43
44
+ /**
45
+ * Sets the name of the topic. The name must start with a letter, and contain only letters
46
+ * ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
47
+ * periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}). It
48
+ * must be between 3 and 255 characters in length and cannot begin with the string {@code goog}.
49
+ */
40
50
public abstract Builder name (String name );
41
51
52
+ /**
53
+ * Creates a topic object.
54
+ */
42
55
public abstract TopicInfo build ();
43
56
}
44
57
@@ -70,6 +83,12 @@ public TopicInfo build() {
70
83
name = builder .name ;
71
84
}
72
85
86
+ /**
87
+ * Returns the name of the topic. The name must start with a letter, and contain only letters
88
+ * ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
89
+ * periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}). It
90
+ * must be between 3 and 255 characters in length and cannot begin with the string {@code goog}.
91
+ */
73
92
public String name () {
74
93
return name ;
75
94
}
@@ -84,10 +103,10 @@ public boolean equals(Object obj) {
84
103
if (this == obj ) {
85
104
return true ;
86
105
}
87
- if (obj == null || getClass () != obj . getClass ()) {
106
+ if (obj == null || ! obj . getClass (). equals ( this . getClass () )) {
88
107
return false ;
89
108
}
90
- return Objects .equals (toPb () , ((TopicInfo ) obj ).toPb () );
109
+ return Objects .equals (name , ((TopicInfo ) obj ).name );
91
110
}
92
111
93
112
@ Override
@@ -97,22 +116,42 @@ public String toString() {
97
116
.toString ();
98
117
}
99
118
100
- com .google .pubsub .v1 .Topic toPb () {
101
- return com .google .pubsub .v1 .Topic .newBuilder ().setName (name ).build ();
119
+ com .google .pubsub .v1 .Topic toPb (String projectId ) {
120
+ return com .google .pubsub .v1 .Topic .newBuilder ()
121
+ .setName (PublisherApi .formatTopicName (projectId , name ))
122
+ .build ();
102
123
}
103
124
104
125
static TopicInfo fromPb (com .google .pubsub .v1 .Topic topicPb ) {
105
- return builder (topicPb .getName ()).build ();
126
+ return builder (PublisherApi . parseTopicFromTopicName ( topicPb .getName () )).build ();
106
127
}
107
128
108
129
public Builder toBuilder () {
109
130
return new BuilderImpl (this );
110
131
}
111
132
133
+ /**
134
+ * Creates a {@code TopicInfo} object given the name of the topic.
135
+ *
136
+ * @param name the name of the topic. The name must start with a letter, and contain only letters
137
+ * ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
138
+ * periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}).
139
+ * It must be between 3 and 255 characters in length and cannot begin with the string
140
+ * {@code goog}.
141
+ */
112
142
public static TopicInfo of (String name ) {
113
143
return builder (name ).build ();
114
144
}
115
145
146
+ /**
147
+ * Creates a builder for {@code TopicInfo} objects given the name of the topic.
148
+ *
149
+ * @param name the name of the topic. The name must start with a letter, and contain only letters
150
+ * ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
151
+ * periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}).
152
+ * It must be between 3 and 255 characters in length and cannot begin with the string
153
+ * {@code goog}.
154
+ */
116
155
public static Builder builder (String name ) {
117
156
return new BuilderImpl (name );
118
157
}
0 commit comments