e8031c1add51f22f0581d0f5687892ac307ba56b
[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 abstract 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     }
41
42     /**
43      * Constructs the properties from the builder.
44      *
45      * @return a copy of the properties
46      */
47     public Properties build() {
48         Properties props = new Properties();
49         props.putAll(properties);
50
51         return props;
52     }
53
54     /**
55      * Adds a topic to the list of topics, configuring all of its properties with default
56      * values.
57      * 
58      * @param topic the topic to be added
59      * @return this builder
60      */
61     public abstract TopicPropertyBuilder makeTopic(String topic);
62
63     /**
64      * Adds a topic to the list of topics. Also sets the current topic so that subsequent
65      * invocations of property methods will manipulate the topic's properties.
66      *
67      * @param topic the topic to be added
68      * @return this builder
69      */
70     public TopicPropertyBuilder addTopic(String topic) {
71         // add topic to the list of topics
72         String topicList = properties.getProperty(prefix);
73         if (topicList == null || topicList.isEmpty()) {
74             topicList = topic;
75         } else {
76             topicList += "," + topic;
77         }
78
79         properties.setProperty(prefix, topicList);
80
81         setTopic(topic);
82
83         return this;
84     }
85
86     /**
87      * Sets the topic for which subsequent properties will be managed.
88      *
89      * @param topic the topic
90      * @return this builder
91      */
92     public TopicPropertyBuilder setTopic(String topic) {
93         this.topicPrefix = prefix + "." + topic;
94         return this;
95     }
96
97     /**
98      * Sets a topic's property.
99      *
100      * @param name name of the property
101      * @param value value to which the property should be set
102      * @return this builder
103      */
104     public TopicPropertyBuilder setTopicProperty(String name, Object value) {
105         properties.setProperty(topicPrefix + name, value.toString());
106         return this;
107     }
108
109     /**
110      * Removes a topic's property.
111      *
112      * @param name name of the property
113      * @return this builder
114      */
115     public TopicPropertyBuilder removeTopicProperty(String name) {
116         properties.remove(topicPrefix + name);
117         return this;
118     }
119 }
120