forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicInfo.java
More file actions
161 lines (135 loc) · 4.9 KB
/
TopicInfo.java
File metadata and controls
161 lines (135 loc) · 4.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.pubsub;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.cloud.pubsub.spi.v1.PublisherApi;
import com.google.common.base.MoreObjects;
import java.io.Serializable;
import java.util.Objects;
/**
* A Google Cloud Pub/Sub topic. A topic is a named resource to which messages are sent by
* publishers. Subscribers can receive messages sent to a topic by creating subscriptions.
*
* @see <a href="https://cloud.google.com/pubsub/overview#data_model">Pub/Sub Data Model</a>
*/
public class TopicInfo implements Serializable {
private static final long serialVersionUID = -5907624842808725353L;
private final String name;
/**
* Builder for {@code TopicInfo} objects.
*/
public abstract static class Builder {
/**
* Sets the name of the topic. The name must start with a letter, and contain only letters
* ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
* periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}). It
* must be between 3 and 255 characters in length and cannot begin with the string {@code goog}.
*/
public abstract Builder name(String name);
/**
* Creates a topic object.
*/
public abstract TopicInfo build();
}
static final class BuilderImpl extends Builder {
private String name;
BuilderImpl(String name) {
this.name = checkNotNull(name);
}
BuilderImpl(TopicInfo topicInfo) {
this.name = topicInfo.name;
}
@Override
public Builder name(String name) {
this.name = checkNotNull(name);
return this;
}
@Override
public TopicInfo build() {
return new TopicInfo(this);
}
}
TopicInfo(BuilderImpl builder) {
name = builder.name;
}
/**
* Returns the name of the topic. The name must start with a letter, and contain only letters
* ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
* periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}). It
* must be between 3 and 255 characters in length and cannot begin with the string {@code goog}.
*/
public String name() {
return name;
}
@Override
public int hashCode() {
return Objects.hash(name);
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || !obj.getClass().equals(this.getClass())) {
return false;
}
return Objects.equals(name, ((TopicInfo) obj).name);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", name)
.toString();
}
com.google.pubsub.v1.Topic toPb(String projectId) {
return com.google.pubsub.v1.Topic.newBuilder()
.setName(PublisherApi.formatTopicName(projectId, name))
.build();
}
static TopicInfo fromPb(com.google.pubsub.v1.Topic topicPb) {
return builder(PublisherApi.parseTopicFromTopicName(topicPb.getName())).build();
}
/**
* Returns a builder for the topic object.
*/
public Builder toBuilder() {
return new BuilderImpl(this);
}
/**
* Creates a {@code TopicInfo} object given the name of the topic.
*
* @param name the name of the topic. The name must start with a letter, and contain only letters
* ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
* periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}).
* It must be between 3 and 255 characters in length and cannot begin with the string
* {@code goog}
*/
public static TopicInfo of(String name) {
return builder(name).build();
}
/**
* Creates a builder for {@code TopicInfo} objects given the name of the topic.
*
* @param name the name of the topic. The name must start with a letter, and contain only letters
* ({@code [A-Za-z]}), numbers ({@code [0-9]}), dashes ({@code -}), underscores ({@code _}),
* periods ({@code .}), tildes ({@code ~}), plus ({@code +}) or percent signs ({@code %}).
* It must be between 3 and 255 characters in length and cannot begin with the string
* {@code goog}
*/
public static Builder builder(String name) {
return new BuilderImpl(name);
}
}