blob: b0cb4d723c5b6dee0f577207d3a11632b0de9081 [file] [log] [blame]
youngscff9cf0e2016-12-14 19:01:27 +08001/*
Brian O'Connor23c7e322017-08-03 18:48:27 -07002 * Copyright 2016-present Open Networking Foundation
youngscff9cf0e2016-12-14 19:01:27 +08003 *
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 */
16package org.onosproject.nemo.model.intent;
17
18import java.util.HashMap;
19import java.util.Map;
20import java.util.Objects;
21
22import static com.google.common.base.MoreObjects.toStringHelper;
23
24/**
25 * Representation of register user output builder.
26 */
27public class RegisterUserOutputBuilder extends DefaultOutputBuilderBase {
28
29 private Map<Class<? extends RegisterUserOutput>, RegisterUserOutput>
30 augmentation = new HashMap<>();
31
32 public RegisterUserOutputBuilder() {
33 }
34
35 /**
36 * Constructs the register user output builder from a result code.
37 *
38 * @param rpc common RPC result
39 */
40 public RegisterUserOutputBuilder(CommonRpcResult rpc) {
41 resultCode = rpc.getResultCode();
42 message = rpc.getMessage();
43 }
44
45 /**
46 * Constructs register user output builder.
47 *
48 * @param base register user output object
49 */
50 public RegisterUserOutputBuilder(RegisterUserOutput base) {
51 message = base.getMessage();
52 resultCode = base.getResultCode();
53 if (base instanceof RegisterUserOutputImpl) {
54 RegisterUserOutputImpl impl = (RegisterUserOutputImpl) base;
55 if (!impl.augmentation.isEmpty()) {
56 augmentation = new HashMap<>(impl.augmentation);
57 }
58 }
59 }
60
61 /**
62 * Adds augmentation by a class type and a register user output.
63 * If the augmentation is null, remove the augmentation type key
64 * from the hash map.
65 *
66 * @param augmentationType type that extends RegisterUserOutput
67 * @param augmentation register user output object
68 * @return self
69 */
70 public RegisterUserOutputBuilder addAugmentation(
71 Class<? extends RegisterUserOutput> augmentationType,
72 RegisterUserOutput augmentation) {
73 if (augmentation == null) {
74 return removeAugmentation(augmentationType);
75 }
76 this.augmentation.put(augmentationType, augmentation);
77 return this;
78 }
79
80 /**
81 * Removes augmentation by a class type.
82 *
83 * @param augmentationType type that extends RegisterUserOutput
84 * @return self
85 */
86 public RegisterUserOutputBuilder removeAugmentation(
87 Class<? extends RegisterUserOutput> augmentationType) {
88 augmentation.remove(augmentationType);
89 return this;
90 }
91
92 /**
93 * Builds a register user output object.
94 *
95 * @return the register user output object
96 */
97 public RegisterUserOutput build() {
98 return new RegisterUserOutputImpl(this);
99 }
100
101 /**
102 * Representation of the implement of RegisterUserOutput.
103 */
104 private static final class RegisterUserOutputImpl
105 extends DefaultOutputImplBase
106 implements RegisterUserOutput {
107
108 private final Map<Class<? extends RegisterUserOutput>, RegisterUserOutput>
109 augmentation;
110
111 private RegisterUserOutputImpl(RegisterUserOutputBuilder base) {
112 message = base.message;
113 resultCode = base.resultCode;
114 augmentation = new HashMap<>(base.augmentation);
115 }
116
117 @Override
118 public int hashCode() {
119 return Objects.hash(message, resultCode, augmentation);
120 }
121
122 @Override
123 public boolean equals(Object obj) {
124 if (this == obj) {
125 return true;
126 }
127 if (obj == null) {
128 return false;
129 }
130 if (getClass() != obj.getClass()) {
131 return false;
132 }
133 RegisterUserOutputImpl other = (RegisterUserOutputImpl) obj;
134 return Objects.equals(message, other.getMessage()) &&
135 Objects.equals(resultCode, other.getResultCode()) &&
136 Objects.equals(augmentation, other.augmentation);
137 }
138
139 @Override
140 public String toString() {
141 return toStringHelper(this)
142 .add("message", message)
143 .add("resultCode", resultCode)
144 .add("augmentation", augmentation).toString();
145 }
146 }
147}