2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.protocol.pcep.object;
10 import java.util.ArrayList;
11 import java.util.Collections;
12 import java.util.List;
14 import org.opendaylight.protocol.pcep.PCEPObject;
15 import org.opendaylight.protocol.pcep.message.PCEPReplyMessage;
18 * Composite SvecObject used in {@link PCEPReplyMessage}
20 public class CompositeReplySvecObject {
22 private final PCEPSvecObject svec;
23 private final PCEPObjectiveFunctionObject objectiveFunction;
24 private final List<PCEPMetricObject> metrics;
27 * Constructs basic composite object only with mandatory objects.
32 public CompositeReplySvecObject(PCEPSvecObject svec) {
33 this(svec, null, null);
37 * Constructs composite object also with optional objects.
41 * @param objectiveFunction
42 * PCEPObjectiveFunctionObject
44 * list of PCEPMetricObject
46 public CompositeReplySvecObject(PCEPSvecObject svec, PCEPObjectiveFunctionObject objectiveFunction, List<PCEPMetricObject> metrics) {
48 throw new IllegalArgumentException("Svec object is mandatory.");
50 this.objectiveFunction = objectiveFunction;
52 this.metrics = metrics;
54 this.metrics = Collections.emptyList();
58 * Gets list of all objects, which are in appropriate order.
60 * @return List<PCEPObject>. Can't be null or empty.
62 public List<PCEPObject> getCompositeAsList() {
63 final List<PCEPObject> list = new ArrayList<PCEPObject>();
65 if (this.objectiveFunction != null)
66 list.add(this.objectiveFunction);
67 if (this.metrics != null && !this.metrics.isEmpty())
68 list.addAll(this.metrics);
73 * Creates this object from a list of PCEPObjects.
76 * List<PCEPObject> list of PCEPObjects from whose this object
78 * @return CompositePathObject
80 public static CompositeReplySvecObject getCompositeFromList(List<PCEPObject> objects) {
81 if (objects == null || objects.isEmpty()) {
82 throw new IllegalArgumentException("List cannot be null or empty.");
85 PCEPSvecObject svec = null;
86 if (objects.get(0) instanceof PCEPSvecObject) {
87 svec = (PCEPSvecObject) objects.get(0);
92 PCEPObjectiveFunctionObject of = null;
93 final List<PCEPMetricObject> metrics = new ArrayList<PCEPMetricObject>();
96 while (!objects.isEmpty()) {
97 final PCEPObject obj = objects.get(0);
102 if (obj instanceof PCEPObjectiveFunctionObject) {
103 of = (PCEPObjectiveFunctionObject) obj;
108 if (obj instanceof PCEPMetricObject) {
109 metrics.add((PCEPMetricObject) obj);
122 return new CompositeReplySvecObject(svec, of, metrics);
126 * Gets {@link PCEPSvecObject}
128 * @return PCEPSvecObject. Can't be null.
130 public PCEPSvecObject getSvec() {
135 * Gets {@link PCEPObjectiveFunctionObject}
137 * @return PCEPObjectiveFunctionObject. May be null.
139 public PCEPObjectiveFunctionObject getObjectiveFunction() {
140 return this.objectiveFunction;
144 * @return the metrics
146 public List<PCEPMetricObject> getMetrics() {
151 public String toString() {
152 final StringBuilder builder = new StringBuilder();
153 builder.append("CompositeReplySvecObject [svec=");
154 builder.append(this.svec);
155 builder.append(", objectiveFunction=");
156 builder.append(this.objectiveFunction);
157 builder.append(", metrics=");
158 builder.append(this.metrics);
160 return builder.toString();
164 public int hashCode() {
165 final int prime = 31;
167 result = prime * result + ((this.metrics == null) ? 0 : this.metrics.hashCode());
168 result = prime * result + ((this.objectiveFunction == null) ? 0 : this.objectiveFunction.hashCode());
169 result = prime * result + ((this.svec == null) ? 0 : this.svec.hashCode());
174 public boolean equals(Object obj) {
179 if (this.getClass() != obj.getClass())
181 final CompositeReplySvecObject other = (CompositeReplySvecObject) obj;
182 if (this.metrics == null) {
183 if (other.metrics != null)
185 } else if (!this.metrics.equals(other.metrics))
187 if (this.objectiveFunction == null) {
188 if (other.objectiveFunction != null)
190 } else if (!this.objectiveFunction.equals(other.objectiveFunction))
192 if (this.svec == null) {
193 if (other.svec != null)
195 } else if (!this.svec.equals(other.svec))