Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.openflow.types; |
| 2 | |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 3 | |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame^] | 4 | public class OFPortMap extends Masked<OFPortBitmap> { |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 5 | |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame^] | 6 | private OFPortMap(OFPortBitmap mask) { |
| 7 | super(OFPortBitmap.NONE, mask); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 8 | } |
| 9 | |
| 10 | public boolean isOn(OFPort port) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame^] | 11 | return this.mask.isOn(port); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 12 | } |
| 13 | |
| 14 | public static OFPortMap ofPorts(OFPort... ports) { |
| 15 | Builder builder = new Builder(); |
| 16 | for (OFPort port: ports) { |
| 17 | builder.set(port); |
| 18 | } |
| 19 | return builder.build(); |
| 20 | } |
| 21 | |
| 22 | @Override |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame^] | 23 | public boolean equals(Object obj) { |
| 24 | if (!(obj instanceof OFPortMap)) |
| 25 | return false; |
| 26 | OFPortMap other = (OFPortMap)obj; |
| 27 | return (other.value.equals(this.value) && other.mask.equals(this.mask)); |
| 28 | } |
| 29 | |
| 30 | @Override |
| 31 | public int hashCode() { |
| 32 | return 619 * mask.hashCode() + 257 * value.hashCode(); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | public static class Builder { |
| 36 | private long raw1, raw2; |
| 37 | |
| 38 | public Builder() { |
| 39 | |
| 40 | } |
| 41 | |
| 42 | public boolean isOn(OFPort port) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame^] | 43 | return OFPortBitmap.isBitOn(raw1, raw2, port.getPortNumber()); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | public Builder set(OFPort port) { |
| 47 | int bit = port.getPortNumber(); |
| 48 | if (bit < 0 || bit >= 128) |
| 49 | throw new IndexOutOfBoundsException("Port number is out of bounds"); |
| 50 | if (bit < 64) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame^] | 51 | raw2 |= ((long)1 << bit); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 52 | } else { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame^] | 53 | raw1 |= ((long)1 << (bit - 64)); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 54 | } |
| 55 | return this; |
| 56 | } |
| 57 | |
| 58 | public Builder unset(OFPort port) { |
| 59 | int bit = port.getPortNumber(); |
| 60 | if (bit < 0 || bit >= 128) |
| 61 | throw new IndexOutOfBoundsException("Port number is out of bounds"); |
| 62 | if (bit < 64) { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame^] | 63 | raw2 &= ~((long)1 << bit); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 64 | } else { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame^] | 65 | raw1 &= ~((long)1 << (bit - 64)); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 66 | } |
| 67 | return this; |
| 68 | } |
| 69 | |
| 70 | public OFPortMap build() { |
Yotam Harchol | 595c644 | 2013-09-27 16:29:08 -0700 | [diff] [blame^] | 71 | return new OFPortMap(OFPortBitmap.of(raw1, raw2)); |
Yotam Harchol | a11f38b | 2013-09-26 15:38:17 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | } |