Line data Source code
1 : // ignore_for_file: prefer-first 2 : 3 : /// 4 : /// 5 : /// 6 : class Ipv4Address { 7 : final int _ip; 8 : 9 : /// 10 : /// 11 : /// 12 1 : Ipv4Address(int ip) : _ip = ip; 13 : 14 : /// 15 : /// 16 : /// 17 1 : Ipv4Address.fromDecimals(int oc1, int oc2, int oc3, int oc4) 18 : : assert( 19 3 : oc1 >= 0 && oc1 <= 255, 20 : 'First octet must be between 0 and 255', 21 : ), 22 : assert( 23 3 : oc2 >= 0 && oc2 <= 255, 24 : 'Second octet must be between 0 and 255', 25 : ), 26 : assert( 27 3 : oc3 >= 0 && oc3 <= 255, 28 : 'Third octet must be between 0 and 255', 29 : ), 30 : assert( 31 3 : oc4 >= 0 && oc4 <= 255, 32 : 'Fourth octet must be between 0 and 255', 33 : ), 34 6 : _ip = (oc1 << 24) + (oc2 << 16) + (oc3 << 8) + oc4; 35 : 36 : /// 37 : /// 38 : /// 39 4 : int get dot1 => _ip >> 24 & 0xFF; 40 : 41 : /// 42 : /// 43 : /// 44 4 : int get dot2 => (_ip >> 16) & 0xFF; 45 : 46 : /// 47 : /// 48 : /// 49 4 : int get dot3 => (_ip >> 8) & 0xFF; 50 : 51 : /// 52 : /// 53 : /// 54 3 : int get dot4 => _ip & 0xFF; 55 : 56 : /// 57 : /// 58 : /// 59 2 : int get integer => _ip; 60 : 61 : /// 62 : /// 63 : /// 64 1 : @override 65 5 : String toString() => '$dot1.$dot2.$dot3.$dot4'; 66 : 67 : /// 68 : /// 69 : /// 70 0 : String get dash => '$dot1-$dot2-$dot3-$dot4'; 71 : 72 : /// 73 : /// 74 : /// 75 : // ignore: prefer_constructors_over_static_methods 76 1 : static Ipv4Address fromString(String? value) { 77 1 : if (value == null || value.isEmpty) { 78 1 : throw ArgumentError('invalidIpAddress'); 79 : } 80 : 81 1 : List<String> parts = value.split('.'); 82 : 83 2 : if (parts.length != 4) { 84 1 : throw ArgumentError('invalidIpAddress'); 85 : } 86 : 87 1 : List<int> ocs = <int>[]; 88 : 89 2 : for (final String part in parts) { 90 1 : int? octet = int.tryParse(part); 91 2 : if (octet == null || octet < 0 || octet > 255) { 92 1 : throw ArgumentError('invalidIpAddress'); 93 : } 94 1 : ocs.add(octet); 95 : } 96 : 97 5 : return Ipv4Address.fromDecimals(ocs[0], ocs[1], ocs[2], ocs[3]); 98 : } 99 : 100 : /// 101 : /// 102 : /// 103 0 : static Ipv4Address fromList(List<dynamic> value) => 104 0 : fromString(value.join('.')); 105 : 106 : /// 107 : /// 108 : /// 109 0 : @override 110 0 : int get hashCode => _ip; 111 : 112 : /// 113 : /// 114 : /// 115 1 : @override 116 : bool operator ==(Object other) => 117 4 : other is Ipv4Address && _ip == other.integer; 118 : 119 : /// 120 : /// 121 : /// 122 1 : Ipv4Address operator +(int value) { 123 2 : int newValue = _ip + value; 124 1 : if (newValue > 4294967295) { 125 1 : throw ArgumentError('invalidIpAddress'); 126 : } 127 : 128 1 : return Ipv4Address(newValue); 129 : } 130 : 131 : /// 132 : /// 133 : /// 134 1 : Ipv4Address operator -(int value) { 135 2 : int newValue = _ip - value; 136 1 : if (newValue < 0) { 137 1 : throw ArgumentError('invalidIpAddress'); 138 : } 139 : 140 1 : return Ipv4Address(newValue); 141 : } 142 : 143 : /// 144 : /// 145 : /// 146 0 : bool operator >(Ipv4Address value) => _ip > value.integer; 147 : 148 : /// 149 : /// 150 : /// 151 0 : bool operator <(Ipv4Address value) => _ip < value.integer; 152 : 153 : /// 154 : /// 155 : /// 156 0 : bool operator >=(Ipv4Address value) => _ip >= value.integer; 157 : 158 : /// 159 : /// 160 : /// 161 0 : bool operator <=(Ipv4Address value) => _ip <= value.integer; 162 : }