|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.pubsub; |
| 18 | + |
| 19 | +import static com.google.cloud.pubsub.spi.v1.SubscriberApi.parseProjectFromSubscriptionName; |
| 20 | +import static com.google.cloud.pubsub.spi.v1.SubscriberApi.parseSubscriptionFromSubscriptionName; |
| 21 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 22 | + |
| 23 | +import com.google.common.base.MoreObjects; |
| 24 | + |
| 25 | +import java.io.Serializable; |
| 26 | +import java.util.Objects; |
| 27 | + |
| 28 | +/** |
| 29 | + * Identity for a Google PubSub subscription. {@code SubscriptionId} objects are returned by the |
| 30 | + * {@link PubSub#listSubscriptions(String, PubSub.ListOption...)} and |
| 31 | + * {@link PubSub#listSubscriptionsAsync(String, PubSub.ListOption...)} methods as a topic may have |
| 32 | + * subscriptions from different projects. |
| 33 | + */ |
| 34 | +public class SubscriptionId implements Serializable { |
| 35 | + |
| 36 | + private static final long serialVersionUID = 6507142968866856283L; |
| 37 | + |
| 38 | + private final String project; |
| 39 | + private final String subscription; |
| 40 | + |
| 41 | + SubscriptionId(String project, String subscription) { |
| 42 | + this.project = checkNotNull(project); |
| 43 | + this.subscription = checkNotNull(subscription); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Returns the name of the project where the subscription resides. |
| 48 | + */ |
| 49 | + public String project() { |
| 50 | + return project; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Returns the name of the subscription. |
| 55 | + */ |
| 56 | + public String subscription() { |
| 57 | + return subscription; |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public String toString() { |
| 62 | + return MoreObjects.toStringHelper(this) |
| 63 | + .add("project", project) |
| 64 | + .add("subscription", subscription).toString(); |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public final int hashCode() { |
| 69 | + return Objects.hash(project, subscription); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public final boolean equals(Object obj) { |
| 74 | + if (obj == this) { |
| 75 | + return true; |
| 76 | + } |
| 77 | + if (!(obj instanceof SubscriptionId)) { |
| 78 | + return false; |
| 79 | + } |
| 80 | + SubscriptionId other = (SubscriptionId) obj; |
| 81 | + return Objects.equals(project, other.project) |
| 82 | + && Objects.equals(subscription, other.subscription); |
| 83 | + } |
| 84 | + |
| 85 | + static SubscriptionId fromPb(String pb) { |
| 86 | + return new SubscriptionId(parseProjectFromSubscriptionName(pb), |
| 87 | + parseSubscriptionFromSubscriptionName(pb)); |
| 88 | + } |
| 89 | +} |
0 commit comments