4982d11d67898a19311442fe10fa37c50fb65cf4
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine - Common Modules
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.common.endpoints.event.comm.bus;
22
23 import java.util.Properties;
24
25 /**
26  * Builder of properties used when configuring topics.
27  */
28 public class TopicPropertyBuilder {
29     private final Properties properties = new Properties();
30     private final String prefix;
31     private String topicPrefix;
32
33     /**
34      * Constructs the object.
35      *
36      * @param prefix the prefix for the properties to be built
37      */
38     public TopicPropertyBuilder(String prefix) {
39         this.prefix = prefix;
40         properties.setProperty(prefix, "");
41     }
42
43     /**
44      * Constructs the properties from the builder.
45      *
46      * @return a copy of the properties
47      */
48     public Properties build() {
49         Properties props = new Properties();
50         props.putAll(properties);
51
52         return props;
53     }
54
55     /**
56      * Adds a topic to the list of topics. Also sets the current topic so that subsequent
57      * invocations of property methods will manipulate the topic's properties.
58      *
59      * @param topic the topic to be added
60      * @return this builder
61      */
62     public TopicPropertyBuilder addTopic(String topic) {
63         // add topic to the list of topics
64         String topicList = properties.getProperty(prefix);
65         if (!topicList.isEmpty()) {
66             topicList += ",";
67         }
68         topicList += topic;
69         properties.setProperty(prefix, topicList);
70
71         setTopic(topic);
72
73         return this;
74     }
75
76     /**
77      * Sets the topic for which subsequent properties will be managed.
78      *
79      * @param topic the topic
80      * @return this builder
81      */
82     public TopicPropertyBuilder setTopic(String topic) {
83         this.topicPrefix = prefix + "." + topic;
84         return this;
85     }
86
87     /**
88      * Sets a topic's property.
89      *
90      * @param name name of the property
91      * @param value value to which the property should be set
92      * @return this builder
93      */
94     public TopicPropertyBuilder setTopicProperty(String name, Object value) {
95         properties.setProperty(topicPrefix + name, value.toString());
96         return this;
97     }
98
99     /**
100      * Removes a topic's property.
101      *
102      * @param name name of the property
103      * @return this builder
104      */
105     public TopicPropertyBuilder removeTopicProperty(String name) {
106         properties.remove(topicPrefix + name);
107         return this;
108     }
109 }
110