Fixing warnings in the sal sub-project.
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / core / Path.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 /**
11  * @file   Path.java
12  *
13  * @brief  Describe a path as a sequence of Edge such that from
14  * each of its Head Node there is an link to the next Tail Node in the sequence
15  *
16  */
17 package org.opendaylight.controller.sal.core;
18
19 import java.io.Serializable;
20 import java.util.LinkedList;
21 import java.util.List;
22
23 import javax.xml.bind.annotation.XmlAccessType;
24 import javax.xml.bind.annotation.XmlAccessorType;
25 import javax.xml.bind.annotation.XmlElement;
26 import javax.xml.bind.annotation.XmlRootElement;
27
28 /**
29  * Describe a path as a sequence of Edge such that from
30  * each of its Head Node there is an link to the next Tail Node in the
31  * sequence
32  *
33  */
34 @XmlRootElement
35 @XmlAccessorType(XmlAccessType.NONE)
36 public class Path implements Serializable {
37     private static final long serialVersionUID = 1L;
38     @XmlElement
39     private List<Edge> edges;
40
41     /**
42      * Private constructor used for JAXB mapping
43      */
44     @SuppressWarnings("unused")
45     private Path() {
46         this.edges = null;
47     }
48
49     /**
50      * Construct an object representing a path, the constructor will
51      * check if the passed list of edges is such that for every
52      * consecutive edges the head node of the first edge coincide with
53      * the tail node of the subsequent in order for connectivity to be there.
54      *
55      * @param edges Edges of the path
56      *
57      */
58     public Path(List<Edge> edges) throws ConstructionException {
59         // Lets check if the list of edges is such that the head node
60         // of an edge is also the tail node of the subsequent one
61         boolean sequential = true;
62         if (edges.size() >= 2) {
63             for (int i = 0; i < edges.size() - 1; i++) {
64                 Edge current = edges.get(i);
65                 Edge next = edges.get(i + 1);
66                 if (!current.getHeadNodeConnector().getNode()
67                         .equals(
68                                 next.getTailNodeConnector()
69                                         .getNode())) {
70                     sequential = false;
71                 }
72             }
73         } else if (edges.size() == 0) {
74             throw new ConstructionException("Path is empty");
75         }
76
77         if (!sequential) {
78             throw new ConstructionException("Path is not sequential");
79         }
80
81         this.edges = edges;
82     }
83
84     /**
85      * Copy Construct for a path
86      *
87      * @param src Path to copy from
88      *
89      */
90     public Path(Path src) throws ConstructionException {
91         if (src != null) {
92             this.edges = new LinkedList<Edge>(src.getEdges());
93         } else {
94             throw new ConstructionException("src supplied was null");
95         }
96     }
97
98     /**
99      * get the First Node of the path
100      *
101      *
102      * @return The start Node of the Path
103      */
104     public Node getStartNode() {
105         return this.edges.get(0).getTailNodeConnector().getNode();
106     }
107
108     /**
109      * get the Last Node of the path
110      *
111      *
112      * @return The last Node of the Path
113      */
114     public Node getEndNode() {
115         return this.edges.get(this.edges.size() - 1).getHeadNodeConnector()
116                 .getNode();
117     }
118
119     /**
120      * getter method for the Path
121      *
122      *
123      * @return Return the list of edges that constitue the Path
124      */
125     public List<Edge> getEdges() {
126         return this.edges;
127     }
128
129     @Override
130     public int hashCode() {
131         final int prime = 31;
132         int result = 1;
133         result = prime * result + ((edges == null) ? 0 : edges.hashCode());
134         return result;
135     }
136
137     @Override
138     public boolean equals(Object obj) {
139         if (this == obj)
140             return true;
141         if (obj == null)
142             return false;
143         if (getClass() != obj.getClass())
144             return false;
145         Path other = (Path) obj;
146         if (edges == null) {
147             if (other.edges != null)
148                 return false;
149         } else if (!edges.equals(other.edges))
150             return false;
151         return true;
152     }
153
154     @Override
155     public String toString() {
156         StringBuilder sb = new StringBuilder();
157         sb.append("[");
158         for (int i = 0; i < this.edges.size(); i++) {
159             if (i != 0) {
160                 // add the comma to the previous element
161                 sb.append(",");
162             }
163             sb.append(this.edges.get(i).toString());
164         }
165         sb.append("]");
166         return sb.toString();
167     }
168 }