Merge changes I936ddd89,I1f20b8df
[bgpcep.git] / pcep / api / src / main / java / org / opendaylight / protocol / pcep / PCEPMessage.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.protocol.pcep;
9
10 import java.util.List;
11
12 /**
13  * Basic structure for PCEP Message. Cannot be instantiated directly. Current PCEP version is 1. Each message contains a
14  * list of PCEP objects.
15  * 
16  */
17 public abstract class PCEPMessage {
18
19         private static final long serialVersionUID = 4293319459468168384L;
20
21         /**
22          * Current supported version of PCEP.
23          */
24         public static final int PCEP_VERSION = 1;
25
26         private final List<PCEPObject> objects;
27
28         /**
29          * Constructor is protected to prevent direct instantiation, but to allow to call this constructor via super().
30          * 
31          * @param objects
32          */
33         protected PCEPMessage(final List<PCEPObject> objects) {
34                 if (objects.contains(null)) {
35                         throw new IllegalArgumentException("Object list contains null element at offset " + objects.indexOf(null));
36                 }
37
38                 this.objects = objects;
39         }
40
41         /**
42          * Returns list of all objects that the message contains
43          * 
44          * @return list of all objects that the message contains
45          */
46         public List<PCEPObject> getAllObjects() {
47                 return this.objects;
48         }
49
50         @Override
51         public int hashCode() {
52                 final int prime = 31;
53                 int result = 1;
54                 result = prime * result + ((this.objects == null) ? 0 : this.objects.hashCode());
55                 return result;
56         }
57
58         @Override
59         public boolean equals(final Object obj) {
60                 if (this == obj) {
61                         return true;
62                 }
63                 if (obj == null) {
64                         return false;
65                 }
66                 if (this.getClass() != obj.getClass()) {
67                         return false;
68                 }
69                 final PCEPMessage other = (PCEPMessage) obj;
70                 if (this.objects == null) {
71                         if (other.objects != null) {
72                                 return false;
73                         }
74                 } else if (!this.objects.equals(other.objects)) {
75                         return false;
76                 }
77                 return true;
78         }
79
80         @Override
81         public String toString() {
82                 final StringBuilder builder = new StringBuilder();
83                 builder.append("PCEPMessage [objects=");
84                 builder.append(this.objects);
85                 builder.append("]");
86                 return builder.toString();
87         }
88
89 }