diff options
author | Samuel Holland <samuel@sholland.org> | 2018-09-05 20:17:14 -0500 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2018-12-08 02:39:41 +0100 |
commit | daba6506567ffc5f88c3a2a3cf8c009b2a9a7a6d (patch) | |
tree | d95ad1ae84d02fc3e18a211aa1e1ef8150d8fa35 /app/src/main/java/com/wireguard/config/InetNetwork.java | |
parent | Auto-format the source directories (diff) | |
download | wireguard-android-daba6506567ffc5f88c3a2a3cf8c009b2a9a7a6d.tar.xz wireguard-android-daba6506567ffc5f88c3a2a3cf8c009b2a9a7a6d.zip |
Remodel the Model
- The configuration and crypto model is now entirely independent
of Android classes other than Nullable and TextUtils.
- Model classes are immutable and use builders that enforce the
appropriate optional/required attributes.
- The Android config proxies (for Parcelable and databinding) are
moved to the Android side of the codebase, and are designed to be
safe for two-way databinding. This allows proper observability in
TunnelDetailFragment.
- Various robustness fixes and documentation updates to helper classes.
Diffstat (limited to 'app/src/main/java/com/wireguard/config/InetNetwork.java')
-rw-r--r-- | app/src/main/java/com/wireguard/config/InetNetwork.java | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/app/src/main/java/com/wireguard/config/InetNetwork.java b/app/src/main/java/com/wireguard/config/InetNetwork.java index 836a1335..9e5e8c64 100644 --- a/app/src/main/java/com/wireguard/config/InetNetwork.java +++ b/app/src/main/java/com/wireguard/config/InetNetwork.java @@ -7,26 +7,36 @@ package com.wireguard.config; import java.net.Inet4Address; import java.net.InetAddress; -import java.util.Objects; -public class InetNetwork { +/** + * An Internet network, denoted by its address and netmask + * <p> + * Instances of this class are immutable. + */ +public final class InetNetwork { private final InetAddress address; private final int mask; - public InetNetwork(final String input) { - final int slash = input.lastIndexOf('/'); + private InetNetwork(final InetAddress address, final int mask) { + this.address = address; + this.mask = mask; + } + + public static InetNetwork parse(final String network) { + final int slash = network.lastIndexOf('/'); final int rawMask; final String rawAddress; if (slash >= 0) { - rawMask = Integer.parseInt(input.substring(slash + 1), 10); - rawAddress = input.substring(0, slash); + rawMask = Integer.parseInt(network.substring(slash + 1), 10); + rawAddress = network.substring(0, slash); } else { rawMask = -1; - rawAddress = input; + rawAddress = network; } - address = InetAddresses.parse(rawAddress); + final InetAddress address = InetAddresses.parse(rawAddress); final int maxMask = (address instanceof Inet4Address) ? 32 : 128; - mask = rawMask >= 0 && rawMask <= maxMask ? rawMask : maxMask; + final int mask = rawMask >= 0 && rawMask <= maxMask ? rawMask : maxMask; + return new InetNetwork(address, mask); } @Override @@ -34,7 +44,7 @@ public class InetNetwork { if (!(obj instanceof InetNetwork)) return false; final InetNetwork other = (InetNetwork) obj; - return Objects.equals(address, other.address) && mask == other.mask; + return address.equals(other.address) && mask == other.mask; } public InetAddress getAddress() { |