blob: bf1fbf839811ae0f27da1a1baa190eaf23b7e70d [file] [log] [blame]
Sanjana Agarwaleb9f0c52016-06-07 11:10:34 -07001/**
2 * Copyright 2016-present Open Networking Laboratory
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6
7 * http://www.apache.org/licenses/LICENSE-2.0
8
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15package org.onosproject.kafkaintegration.api.dto;
16
17import static com.google.common.base.MoreObjects.toStringHelper;
18import static com.google.common.base.Preconditions.checkNotNull;
19
20import java.util.Objects;
21
22import org.onosproject.kafkaintegration.api.dto.OnosEvent.Type;
23
24/**
25 * Representation of a subscription to an event type.
26 *
27 */
28public final class DefaultEventSubscriber implements EventSubscriber {
29 private final String appName;
30 private final EventSubscriberGroupId subscriberGroupId;
31 private final Type eventType;
32
33 /**
34 * Creates a new Event Subscriber.
35 *
36 * @param name Application Name
37 * @param groupId Subscriber group id of the application
38 * @param eventType ONOS event type
39 */
40 public DefaultEventSubscriber(String name, EventSubscriberGroupId groupId,
41 Type eventType) {
42 this.appName = checkNotNull(name);
43 this.subscriberGroupId = checkNotNull(groupId);
44 this.eventType = checkNotNull(eventType);
45 }
46
47 @Override
48 public String appName() {
49 return appName;
50 }
51
52 @Override
53 public EventSubscriberGroupId subscriberGroupId() {
54 return subscriberGroupId;
55 }
56
57 @Override
58 public Type eventType() {
59 return eventType;
60 }
61
62 @Override
63 public int hashCode() {
64 return Objects.hash(appName, subscriberGroupId, eventType);
65 }
66
67 @Override
68 public boolean equals(Object o) {
69 if (o instanceof DefaultEventSubscriber) {
70 DefaultEventSubscriber sub = (DefaultEventSubscriber) o;
71 if (sub.appName.equals(appName)
72 && sub.subscriberGroupId.equals(subscriberGroupId)
73 && sub.eventType.equals(eventType)) {
74 return true;
75 }
76 }
77
78 return false;
79 }
80
81 @Override
82 public String toString() {
83 return toStringHelper(this).add("appName", appName)
84 .addValue(subscriberGroupId.toString())
85 .add("eventType", eventType).toString();
86 }
87 /**
88 * To create an instance of the builder.
89 *
90 * @return instance of builder
91 */
92 public static Builder builder() {
93 return new Builder();
94 }
95 /**
96 * Builder class for Event subscriber.
97 */
98 public static final class Builder implements EventSubscriber.Builder {
99 private String appName;
100 private EventSubscriberGroupId subscriberGroupId;
101 private Type eventType;
102
103 @Override
104 public Builder setAppName(String appName) {
105 this.appName = appName;
106 return this;
107 }
108
109 @Override
110 public Builder setSubscriberGroupId(EventSubscriberGroupId
111 subscriberGroupId) {
112 this.subscriberGroupId = subscriberGroupId;
113 return this;
114 }
115
116 @Override
117 public Builder setEventType(Type eventType) {
118 this.eventType = eventType;
119 return this;
120 }
121
122 @Override
123 public EventSubscriber build() {
124 checkNotNull(appName, "App name cannot be null");
125 checkNotNull(subscriberGroupId, "Subscriber group ID cannot " +
126 "be " +
127 "null");
128 checkNotNull(eventType, "Event type cannot be null");
129
130 return new DefaultEventSubscriber(appName,
131 subscriberGroupId,
132 eventType);
133 }
134 }
135
136}