Skip to content

Commit

Permalink
feat: simplified the code and fixed the review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
athakor authored and sydney-munro committed Mar 17, 2022
1 parent ce09c25 commit 8489ff2
Show file tree
Hide file tree
Showing 10 changed files with 315 additions and 616 deletions.
2 changes: 1 addition & 1 deletion google-cloud-storage/clirr-ignored-differences.xml
Expand Up @@ -13,7 +13,7 @@
</difference>
<difference>
<className>com/google/cloud/storage/Storage</className>
<method>com.google.cloud.storage.Notification createNotification(java.lang.String, com.google.cloud.storage.NotificationInfo)</method>
<method>com.google.cloud.storage.Notification createNotification(java.lang.String, com.google.cloud.storage.Notification)</method>
<differenceType>7012</differenceType>
</difference>
<difference>
Expand Down
Expand Up @@ -13,127 +13,301 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.google.cloud.storage;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.api.pathtemplate.PathTemplate;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
* Google Storage Notification object;
* Google Storage Notification metadata;
*
* @see <a href="https://cloud.google.com/storage/docs/json_api/v1/notifications">notifications</a>
* @see <a href="https://cloud.google.com/storage/docs/concepts-techniques#concepts">Concepts and
* Terminology</a>
*/
public class Notification extends NotificationInfo {
public class Notification implements Serializable {

private static final long serialVersionUID = 5725883368559753810L;
private static final PathTemplate PATH_TEMPLATE =
PathTemplate.createWithoutUrlEncoding("projects/{project}/topics/{topic}");

public enum PayloadFormat {
JSON_API_V1,
NONE
}

static final Function<com.google.api.services.storage.model.Notification, Notification>
FROM_PB_FUNCTION =
new Function<com.google.api.services.storage.model.Notification, Notification>() {
@Override
public Notification apply(com.google.api.services.storage.model.Notification pb) {
return Notification.fromPb(pb);
}
};
static final Function<Notification, com.google.api.services.storage.model.Notification>
TO_PB_FUNCTION =
new Function<Notification, com.google.api.services.storage.model.Notification>() {
@Override
public com.google.api.services.storage.model.Notification apply(
Notification notification) {
return notification.toPb();
}
};
private final String generatedId;
private final String topic;
private final List<String> eventTypes;
private final Map<String, String> customAttributes;
private final PayloadFormat payloadFormat;
private final String objectNamePrefix;
private final String etag;
private final String selfLink;

/** Builder for {@code NotificatioInfo}. */
public static class Builder {

private final StorageOptions options;
private transient Storage storage;
private String generatedId;
private String topic;
private List<String> eventTypes;
private Map<String, String> customAttributes;
private PayloadFormat payloadFormat;
private String objectNamePrefix;
private String etag;
private String selfLink;

/** Builder for {@code Notification}. */
public static class Builder extends NotificationInfo.Builder {
private final Storage storage;
private final NotificationInfo.BuilderImpl infoBuilder;
private Builder(String topic) {
this.topic = topic;
}

Builder(Notification notification) {
this.storage = notification.storage;
this.infoBuilder = new NotificationInfo.BuilderImpl(notification);
private Builder(Notification notification) {
generatedId = notification.generatedId;
etag = notification.etag;
selfLink = notification.selfLink;
topic = notification.topic;
eventTypes = notification.eventTypes;
customAttributes = notification.customAttributes;
payloadFormat = notification.payloadFormat;
objectNamePrefix = notification.objectNamePrefix;
}

@Override
public Builder setGeneratedId(String generatedId) {
infoBuilder.setGeneratedId(generatedId);
this.generatedId = generatedId;
return this;
}

@Override
public Builder setSelfLink(String selfLink) {
infoBuilder.setSelfLink(selfLink);
this.selfLink = selfLink;
return this;
}

@Override
/** The name of the topic. It must have the format "projects/{project}/topics/{topic}". */
public Builder setTopic(String topic) {
infoBuilder.setTopic(topic);
this.topic = topic;
return this;
}

@Override
public Builder setPayloadFormat(PayloadFormat payloadFormat) {
infoBuilder.setPayloadFormat(payloadFormat);
this.payloadFormat = payloadFormat;
return this;
}

@Override
public Builder setObjectNamePrefix(String objectNamePrefix) {
infoBuilder.setObjectNamePrefix(objectNamePrefix);
this.objectNamePrefix = objectNamePrefix;
return this;
}

@Override
public Builder setEventTypes(Iterable<String> eventTypes) {
infoBuilder.setEventTypes(eventTypes);
this.eventTypes = eventTypes != null ? ImmutableList.copyOf(eventTypes) : null;
return this;
}

@Override
public Builder setEtag(String etag) {
infoBuilder.setEtag(etag);
this.etag = etag;
return this;
}

@Override
public Builder setCustomAttributes(Map<String, String> customAttributes) {
infoBuilder.setCustomAttributes(customAttributes);
this.customAttributes =
customAttributes != null ? ImmutableMap.copyOf(customAttributes) : null;
return this;
}

@Override
public Notification build() {
return new Notification(storage, infoBuilder);
checkNotNull(topic);
return new Notification(this);
}
}

Notification(Storage storage, NotificationInfo.BuilderImpl infoBuilder) {
super(infoBuilder);
this.storage = checkNotNull(storage);
this.options = storage.getOptions();
private Notification(Builder builder) {
generatedId = builder.generatedId;
etag = builder.etag;
selfLink = builder.selfLink;
topic = builder.topic;
eventTypes = builder.eventTypes;
customAttributes = builder.customAttributes;
payloadFormat = builder.payloadFormat;
objectNamePrefix = builder.objectNamePrefix;
}

/** Returns the service-generated id for the notification. */
public String getGeneratedId() {
return generatedId;
}

/** Returns the notification's {@code Storage} object used to issue requests. */
public Storage getStorage() {
return storage;
/** Returns the topic to which this subscription publishes. */
public String getTopic() {
return topic;
}

/** Returns the canonical URI of this topic as a string. */
public String getSelfLink() {
return selfLink;
}

/** Returns the desired content of the Payload. */
public PayloadFormat getPayloadFormat() {
return payloadFormat;
}

/** Returns the object name prefix for which this notification configuration applies. */
public String getObjectNamePrefix() {
return objectNamePrefix;
}

/**
* Returns HTTP 1.1 Entity tag for the notification.
*
* @see <a href="http://tools.ietf.org/html/rfc2616#section-3.11">Entity Tags</a>
*/
public String getEtag() {
return etag;
}

/**
* Returns the list of event types that this notification will apply to. If empty, notifications
* will be sent on all event types.
*
* @see <a href="https://cloud.google.com/storage/docs/cross-origin">Cross-Origin Resource Sharing
* (CORS)</a>
*/
public List<String> getEventTypes() {
return eventTypes;
}

/**
* Returns the list of additional attributes to attach to each Cloud PubSub message published for
* this notification subscription.
*
* @see <a href="https://cloud.google.com/storage/docs/access-control#About-Access-Control-Lists">
* About Access Control Lists</a>
*/
public Map<String, String> getCustomAttributes() {
return customAttributes;
}

@Override
public Builder toBuilder() {
return new Notification.Builder(this);
public int hashCode() {
return Objects.hash(getTopic());
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
public boolean equals(Object obj) {
return obj == this
|| obj != null
&& obj.getClass().equals(Notification.class)
&& Objects.equals(toPb(), ((Notification) obj).toPb());
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("topic", getTopic()).toString();
}

com.google.api.services.storage.model.Notification toPb() {
com.google.api.services.storage.model.Notification notificationPb =
new com.google.api.services.storage.model.Notification();
notificationPb.setId(generatedId);
notificationPb.setEtag(etag);
if (customAttributes != null) {
notificationPb.setCustomAttributes(customAttributes);
}
if (eventTypes != null) {
notificationPb.setEventTypes(eventTypes);
}
if (o == null || getClass() != o.getClass()) {
return false;
if (objectNamePrefix != null) {
notificationPb.setObjectNamePrefix(objectNamePrefix);
}
if (!super.equals(o)) {
return false;
if (payloadFormat != null) {
notificationPb.setPayloadFormat(payloadFormat.toString());
} else {
notificationPb.setPayloadFormat(PayloadFormat.NONE.toString());
}
Notification that = (Notification) o;
return Objects.equals(toPb(), that.toPb()) && Objects.equals(options, that.options);
notificationPb.setSelfLink(selfLink);
notificationPb.setTopic(topic);

return notificationPb;
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), options, storage);
/**
* Creates a {@code Notification} object for the provided topic name.
*
* @param topic The name of the topic. It must have the format
* "projects/{project}/topics/{topic}".
*/
public static Notification of(String topic) {
PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format");
return newBuilder(topic).build();
}

/** Returns a builder for the current notification. */
public Builder toBuilder() {
return new Builder(this);
}

/**
* Returns a {@code Notification} builder where the topic's name is set to the provided name.
*
* @param topic The name of the topic. It must have the format
* "projects/{project}/topics/{topic}".
*/
public static Builder newBuilder(String topic) {
PATH_TEMPLATE.validatedMatch(topic, "topic name must be in valid format");
return new Builder(topic);
}

static Notification fromPb(
Storage storage, com.google.api.services.storage.model.Notification notificationPb) {
return new Notification(
storage, new NotificationInfo.BuilderImpl(NotificationInfo.fromPb(notificationPb)));
static Notification fromPb(com.google.api.services.storage.model.Notification notificationPb) {
Builder builder = newBuilder(notificationPb.getTopic());
if (notificationPb.getId() != null) {
builder.setGeneratedId(notificationPb.getId());
}
if (notificationPb.getEtag() != null) {
builder.setEtag(notificationPb.getEtag());
}
if (notificationPb.getCustomAttributes() != null) {
builder.setCustomAttributes(notificationPb.getCustomAttributes());
}
if (notificationPb.getSelfLink() != null) {
builder.setSelfLink(notificationPb.getSelfLink());
}
if (notificationPb.getObjectNamePrefix() != null) {
builder.setObjectNamePrefix(notificationPb.getObjectNamePrefix());
}
if (notificationPb.getTopic() != null) {
builder.setTopic(notificationPb.getTopic());
}
if (notificationPb.getEventTypes() != null) {
builder.setEventTypes(notificationPb.getEventTypes());
}
if (notificationPb.getPayloadFormat() != null) {
builder.setPayloadFormat(PayloadFormat.valueOf(notificationPb.getPayloadFormat()));
}
return builder.build();
}
}

0 comments on commit 8489ff2

Please sign in to comment.