Advertisement
MrModest

IpRange

Aug 31st, 2019
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.62 KB | None | 0 0
  1. data class Ip(val node1: Int, val node2: Int, val node3: Int, val node4: Int) : Comparable<Ip> {
  2.     constructor(ipStr: String) {
  3.         val nodesStr = ipStr.split('.')
  4.  
  5.         if (nodesStr.count() != 4)
  6.             throw InvalidOperationException("Incorrect ip string format")
  7.            
  8.         val nodes = nodesStr.map { it.toIntOrNull() }.filter { it != null }
  9.        
  10.         if (nodes.count() != 4)
  11.             throw InvalidOperationException("Some nodes have incorrect format")
  12.            
  13.         if (!nodes.all { it >= 0 && it <= 255 })
  14.             throw InvalidOperationException("Some nodes great than 255 or less than 0")
  15.            
  16.         node1 = nodes[0]
  17.         node2 = nodes[1]
  18.         node3 = nodes[2]
  19.         node4 = nodes[3]
  20.     }
  21.    
  22.     override operator fun compareTo(other: Ip): Int {
  23.         var compare = this.node1 - other.node1
  24.         if (compare != 0) return compare.sign()
  25.            
  26.         compare = this.node2 - other.node2
  27.         if (compare != 0) return compare.sign()
  28.                
  29.         compare = this.node3 - other.node3
  30.         if (compare != 0) return compare.sign()
  31.                
  32.         compare = this.node3 - other.node3
  33.         return compare.sign()
  34.     }
  35.    
  36.     override operator fun inc(){
  37.         if (node4 < 255)
  38.             return Ip(node1, node2, node3, node4 + 1)
  39.            
  40.         if (node3 < 255)
  41.             return Ip(node1, node2, node3 + 1, node4)
  42.            
  43.         if (node2 < 255)
  44.             return Ip(node1, node2 + 1, node3, node4)
  45.            
  46.         if (node1 < 255)
  47.             return Ip(node1 + 1, node2, node3, node4)
  48.            
  49.         throw OutOfRangeException("You try increment the max value of Ip (${this})")
  50.     }
  51.    
  52.     override fun toString(): String {
  53.         return "${node1}.${node2}.${node3}.${node4}"
  54.     }
  55. }
  56.  
  57. fun getIpsByRange(start: Ip, end: Ip): ArrayList<Ip> {
  58.     if (start > end) return emptyList()
  59.        
  60.     if (start == end) return listOf(start)
  61.        
  62.     if (end == ++start) return listOf(start, end)
  63.        
  64.     val resultList = listOf(start)
  65.    
  66.     var current = start
  67.    
  68.     while (current < end) {
  69.         resultList.add(++current)
  70.     }
  71.    
  72.     resultList.add(end)
  73.    
  74.     return resultList
  75. }
  76.  
  77. fun main(args: Array<String>) {
  78.     if (args.count() != 2) {
  79.         println("Invalid arguments: you need get the two ip for range")
  80.         return
  81.     }
  82.    
  83.     try {
  84.         getIpsByRange(args[0], args[1]).forEach {
  85.             println(it)
  86.         }
  87.     }
  88.     catch(e: InvalidOperationException | OutOfRangeException) {
  89.         println(e.message)
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement