Thomas Vachuska | 24c849c | 2014-10-27 09:53:05 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
Pavlin Radoslavov | 9de2772 | 2014-10-23 20:31:15 -0700 | [diff] [blame] | 19 | package org.onlab.packet; |
| 20 | |
| 21 | import java.util.Objects; |
| 22 | |
| 23 | /** |
| 24 | * The class representing an IPv4 network address. |
| 25 | * This class is immutable. |
| 26 | */ |
| 27 | public final class Ip4Prefix { |
| 28 | private final Ip4Address address; // The IPv4 address |
| 29 | private final short prefixLen; // The prefix length |
| 30 | |
| 31 | /** |
| 32 | * Default constructor. |
| 33 | */ |
| 34 | public Ip4Prefix() { |
| 35 | this.address = new Ip4Address(); |
| 36 | this.prefixLen = 0; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Copy constructor. |
| 41 | * |
| 42 | * @param other the object to copy from |
| 43 | */ |
| 44 | public Ip4Prefix(Ip4Prefix other) { |
| 45 | this.address = new Ip4Address(other.address); |
| 46 | this.prefixLen = other.prefixLen; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Constructor for a given address and prefix length. |
| 51 | * |
| 52 | * @param address the address to use |
| 53 | * @param prefixLen the prefix length to use |
| 54 | */ |
| 55 | public Ip4Prefix(Ip4Address address, short prefixLen) { |
| 56 | this.address = Ip4Address.makeMaskedAddress(address, prefixLen); |
| 57 | this.prefixLen = prefixLen; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Constructs an IPv4 prefix from a string representation of the |
| 62 | * prefix. |
| 63 | *<p> |
| 64 | * Example: "1.2.0.0/16" |
| 65 | * |
| 66 | * @param value the value to use |
| 67 | */ |
| 68 | public Ip4Prefix(String value) { |
| 69 | String[] splits = value.split("/"); |
| 70 | if (splits.length != 2) { |
| 71 | throw new IllegalArgumentException("Specified IPv4 prefix must contain an IPv4 " + |
| 72 | "address and a prefix length separated by '/'"); |
| 73 | } |
| 74 | this.prefixLen = Short.decode(splits[1]); |
| 75 | this.address = Ip4Address.makeMaskedAddress(new Ip4Address(splits[0]), |
| 76 | this.prefixLen); |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Gets the address value of the IPv4 prefix. |
| 81 | * |
| 82 | * @return the address value of the IPv4 prefix |
| 83 | */ |
| 84 | public Ip4Address getAddress() { |
| 85 | return address; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Gets the prefix length value of the IPv4 prefix. |
| 90 | * |
| 91 | * @return the prefix length value of the IPv4 prefix |
| 92 | */ |
| 93 | public short getPrefixLen() { |
| 94 | return prefixLen; |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Converts the IPv4 prefix value to an "address/prefixLen" string. |
| 99 | * |
| 100 | * @return the IPv4 prefix value as an "address/prefixLen" string |
| 101 | */ |
| 102 | @Override |
| 103 | public String toString() { |
| 104 | return this.address.toString() + "/" + this.prefixLen; |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Compares the value of two Ip4Prefix objects. |
| 109 | * <p/> |
| 110 | * Note the value of the IPv4 address is compared directly between the |
| 111 | * objects, and must match exactly for the objects to be considered equal. |
| 112 | * This may result in objects which represent the same IP prefix being |
| 113 | * classified as unequal, because the unsignificant bits of the address |
| 114 | * field don't match (the bits to the right of the prefix length). |
| 115 | * <p/> |
| 116 | * TODO Change this behavior so that objects that represent the same prefix |
| 117 | * are classified as equal according to this equals method. |
| 118 | * |
| 119 | * @see Object#equals(Object) |
| 120 | */ |
| 121 | @Override |
| 122 | public boolean equals(Object other) { |
| 123 | if (other == this) { |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | if (!(other instanceof Ip4Prefix)) { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | Ip4Prefix otherIp4Prefix = (Ip4Prefix) other; |
| 132 | |
| 133 | return Objects.equals(this.address, otherIp4Prefix.address) |
| 134 | && this.prefixLen == otherIp4Prefix.prefixLen; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public int hashCode() { |
| 139 | return Objects.hash(address, prefixLen); |
| 140 | } |
| 141 | } |