2 * ============LICENSE_START==========================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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============================================
21 package org.onap.aai.champcore.model;
23 import java.util.HashMap;
24 import java.util.HashSet;
28 import org.onap.aai.champcore.model.fluent.partition.CreateChampPartitionable;
29 import org.onap.aai.champcore.model.fluent.partition.impl.CreateChampPartionableImpl;
31 public final class ChampPartition {
33 private final Set<ChampObject> champObjects;
34 private final Set<ChampRelationship> champRelationships;
36 private ChampPartition() {
37 throw new RuntimeException("Cannot call ChampGraph() constructor");
40 private ChampPartition(Builder builder) {
41 this.champObjects = builder.champObjects;
42 this.champRelationships = builder.champRelationships;
45 public static CreateChampPartitionable create() {
46 return new CreateChampPartionableImpl();
49 public Set<ChampObject> getChampObjects() { return champObjects; }
50 public Set<ChampRelationship> getChampRelationships() { return champRelationships; }
52 public Set<ChampRelationship> getIncidentRelationships(ChampObject source) {
53 final Set<ChampRelationship> incidentRelationships = new HashSet<ChampRelationship> ();
55 for (ChampRelationship relationship : getChampRelationships()) {
56 if (relationship.getSource().equals(source) ||
57 relationship.getTarget().equals(source)) {
58 incidentRelationships.add(relationship);
62 return incidentRelationships;
65 public Map<String, Set<ChampRelationship>> getIncidentRelationshipsByType(ChampObject source) {
66 final Map<String, Set<ChampRelationship>> incidentRelationships = new HashMap<String, Set<ChampRelationship>> ();
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> ());
75 incidentRelationships.get(relationship.getType()).add(relationship);
79 return incidentRelationships;
82 public static class Builder {
83 private final Set<ChampObject> champObjects;
84 private final Set<ChampRelationship> champRelationships;
87 this.champObjects = new HashSet<ChampObject> ();
88 this.champRelationships = new HashSet<ChampRelationship> ();
91 public Builder object(ChampObject object) {
92 champObjects.add(object);
96 public Builder relationship(ChampRelationship relationship) {
97 champRelationships.add(relationship);
101 public Builder objects(Set<ChampObject> objects) {
102 champObjects.addAll(objects);
106 public Builder relationships(Set<ChampRelationship> relationships) {
107 champRelationships.addAll(relationships);
111 public ChampPartition build() {
112 return new ChampPartition(this);
117 public String toString() {
119 final StringBuilder sb = new StringBuilder();
121 sb.append("{objects: [");
123 for (ChampObject object : champObjects) {
124 sb.append(object.toString());
128 if (sb.charAt(sb.length() - 1) == ',') {
129 sb.deleteCharAt(sb.length() - 1); //Delete last comma
132 sb.append("], relationships: [");
134 for (ChampRelationship relationship : champRelationships) {
135 sb.append(relationship.toString());
139 if (sb.charAt(sb.length() - 1) == ',') {
140 sb.deleteCharAt(sb.length() - 1); //Delete last comma
145 return sb.toString();