|
| 1 | +package org.kdc; |
| 2 | + |
| 3 | +// This is an incomplete class that I threw together fairly quickly because |
| 4 | +// I wanted to teach myself some more about Eclipse. |
| 5 | + |
| 6 | +import java.util.regex.Pattern; |
| 7 | +import java.util.regex.Matcher; |
| 8 | + |
| 9 | +// This class encapsulates a IEEE MAC Address. |
| 10 | +// |
| 11 | +// Currently this class will construct an object from a string having any one |
| 12 | +// of these formats: |
| 13 | +// |
| 14 | +// 00:11:22:33:44:55 (octets separated by colons) |
| 15 | +// aa:bb:cc:dd:ee:ff (lowercase hex digits) |
| 16 | +// AA:BB:CC:dd:ee:ff (uppercase hex digits) |
| 17 | +// 00-aa-22-CC-33:DD (digits separated by dashes) |
| 18 | +// 0:0a:1:2:34:56 (missing leading digits assumed to be "0) |
| 19 | +// |
| 20 | +// If a client of this class wishes to verify that the format of |
| 21 | +// a string is a valid MAC address, then they can invoke |
| 22 | +// MacAddress.isValidString(String). |
| 23 | + |
| 24 | + |
| 25 | +// Design note: I wrote this class in this way because in the past I have |
| 26 | +// worked with a system that dealt with a lot of bad MAC addresses |
| 27 | +// and there were lots of places in the code where either the code |
| 28 | +// either got into a bad state because of lack of input validation (this |
| 29 | +// both drove me nuts and cost a lot of time any money...)...or |
| 30 | +// else the code became really ugly because there were calls to |
| 31 | +// LegacyMacAddressClass.isValid() everywhere....even in code deeply |
| 32 | +// embedded in the system. It is my belief that if a MacAddress object |
| 33 | +// exists in a running system, that the object should be valid. |
| 34 | + |
| 35 | +public class MacAddress { |
| 36 | + |
| 37 | + // This array holds the raw bytes |
| 38 | + short[] address = new short[6]; // no unsigned ints in Java.... |
| 39 | + |
| 40 | + // This array holds what the author has arbitrarily decided is a good String |
| 41 | + // representation of this object. The string will look like this: |
| 42 | + // |
| 43 | + // 00:0a:0b:0c:0d:0e |
| 44 | + String string; |
| 45 | + |
| 46 | + /////////////////////////////////////////////////////////////////////////// |
| 47 | + |
| 48 | + static final Pattern pattern1; |
| 49 | + |
| 50 | + static { |
| 51 | + pattern1 = Pattern.compile("^\\s*" + |
| 52 | + "(\\p{XDigit}){1,2}[-:]" + // 1 |
| 53 | + "(\\p{XDigit}){1,2}[-:]" + // 2 |
| 54 | + "(\\p{XDigit}){1,2}[-:]" + // 3 |
| 55 | + "(\\p{XDigit}){1,2}[-:]" + // 4 |
| 56 | + "(\\p{XDigit}){1,2}[-:]" + // 5 |
| 57 | + "(\\p{XDigit}){1,2}" + // 6 |
| 58 | + "\\s*$"); |
| 59 | + } |
| 60 | + |
| 61 | + /////////////////////////////////////////////////////////////////////////// |
| 62 | + public static boolean isValidString(String s) |
| 63 | + { |
| 64 | + Matcher matcher = pattern1.matcher(s); |
| 65 | + |
| 66 | + return matcher.matches(); |
| 67 | + } |
| 68 | + |
| 69 | + /////////////////////////////////////////////////////////////////////////// |
| 70 | + private class MacMatchResult { |
| 71 | + boolean valid = false; |
| 72 | + short[] address = null; |
| 73 | + } |
| 74 | + |
| 75 | + /////////////////////////////////////////////////////////////////////////// |
| 76 | + private static boolean isValidString(String s, MacMatchResult mmr) { |
| 77 | + Matcher matcher = pattern1.matcher(s); |
| 78 | + |
| 79 | + if (! matcher.matches()) { |
| 80 | + mmr.valid= false; |
| 81 | + } |
| 82 | + else { |
| 83 | + mmr.valid = true; |
| 84 | + mmr.address = new short[6]; |
| 85 | + mmr.address[0] = Short.parseShort(matcher.group(1), 16); |
| 86 | + mmr.address[1] = Short.parseShort(matcher.group(2), 16); |
| 87 | + mmr.address[2] = Short.parseShort(matcher.group(3), 16); |
| 88 | + mmr.address[3] = Short.parseShort(matcher.group(4), 16); |
| 89 | + mmr.address[4] = Short.parseShort(matcher.group(5), 16); |
| 90 | + mmr.address[5] = Short.parseShort(matcher.group(6), 16); |
| 91 | + } |
| 92 | + |
| 93 | + return mmr.valid; |
| 94 | + } |
| 95 | + |
| 96 | + /////////////////////////////////////////////////////////////////////////// |
| 97 | + /** |
| 98 | + * @param s The human-readable MAC Address string to create this object from. |
| 99 | + */ |
| 100 | + public MacAddress(String s) |
| 101 | + { |
| 102 | + MacMatchResult mmr = new MacMatchResult(); |
| 103 | + |
| 104 | + if (! isValidString(s, mmr)) { |
| 105 | + throw new IllegalArgumentException("Malformed MAC Address: " + s); |
| 106 | + } |
| 107 | + |
| 108 | + System.arraycopy(mmr.address, 0, this.address, 0, 6); |
| 109 | + this.string = String.format("%02x:%02x:%02x:%02x:%02x:%02x", |
| 110 | + this.address[0], |
| 111 | + this.address[1], |
| 112 | + this.address[2], |
| 113 | + this.address[3], |
| 114 | + this.address[4], |
| 115 | + this.address[5]); |
| 116 | + } |
| 117 | + |
| 118 | + /////////////////////////////////////////////////////////////////////////// |
| 119 | + public String toString() |
| 120 | + { |
| 121 | + return this.string; |
| 122 | + } |
| 123 | + |
| 124 | + /////////////////////////////////////////////////////////////////////////// |
| 125 | + @Override public boolean equals(Object o) |
| 126 | + { |
| 127 | + if (! (o instanceof MacAddress)) { |
| 128 | + return false; |
| 129 | + } |
| 130 | + |
| 131 | + MacAddress that = (MacAddress)o; |
| 132 | + |
| 133 | + return this.address[0] == that.address[0] && |
| 134 | + this.address[1] == that.address[1] && |
| 135 | + this.address[2] == that.address[2] && |
| 136 | + this.address[3] == that.address[3] && |
| 137 | + this.address[4] == that.address[4] && |
| 138 | + this.address[5] == that.address[5]; |
| 139 | + } |
| 140 | + |
| 141 | + /////////////////////////////////////////////////////////////////////////// |
| 142 | + public int hashCode() |
| 143 | + { |
| 144 | + return string.hashCode(); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments