forked from googleapis/google-cloud-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTopicId.java
More file actions
138 lines (118 loc) · 4.03 KB
/
TopicId.java
File metadata and controls
138 lines (118 loc) · 4.03 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
/*
* 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.cloud.pubsub.spi.v1.PublisherApi.formatTopicName;
import static com.google.cloud.pubsub.spi.v1.PublisherApi.parseProjectFromTopicName;
import static com.google.cloud.pubsub.spi.v1.PublisherApi.parseTopicFromTopicName;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.MoreObjects;
import java.io.Serializable;
import java.util.Objects;
/**
* Identity for a Google PubSub topic. A {@code TopicId} object can be used to create subscriptions
* for topics that possibly reside in different projects.
*/
public final class TopicId implements Serializable {
private static final long serialVersionUID = -4913169763174877777L;
private static final String DELETED_TOPIC_NAME = "_deleted-topic_";
private static final TopicId DELETED_TOPIC = new TopicId(null, DELETED_TOPIC_NAME, true);
private final String project;
private final String topic;
private final boolean isDeleted;
private TopicId(String project, String topic, boolean isDeleted) {
this.project = project;
this.topic = checkNotNull(topic);
this.isDeleted = isDeleted;
}
private TopicId(String project, String topic) {
this(project, topic, false);
}
/**
* Returns the name of the project where the topic resides. If {@code null} the topic is assumed
* to reside in the {@link PubSubOptions#projectId()} project.
*/
public String project() {
return project;
}
/**
* Returns the name of the topic.
*/
public String topic() {
return topic;
}
/**
* Returns {@code true} if this object is the identity of a deleted topic, {@code false}
* otherwhise. If {@code isDeleted()} is {@code true}, {@link #topic()} returns
* "{@code _deleted-topic_}" and {@link #project()} returns {@code null}.
*/
public boolean isDeleted() {
return isDeleted;
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("project", project)
.add("topic", topic)
.add("isDeleted", isDeleted)
.toString();
}
@Override
public int hashCode() {
return Objects.hash(project, topic, isDeleted);
}
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof TopicId)) {
return false;
}
TopicId other = (TopicId) obj;
return Objects.equals(project, other.project)
&& Objects.equals(topic, other.topic)
&& Objects.equals(isDeleted, other.isDeleted);
}
String toPb(String projectId) {
return formatTopicName(project != null ? project : projectId, topic);
}
/**
* Returns the identity of a deleted topic. The deleted topic is such that {@link #isDeleted()}
* returns {@code true}, {@link #topic()} returns "{@code _is_deleted_}" and {@link #project()}
* returns {@code null}.
*/
public static TopicId deletedTopic() {
return DELETED_TOPIC;
}
/**
* Returns a topic identity given the topic name.
*/
public static TopicId of(String topic) {
return new TopicId(null, topic);
}
/**
* Returns a topic identity given project and topic names.
*/
public static TopicId of(String project, String topic) {
return new TopicId(project, topic);
}
static TopicId fromPb(String pb) {
if (Objects.equals(pb, DELETED_TOPIC_NAME)) {
return DELETED_TOPIC;
}
return TopicId.of(parseProjectFromTopicName(pb), parseTopicFromTopicName(pb));
}
}