|
| 1 | +/* |
| 2 | + * Copyright 2020 Google LLC |
| 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 | +package com.google.cloud.storage; |
| 17 | + |
| 18 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | + |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Objects; |
| 22 | + |
| 23 | +/** |
| 24 | + * The class representing Pub/Sub notifications for the Storage. See <a |
| 25 | + * href="https://cloud.google.com/storage/docs/pubsub-notifications">pubsub-notifications</a> for |
| 26 | + * details. |
| 27 | + */ |
| 28 | +public class Notification extends NotificationInfo { |
| 29 | + |
| 30 | + private final StorageOptions options; |
| 31 | + private transient Storage storage; |
| 32 | + |
| 33 | + /** Builder for {@code Notification}. */ |
| 34 | + public static class Builder extends NotificationInfo.Builder { |
| 35 | + private final Storage storage; |
| 36 | + private final NotificationInfo.BuilderImpl infoBuilder; |
| 37 | + |
| 38 | + Builder(Notification notification) { |
| 39 | + this.storage = notification.storage; |
| 40 | + this.infoBuilder = new NotificationInfo.BuilderImpl(notification); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + Builder setNotificationId(String notificationId) { |
| 45 | + infoBuilder.setNotificationId(notificationId); |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Builder setSelfLink(String selfLink) { |
| 51 | + infoBuilder.setSelfLink(selfLink); |
| 52 | + return this; |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public Builder setTopic(String topic) { |
| 57 | + infoBuilder.setTopic(topic); |
| 58 | + return this; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public Builder setPayloadFormat(PayloadFormat payloadFormat) { |
| 63 | + infoBuilder.setPayloadFormat(payloadFormat); |
| 64 | + return this; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Builder setObjectNamePrefix(String objectNamePrefix) { |
| 69 | + infoBuilder.setObjectNamePrefix(objectNamePrefix); |
| 70 | + return this; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Builder setEventTypes(EventType... eventTypes) { |
| 75 | + infoBuilder.setEventTypes(eventTypes); |
| 76 | + return this; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Builder setEtag(String etag) { |
| 81 | + infoBuilder.setEtag(etag); |
| 82 | + return this; |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + public Builder setCustomAttributes(Map<String, String> customAttributes) { |
| 87 | + infoBuilder.setCustomAttributes(customAttributes); |
| 88 | + return this; |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public Notification build() { |
| 93 | + return new Notification(storage, infoBuilder); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + Notification(Storage storage, NotificationInfo.BuilderImpl infoBuilder) { |
| 98 | + super(infoBuilder); |
| 99 | + this.storage = checkNotNull(storage); |
| 100 | + this.options = storage.getOptions(); |
| 101 | + } |
| 102 | + |
| 103 | + /** Returns the notification's {@code Storage} object used to issue requests. */ |
| 104 | + public Storage getStorage() { |
| 105 | + return storage; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public Builder toBuilder() { |
| 110 | + return new Notification.Builder(this); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public boolean equals(Object o) { |
| 115 | + if (this == o) { |
| 116 | + return true; |
| 117 | + } |
| 118 | + if (o == null || getClass() != o.getClass()) { |
| 119 | + return false; |
| 120 | + } |
| 121 | + if (!super.equals(o)) { |
| 122 | + return false; |
| 123 | + } |
| 124 | + Notification notification = (Notification) o; |
| 125 | + return Objects.equals(toPb(), notification.toPb()) |
| 126 | + && Objects.equals(options, notification.options); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public int hashCode() { |
| 131 | + return Objects.hash(super.hashCode(), options, storage); |
| 132 | + } |
| 133 | + |
| 134 | + static Notification fromPb( |
| 135 | + Storage storage, com.google.api.services.storage.model.Notification notificationPb) { |
| 136 | + return new Notification( |
| 137 | + storage, new NotificationInfo.BuilderImpl(NotificationInfo.fromPb(notificationPb))); |
| 138 | + } |
| 139 | +} |
0 commit comments