-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduced AbstractIpRange to share common logic between IPv4Range an…
…d IPv6Range
- Loading branch information
Showing
4 changed files
with
103 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import * as bigInt from "big-integer"; | ||
import {Prefix} from "./Prefix"; | ||
import {IPv6} from "./IPv6"; | ||
import {IPv6Range} from "./IPv6Range"; | ||
import {IPv4Range} from "./IPv4Range"; | ||
import {IPv4} from "./IPv4"; | ||
|
||
/** | ||
* Provides the implementation of functionality that are common to {@link IPRange}s | ||
*/ | ||
|
||
export abstract class AbstractIpRange { | ||
|
||
abstract readonly bitValue: bigInt.BigInteger; | ||
abstract readonly cidrPrefix: Prefix; | ||
abstract getFirst(): IPv6 | IPv4 | ||
abstract getLast(): IPv6 | IPv4 | ||
|
||
public getSize(): bigInt.BigInteger { | ||
/** | ||
* Using bitwise shit operation this will be | ||
* 1 << (this.bitValue - this.prefix.getValue()) | ||
* Since left shift a number by x is equivalent to multiplying the number by the power x raised to 2 | ||
* 2 << 4 = 2 * (2 raised to 4) | ||
*/ | ||
return bigInt(2).pow(this.bitValue.minus(bigInt(this.cidrPrefix.getValue()))); | ||
} | ||
|
||
public inside(otherRange: IPv6Range | IPv4Range): boolean { | ||
let thisFirst: IPv6 | IPv4 = this.getFirst(); | ||
let thisLast: IPv6 | IPv4 = this.getLast(); | ||
let otherFirst: IPv6 | IPv4 = otherRange.getFirst(); | ||
let otherLast: IPv6 | IPv4 = otherRange.getLast(); | ||
|
||
return (otherFirst.isLessThanOrEquals(thisFirst) && otherLast.isGreaterThanOrEquals(thisLast)); | ||
} | ||
|
||
public contains(otherRange: IPv6Range | IPv4Range): boolean { | ||
let thisFirst: IPv6 | IPv4 = this.getFirst(); | ||
let thisLast: IPv6 | IPv4 = this.getLast(); | ||
let otherFirst: IPv6 | IPv4 = otherRange.getFirst(); | ||
let otherLast: IPv6 | IPv4 = otherRange.getLast(); | ||
|
||
return (thisFirst.isLessThanOrEquals(otherFirst) && thisLast.isGreaterThanOrEquals(otherLast)); | ||
} | ||
|
||
public isOverlapping(otherRange: IPv6Range | IPv4Range): boolean { | ||
let thisFirst: IPv6 | IPv4 = this.getFirst(); | ||
let thisLast: IPv6 | IPv4 = this.getLast(); | ||
let otherFirst: IPv6 | IPv4 = otherRange.getFirst(); | ||
let otherLast: IPv6 | IPv4 = otherRange.getLast(); | ||
|
||
return ( | ||
thisLast.isGreaterThan(otherFirst) && thisLast.isLessThanOrEquals(otherLast) && thisFirst.isLessThan(otherFirst) | ||
|| | ||
otherLast.isGreaterThan(thisFirst) && otherLast.isLessThanOrEquals(thisLast) && otherFirst.isLessThan(otherFirst) | ||
); | ||
} | ||
|
||
public isConsecutive(otherRange: IPv6Range | IPv4Range): boolean { | ||
let thisFirst: IPv6 | IPv4 = this.getFirst(); | ||
let thisLast: IPv6 | IPv4 = this.getLast(); | ||
let otherFirst: IPv6 | IPv4 = otherRange.getFirst(); | ||
let otherLast: IPv6 | IPv4 = otherRange.getLast(); | ||
|
||
return ( | ||
thisLast.hasNext() && thisLast.nextIPNumber().isEquals(otherFirst) | ||
|| | ||
otherLast.hasNext() && otherLast.nextIPNumber().isEquals(thisFirst) | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters