Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 1 | /* |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 2 | * Copyright 2014 Open Networking Laboratory |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 3 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 7 | * |
Thomas Vachuska | 4f1a60c | 2014-10-28 13:39:07 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame] | 15 | */ |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 16 | package org.onlab.packet; |
| 17 | |
| 18 | import java.util.Objects; |
| 19 | |
| 20 | /** |
| 21 | * The class representing an IPv4 network address. |
| 22 | * This class is immutable. |
| 23 | */ |
| 24 | public final class Ip4Prefix { |
| 25 | private final Ip4Address address; // The IPv4 address |
| 26 | private final short prefixLen; // The prefix length |
| 27 | |
| 28 | /** |
| 29 | * Default constructor. |
| 30 | */ |
| 31 | public Ip4Prefix() { |
| 32 | this.address = new Ip4Address(); |
| 33 | this.prefixLen = 0; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Copy constructor. |
| 38 | * |
| 39 | * @param other the object to copy from |
| 40 | */ |
| 41 | public Ip4Prefix(Ip4Prefix other) { |
| 42 | this.address = new Ip4Address(other.address); |
| 43 | this.prefixLen = other.prefixLen; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Constructor for a given address and prefix length. |
| 48 | * |
| 49 | * @param address the address to use |
| 50 | * @param prefixLen the prefix length to use |
| 51 | */ |
| 52 | public Ip4Prefix(Ip4Address address, short prefixLen) { |
| 53 | this.address = Ip4Address.makeMaskedAddress(address, prefixLen); |
| 54 | this.prefixLen = prefixLen; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Constructs an IPv4 prefix from a string representation of the |
| 59 | * prefix. |
| 60 | *<p> |
| 61 | * Example: "1.2.0.0/16" |
| 62 | * |
| 63 | * @param value the value to use |
| 64 | */ |
| 65 | public Ip4Prefix(String value) { |
| 66 | String[] splits = value.split("/"); |
| 67 | if (splits.length != 2) { |
| 68 | throw new IllegalArgumentException("Specified IPv4 prefix must contain an IPv4 " + |
| 69 | "address and a prefix length separated by '/'"); |
| 70 | } |
| 71 | this.prefixLen = Short.decode(splits[1]); |
| 72 | this.address = Ip4Address.makeMaskedAddress(new Ip4Address(splits[0]), |
| 73 | this.prefixLen); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Gets the address value of the IPv4 prefix. |
| 78 | * |
| 79 | * @return the address value of the IPv4 prefix |
| 80 | */ |
| 81 | public Ip4Address getAddress() { |
| 82 | return address; |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Gets the prefix length value of the IPv4 prefix. |
| 87 | * |
| 88 | * @return the prefix length value of the IPv4 prefix |
| 89 | */ |
| 90 | public short getPrefixLen() { |
| 91 | return prefixLen; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Converts the IPv4 prefix value to an "address/prefixLen" string. |
| 96 | * |
| 97 | * @return the IPv4 prefix value as an "address/prefixLen" string |
| 98 | */ |
| 99 | @Override |
| 100 | public String toString() { |
| 101 | return this.address.toString() + "/" + this.prefixLen; |
| 102 | } |
| 103 | |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 104 | @Override |
| 105 | public boolean equals(Object other) { |
| 106 | if (other == this) { |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | if (!(other instanceof Ip4Prefix)) { |
| 111 | return false; |
| 112 | } |
| 113 | |
| 114 | Ip4Prefix otherIp4Prefix = (Ip4Prefix) other; |
| 115 | |
| 116 | return Objects.equals(this.address, otherIp4Prefix.address) |
| 117 | && this.prefixLen == otherIp4Prefix.prefixLen; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public int hashCode() { |
| 122 | return Objects.hash(address, prefixLen); |
| 123 | } |
| 124 | } |