Merge "Added @Override annotation above signature"
[aai/champ.git] / src / main / java / org / onap / aai / champ / model / ChampPartition.java
1 /**
2  * ============LICENSE_START==========================================
3  * org.onap.aai
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * Copyright © 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21  */
22 package org.onap.aai.champ.model;
23
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.Map;
27 import java.util.Set;
28
29 import org.onap.aai.champ.model.fluent.partition.CreateChampPartitionable;
30 import org.onap.aai.champ.model.fluent.partition.impl.CreateChampPartionableImpl;
31
32 public final class ChampPartition {
33
34         private final Set<ChampObject> champObjects;
35         private final Set<ChampRelationship> champRelationships;
36
37         private ChampPartition() {
38                 throw new RuntimeException("Cannot call ChampGraph() constructor");
39         }
40         
41         private ChampPartition(Builder builder) {
42                 this.champObjects = builder.champObjects;
43                 this.champRelationships = builder.champRelationships;
44         }
45
46         public static CreateChampPartitionable create() {
47                 return new CreateChampPartionableImpl();
48         }
49
50         public Set<ChampObject> getChampObjects() { return champObjects; }
51         public Set<ChampRelationship> getChampRelationships() { return champRelationships; }
52
53         public Set<ChampRelationship> getIncidentRelationships(ChampObject source) {
54                 final Set<ChampRelationship> incidentRelationships = new HashSet<ChampRelationship> ();
55
56                 for (ChampRelationship relationship : getChampRelationships()) {
57                         if (relationship.getSource().equals(source) ||
58                                 relationship.getTarget().equals(source)) {
59                                 incidentRelationships.add(relationship);
60                         }
61                 }
62
63                 return incidentRelationships;
64         }
65
66         public Map<String, Set<ChampRelationship>> getIncidentRelationshipsByType(ChampObject source) {
67                 final Map<String, Set<ChampRelationship>> incidentRelationships = new HashMap<String, Set<ChampRelationship>> ();
68
69                 for (ChampRelationship relationship : getChampRelationships()) {
70                         if (relationship.getSource().equals(source) ||
71                                 relationship.getTarget().equals(source)) {
72                                 if (!incidentRelationships.containsKey(relationship.getType())) {
73                                         incidentRelationships.put(relationship.getType(), new HashSet<ChampRelationship> ());
74                                 }
75
76                                 incidentRelationships.get(relationship.getType()).add(relationship);
77                         }
78                 }
79
80                 return incidentRelationships;
81         }
82
83         public static class Builder {
84                 private final Set<ChampObject> champObjects;
85                 private final Set<ChampRelationship> champRelationships;
86
87                 public Builder() {
88                         this.champObjects = new HashSet<ChampObject> ();
89                         this.champRelationships = new HashSet<ChampRelationship> ();
90                 }
91
92                 public Builder object(ChampObject object) {
93                         champObjects.add(object);
94                         return this;
95                 }
96
97                 public Builder relationship(ChampRelationship relationship) {
98                         champRelationships.add(relationship);
99                         return this;
100                 }
101
102                 public Builder objects(Set<ChampObject> objects) {
103                         champObjects.addAll(objects);
104                         return this;
105                 }
106                 
107                 public Builder relationships(Set<ChampRelationship> relationships) {
108                         champRelationships.addAll(relationships);
109                         return this;
110                 }
111
112                 public ChampPartition build() {
113                         return new ChampPartition(this);
114                 }
115         }
116
117         @Override
118         public String toString() {
119
120                 final StringBuilder sb = new StringBuilder();
121
122                 sb.append("{objects: [");
123
124                 for (ChampObject object : champObjects) {
125                         sb.append(object.toString());
126                         sb.append(",");
127                 }
128
129                 if (sb.charAt(sb.length() - 1) == ',') sb.deleteCharAt(sb.length() - 1); //Delete last comma
130
131                 sb.append("], relationships: [");
132
133                 for (ChampRelationship relationship : champRelationships) {
134                         sb.append(relationship.toString());
135                         sb.append(",");
136                 }
137
138                 if (sb.charAt(sb.length() - 1) == ',') sb.deleteCharAt(sb.length() - 1); //Delete last comma
139
140                 sb.append("]}");
141
142                 return sb.toString();
143         }
144 }