Update license date and text
[aai/champ.git] / champ-lib / champ-core / src / main / java / org / onap / aai / champcore / model / ChampPartition.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017-2018 Amdocs
7  * ===================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  */
21 package org.onap.aai.champcore.model;
22
23 import java.util.HashMap;
24 import java.util.HashSet;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.onap.aai.champcore.model.fluent.partition.CreateChampPartitionable;
29 import org.onap.aai.champcore.model.fluent.partition.impl.CreateChampPartionableImpl;
30
31 public final class ChampPartition {
32
33         private final Set<ChampObject> champObjects;
34         private final Set<ChampRelationship> champRelationships;
35
36         private ChampPartition() {
37                 throw new RuntimeException("Cannot call ChampGraph() constructor");
38         }
39         
40         private ChampPartition(Builder builder) {
41                 this.champObjects = builder.champObjects;
42                 this.champRelationships = builder.champRelationships;
43         }
44
45         public static CreateChampPartitionable create() {
46                 return new CreateChampPartionableImpl();
47         }
48
49         public Set<ChampObject> getChampObjects() { return champObjects; }
50         public Set<ChampRelationship> getChampRelationships() { return champRelationships; }
51
52         public Set<ChampRelationship> getIncidentRelationships(ChampObject source) {
53                 final Set<ChampRelationship> incidentRelationships = new HashSet<ChampRelationship> ();
54
55                 for (ChampRelationship relationship : getChampRelationships()) {
56                         if (relationship.getSource().equals(source) ||
57                                 relationship.getTarget().equals(source)) {
58                                 incidentRelationships.add(relationship);
59                         }
60                 }
61
62                 return incidentRelationships;
63         }
64
65         public Map<String, Set<ChampRelationship>> getIncidentRelationshipsByType(ChampObject source) {
66                 final Map<String, Set<ChampRelationship>> incidentRelationships = new HashMap<String, Set<ChampRelationship>> ();
67
68                 for (ChampRelationship relationship : getChampRelationships()) {
69                         if (relationship.getSource().equals(source) ||
70                                 relationship.getTarget().equals(source)) {
71                                 if (!incidentRelationships.containsKey(relationship.getType())) {
72                                         incidentRelationships.put(relationship.getType(), new HashSet<ChampRelationship> ());
73                                 }
74
75                                 incidentRelationships.get(relationship.getType()).add(relationship);
76                         }
77                 }
78
79                 return incidentRelationships;
80         }
81
82         public static class Builder {
83                 private final Set<ChampObject> champObjects;
84                 private final Set<ChampRelationship> champRelationships;
85
86                 public Builder() {
87                         this.champObjects = new HashSet<ChampObject> ();
88                         this.champRelationships = new HashSet<ChampRelationship> ();
89                 }
90
91                 public Builder object(ChampObject object) {
92                         champObjects.add(object);
93                         return this;
94                 }
95
96                 public Builder relationship(ChampRelationship relationship) {
97                         champRelationships.add(relationship);
98                         return this;
99                 }
100
101                 public Builder objects(Set<ChampObject> objects) {
102                         champObjects.addAll(objects);
103                         return this;
104                 }
105                 
106                 public Builder relationships(Set<ChampRelationship> relationships) {
107                         champRelationships.addAll(relationships);
108                         return this;
109                 }
110
111                 public ChampPartition build() {
112                         return new ChampPartition(this);
113                 }
114         }
115
116         @Override
117         public String toString() {
118
119                 final StringBuilder sb = new StringBuilder();
120
121                 sb.append("{objects: [");
122
123                 for (ChampObject object : champObjects) {
124                         sb.append(object.toString());
125                         sb.append(",");
126                 }
127
128                 if (sb.charAt(sb.length() - 1) == ',') {
129                         sb.deleteCharAt(sb.length() - 1); //Delete last comma
130                 }
131
132                 sb.append("], relationships: [");
133
134                 for (ChampRelationship relationship : champRelationships) {
135                         sb.append(relationship.toString());
136                         sb.append(",");
137                 }
138
139                 if (sb.charAt(sb.length() - 1) == ',') {
140                         sb.deleteCharAt(sb.length() - 1); //Delete last comma
141                 }
142
143                 sb.append("]}");
144
145                 return sb.toString();
146         }
147 }