Five more Equals/HashCode/StringBuilder replacements
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / packet / address / EthernetAddress.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 package org.opendaylight.controller.sal.packet.address;
11
12 import java.util.Arrays;
13
14 import javax.xml.bind.annotation.XmlAccessType;
15 import javax.xml.bind.annotation.XmlAccessorType;
16 import javax.xml.bind.annotation.XmlElement;
17 import javax.xml.bind.annotation.XmlRootElement;
18 import javax.xml.bind.annotation.XmlTransient;
19
20 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
21 import org.opendaylight.controller.sal.core.ConstructionException;
22 import org.opendaylight.controller.sal.utils.HexEncode;
23
24 @XmlRootElement
25 @XmlAccessorType(XmlAccessType.NONE)
26 public class EthernetAddress extends DataLinkAddress {
27     private static final long serialVersionUID = 1L;
28     @XmlTransient
29     private byte[] macAddress;
30
31     public static final EthernetAddress BROADCASTMAC = createWellKnownAddress(new byte[] {
32             (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff,
33             (byte) 0xff });
34
35     public static final EthernetAddress INVALIDHOST = BROADCASTMAC;
36
37     public static final String addressName = "Ethernet MAC Address";
38     public static final int SIZE = 6;
39
40     private static final EthernetAddress createWellKnownAddress(byte[] mac) {
41         try {
42             return new EthernetAddress(mac);
43         } catch (ConstructionException ce) {
44             return null;
45         }
46     }
47
48     /* Private constructor to satisfy JAXB */
49     private EthernetAddress() {
50
51     }
52
53     /**
54      * Public constructor for an Ethernet MAC address starting from
55      * the byte constituing the address, the constructor validate the
56      * size of the arrive to make sure it met the expected size
57      *
58      * @param macAddress A byte array in big endian format
59      * representing the Ethernet MAC Address
60      *
61      * @return The constructed object if valid
62      */
63     public EthernetAddress(byte[] macAddress) throws ConstructionException {
64         super(addressName);
65
66         if (macAddress == null) {
67             throw new ConstructionException("Null input parameter passed");
68         }
69
70         if (macAddress.length != SIZE) {
71             throw new ConstructionException(
72                     "Wrong size of passed byte array, expected:" + SIZE
73                             + " got:" + macAddress.length);
74         }
75         this.macAddress = new byte[SIZE];
76         System.arraycopy(macAddress, 0, this.macAddress, 0, SIZE);
77     }
78
79     public EthernetAddress clone() {
80         try {
81             return new EthernetAddress(this.macAddress.clone());
82         } catch (ConstructionException ce) {
83             return null;
84         }
85     }
86
87     /**
88      * Return the Ethernet Mac address in byte array format
89      *
90      * @return The Ethernet Mac address in byte array format
91      */
92     public byte[] getValue() {
93         return this.macAddress;
94     }
95
96     @Override
97     public int hashCode() {
98         final int prime = 31;
99         int result = super.hashCode();
100         result = prime * result + Arrays.hashCode(macAddress);
101         return result;
102     }
103
104     @Override
105     public boolean equals(Object obj) {
106         if (this == obj)
107             return true;
108         if (!super.equals(obj))
109             return false;
110         if (getClass() != obj.getClass())
111             return false;
112         EthernetAddress other = (EthernetAddress) obj;
113         if (!Arrays.equals(macAddress, other.macAddress))
114             return false;
115         return true;
116     }
117
118     @Override
119     public String toString() {
120         return "EthernetAddress[" + ReflectionToStringBuilder.toString(this)
121                 + "]";
122     }
123
124     @XmlElement(name = "macAddress")
125     public String getMacAddress() {
126         return HexEncode.bytesToHexStringFormat(macAddress);
127     }
128 }