|
| 1 | +namespace Tynamix.ObjectFiller |
| 2 | +{ |
| 3 | + /// <summary> |
| 4 | + /// Generates an IP address |
| 5 | + /// </summary> |
| 6 | + public class IpAddress : IRandomizerPlugin<string> |
| 7 | + { |
| 8 | + /// <summary> |
| 9 | + /// The maximum of the first IP part (max 255) |
| 10 | + /// </summary> |
| 11 | + private int firstIpPartMax; |
| 12 | + |
| 13 | + /// <summary> |
| 14 | + /// The maximum of the second IP part (max 255) |
| 15 | + /// </summary> |
| 16 | + private int secondIpPartMax; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// The maximum of the third IP part (max 255) |
| 20 | + /// </summary> |
| 21 | + private int thirdIpPartMax; |
| 22 | + |
| 23 | + /// <summary> |
| 24 | + /// The maximum of the last IP part (max 255) |
| 25 | + /// </summary> |
| 26 | + private int lastIpPartMax; |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Initializes a new instance of the <see cref="IpAddress"/> class. |
| 30 | + /// </summary> |
| 31 | + public IpAddress() |
| 32 | + : this(255, 255, 255, 255) |
| 33 | + { |
| 34 | + } |
| 35 | + |
| 36 | + /// <summary> |
| 37 | + /// Initializes a new instance of the <see cref="IpAddress"/> class. |
| 38 | + /// </summary> |
| 39 | + /// <param name="firstIpPartMax"> |
| 40 | + /// The maximum of the first IP part (max 255) |
| 41 | + /// </param> |
| 42 | + public IpAddress(uint firstIpPartMax) |
| 43 | + : this(firstIpPartMax, 255, 255, 255) |
| 44 | + { |
| 45 | + } |
| 46 | + |
| 47 | + /// <summary> |
| 48 | + /// Initializes a new instance of the <see cref="IpAddress"/> class. |
| 49 | + /// </summary> |
| 50 | + /// <param name="firstIpPartMax"> |
| 51 | + /// The maximum of the first IP part (max 255) |
| 52 | + /// </param> |
| 53 | + /// <param name="secondIpPartMax"> |
| 54 | + /// The maximum of the second IP part (max 255) |
| 55 | + /// </param> |
| 56 | + public IpAddress( |
| 57 | + uint firstIpPartMax, |
| 58 | + uint secondIpPartMax) |
| 59 | + : this(firstIpPartMax, secondIpPartMax, 255, 255) |
| 60 | + |
| 61 | + { |
| 62 | + } |
| 63 | + |
| 64 | + /// <summary> |
| 65 | + /// Initializes a new instance of the <see cref="IpAddress"/> class. |
| 66 | + /// </summary> |
| 67 | + /// <param name="firstIpPartMax"> |
| 68 | + /// The maximum of the first IP part (max 255) |
| 69 | + /// </param> |
| 70 | + /// <param name="secondIpPartMax"> |
| 71 | + /// The maximum of the second IP part (max 255) |
| 72 | + /// </param> |
| 73 | + /// <param name="thirdIpPartMax"> |
| 74 | + /// The maximum of the third IP part (max 255) |
| 75 | + /// </param> |
| 76 | + public IpAddress( |
| 77 | + uint firstIpPartMax, |
| 78 | + uint secondIpPartMax, |
| 79 | + uint thirdIpPartMax) |
| 80 | + : this(firstIpPartMax, secondIpPartMax, thirdIpPartMax, 255) |
| 81 | + { |
| 82 | + } |
| 83 | + |
| 84 | + /// <summary> |
| 85 | + /// Initializes a new instance of the <see cref="IpAddress"/> class. |
| 86 | + /// </summary> |
| 87 | + /// <param name="firstIpPartMax"> |
| 88 | + /// The maximum of the first IP part (max 255) |
| 89 | + /// </param> |
| 90 | + /// <param name="secondIpPartMax"> |
| 91 | + /// The maximum of the second IP part (max 255) |
| 92 | + /// </param> |
| 93 | + /// <param name="thirdIpPartMax"> |
| 94 | + /// The maximum of the third IP part (max 255) |
| 95 | + /// </param> |
| 96 | + /// <param name="lastIpPartMax"> |
| 97 | + /// The maximum of the last IP part (max 255) |
| 98 | + /// </param> |
| 99 | + public IpAddress( |
| 100 | + uint firstIpPartMax, |
| 101 | + uint secondIpPartMax, |
| 102 | + uint thirdIpPartMax, |
| 103 | + uint lastIpPartMax) |
| 104 | + { |
| 105 | + this.firstIpPartMax = (int)(firstIpPartMax > 255 ? 255 : firstIpPartMax); |
| 106 | + this.secondIpPartMax = (int)(secondIpPartMax > 255 ? 255 : secondIpPartMax); |
| 107 | + this.thirdIpPartMax = (int)(thirdIpPartMax > 255 ? 255 : thirdIpPartMax); |
| 108 | + this.lastIpPartMax = (int)(lastIpPartMax > 255 ? 255 : lastIpPartMax); |
| 109 | + } |
| 110 | + |
| 111 | + /// <summary> |
| 112 | + /// Gets random IP address |
| 113 | + /// </summary> |
| 114 | + /// <returns>An random IP address</returns> |
| 115 | + public string GetValue() |
| 116 | + { |
| 117 | + int firstSegment = Randomizer<int>.Create(new IntRange(1, this.firstIpPartMax)); |
| 118 | + int secondSegment = Randomizer<int>.Create(new IntRange(1, this.secondIpPartMax)); |
| 119 | + int thirdSegment = Randomizer<int>.Create(new IntRange(1, this.thirdIpPartMax)); |
| 120 | + int lastSegment = Randomizer<int>.Create(new IntRange(1, this.lastIpPartMax)); |
| 121 | + |
| 122 | + return string.Format("{0}.{1}.{2}.{3}", firstSegment, secondSegment, thirdSegment, lastSegment); |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments