LCOV - code coverage report
Current view: top level - lib/util - ipv4_address.dart (source / functions) Hit Total Coverage
Test: Folly Fields Lines: 49 49 100.0 %
Date: 2023-05-12 13:54:58 Functions: 0 0 -

          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             :   String toString({
      66             :     String separator = '.',
      67             :   }) =>
      68           6 :       <int>[dot1, dot2, dot3, dot4].join(separator);
      69             : 
      70             :   ///
      71             :   ///
      72             :   ///
      73             :   // ignore: prefer_constructors_over_static_methods
      74           1 :   static Ipv4Address fromString(
      75             :     String? value, {
      76             :     String separator = '.',
      77             :   }) {
      78           1 :     if (value == null || value.isEmpty) {
      79           1 :       throw ArgumentError('invalidIpAddress');
      80             :     }
      81             : 
      82           1 :     List<String> parts = value.split(separator);
      83             : 
      84           2 :     if (parts.length != 4) {
      85           1 :       throw ArgumentError('invalidIpAddress');
      86             :     }
      87             : 
      88           1 :     List<int> ocs = <int>[];
      89             : 
      90           2 :     for (final String part in parts) {
      91           1 :       int? octet = int.tryParse(part);
      92           2 :       if (octet == null || octet < 0 || octet > 255) {
      93           1 :         throw ArgumentError('invalidIpAddress');
      94             :       }
      95           1 :       ocs.add(octet);
      96             :     }
      97             : 
      98           5 :     return Ipv4Address.fromDecimals(ocs[0], ocs[1], ocs[2], ocs[3]);
      99             :   }
     100             : 
     101             :   ///
     102             :   ///
     103             :   ///
     104           1 :   static Ipv4Address fromList(
     105             :     List<dynamic> list, {
     106             :     String separator = '.',
     107             :   }) {
     108           2 :     if (list.length != 4) {
     109           1 :       throw ArgumentError('invalidIpAddress');
     110             :     }
     111             : 
     112           2 :     return fromString(list.join(separator), separator: separator);
     113             :   }
     114             : 
     115             :   ///
     116             :   ///
     117             :   ///
     118           1 :   @override
     119           1 :   int get hashCode => _ip;
     120             : 
     121             :   ///
     122             :   ///
     123             :   ///
     124           1 :   @override
     125             :   bool operator ==(Object other) =>
     126           4 :       other is Ipv4Address && _ip == other.integer;
     127             : 
     128             :   ///
     129             :   ///
     130             :   ///
     131           1 :   Ipv4Address operator +(int value) {
     132           2 :     int newValue = _ip + value;
     133           1 :     if (newValue > 4294967295) {
     134           1 :       throw ArgumentError('invalidIpAddress');
     135             :     }
     136             : 
     137           1 :     return Ipv4Address(newValue);
     138             :   }
     139             : 
     140             :   ///
     141             :   ///
     142             :   ///
     143           1 :   Ipv4Address operator -(int value) {
     144           2 :     int newValue = _ip - value;
     145           1 :     if (newValue < 0) {
     146           1 :       throw ArgumentError('invalidIpAddress');
     147             :     }
     148             : 
     149           1 :     return Ipv4Address(newValue);
     150             :   }
     151             : 
     152             :   ///
     153             :   ///
     154             :   ///
     155           4 :   bool operator >(Ipv4Address value) => _ip > value.integer;
     156             : 
     157             :   ///
     158             :   ///
     159             :   ///
     160           4 :   bool operator <(Ipv4Address value) => _ip < value.integer;
     161             : 
     162             :   ///
     163             :   ///
     164             :   ///
     165           4 :   bool operator >=(Ipv4Address value) => _ip >= value.integer;
     166             : 
     167             :   ///
     168             :   ///
     169             :   ///
     170           4 :   bool operator <=(Ipv4Address value) => _ip <= value.integer;
     171             : }

Generated by: LCOV version 1.14