BowlerKernel
MACAddress.java
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright 2010 Neuron Robotics, LLC
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  ******************************************************************************/
15 package com.neuronrobotics.sdk.common;
16 // TODO: Auto-generated Javadoc
22 public class MACAddress implements ISendable {
23 
25  public static final String BROADCAST = "00:00:00:00:00:00";
26 
28  private byte [] address = new byte[]{0,0,0,0,0,0};
29 
33  public MACAddress(){
34  }
39  public MACAddress(String address) {
40  init(address);
41  }
42 
48  private void init(String address){
49  address = address.toUpperCase().trim();
50  address = address.replace("-", ":");
51 
52  if (address.matches("^([0-9A-Z]{2}:){5}[0-9A-Z]{2}$")) {
53  String[] strs = address.split(":");
54  for(int i=0; i<6; i++) {
55  this.address[i] = Integer.decode("0x" + strs[i]).byteValue();
56  }
57  }
58  }
59 
64  public MACAddress(byte[] address) {
65  for(int i=0; i<6; i++) {
66  this.address[i] = address[i];
67  }
68  }
69 
76  public boolean equals(Object o) {
77  if(!(o instanceof MACAddress)) { throw new RuntimeException("Object being compared is not of type MACAddress"); }
78 
79  return equals((MACAddress) o);
80  }
81 
88  public boolean equals(MACAddress addr){
89  for(int i=0; i<6; i++) {
90  if(addr.address[i] != address[i]) {
91  return false;
92  }
93  }
94  return true;
95  }
96 
97  /* (non-Javadoc)
98  * @see java.lang.Object#toString()
99  */
100  public String toString() {
101  String rtn = "";
102  for(int i=0;i< address.length;i++){
103  rtn += getHexByteString(i)+":";
104  }
105  rtn = rtn.substring(0, rtn.length()-1);
106  return rtn.toUpperCase();
107  }
108 
115  public String getHexByteString(int index){
116  return String.format("%02x", address[index]);
117  }
118 
124  public boolean isValid() {
125  return true;
126  }
127 
128  /* (non-Javadoc)
129  * @see com.neuronrobotics.sdk.common.ISendable#getBytes()
130  */
131 
132  public byte[] getBytes() {
133  return address;
134  }
135 
139  public void increment(){
140  if(address[5]<255) {
141  address[5]++;
142  return;
143  }else {
144  if(address[4]<255) {
145  address[5]=0;
146  address[4]++;
147  }else {
148  if(address[3]<255) {
149  address[5]=0;
150  address[4]=0;
151  address[3]++;
152  }else {
153  throw new RuntimeException("MAC Address can not be incremented!");
154  }
155  }
156  }
157  }
158 
164  public void setValues(MACAddress address2) {
165  //System.out.println("Setting new values: "+address2);
166  for(int i=0; i<6; i++) {
167  address[i] = address2.address[i];
168  }
169  }
170 }
void setValues(MACAddress address2)