Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 18 | package net.onrc.onos.core.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 19 | |
| 20 | import java.util.Arrays; |
| 21 | |
| 22 | /** |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 23 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 24 | */ |
| 25 | public class DHCPOption { |
| 26 | protected byte code; |
| 27 | protected byte length; |
| 28 | protected byte[] data; |
| 29 | |
| 30 | /** |
| 31 | * @return the code |
| 32 | */ |
| 33 | public byte getCode() { |
| 34 | return code; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param code the code to set |
| 39 | */ |
| 40 | public DHCPOption setCode(byte code) { |
| 41 | this.code = code; |
| 42 | return this; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return the length |
| 47 | */ |
| 48 | public byte getLength() { |
| 49 | return length; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @param length the length to set |
| 54 | */ |
| 55 | public DHCPOption setLength(byte length) { |
| 56 | this.length = length; |
| 57 | return this; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @return the data |
| 62 | */ |
| 63 | public byte[] getData() { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 64 | if (this.data == null) { |
| 65 | return null; |
| 66 | } |
| 67 | return this.data.clone(); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * @param data the data to set |
| 72 | */ |
| 73 | public DHCPOption setData(byte[] data) { |
Pavlin Radoslavov | 0a9d5c3 | 2014-04-15 17:45:23 -0700 | [diff] [blame] | 74 | if (data == null) { |
| 75 | this.data = null; |
| 76 | } else { |
| 77 | this.data = data.clone(); |
| 78 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 79 | return this; |
| 80 | } |
| 81 | |
| 82 | /* (non-Javadoc) |
| 83 | * @see java.lang.Object#hashCode() |
| 84 | */ |
| 85 | @Override |
| 86 | public int hashCode() { |
| 87 | final int prime = 31; |
| 88 | int result = 1; |
| 89 | result = prime * result + code; |
| 90 | result = prime * result + Arrays.hashCode(data); |
| 91 | result = prime * result + length; |
| 92 | return result; |
| 93 | } |
| 94 | |
| 95 | /* (non-Javadoc) |
| 96 | * @see java.lang.Object#equals(java.lang.Object) |
| 97 | */ |
| 98 | @Override |
| 99 | public boolean equals(Object obj) { |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 100 | if (this == obj) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 101 | return true; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 102 | } |
| 103 | if (obj == null) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 104 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 105 | } |
| 106 | if (!(obj instanceof DHCPOption)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 107 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 108 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 109 | DHCPOption other = (DHCPOption) obj; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 110 | if (code != other.code) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 111 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 112 | } |
| 113 | if (!Arrays.equals(data, other.data)) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 114 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 115 | } |
| 116 | if (length != other.length) { |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 117 | return false; |
Ray Milkey | b29e626 | 2014-04-09 16:02:14 -0700 | [diff] [blame] | 118 | } |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 119 | return true; |
| 120 | } |
| 121 | |
| 122 | /* (non-Javadoc) |
| 123 | * @see java.lang.Object#toString() |
| 124 | */ |
| 125 | @Override |
| 126 | public String toString() { |
| 127 | return "DHCPOption [code=" + code + ", length=" + length + ", data=" |
| 128 | + Arrays.toString(data) + "]"; |
| 129 | } |
| 130 | } |