View Javadoc
1   package org.kuali.common.jute.net;
2   
3   import static com.google.common.base.Optional.absent;
4   import static org.kuali.common.jute.base.Precondition.checkMin;
5   import static org.kuali.common.jute.reflect.Reflection.checkNoNulls;
6   
7   import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
8   import com.google.common.base.Optional;
9   
10  @JsonDeserialize(builder = InterfaceAddress.Builder.class)
11  public final class InterfaceAddress {
12  
13      private final InetAddress address;
14      private final Optional<InetAddress> broadcast;
15      private final int networkPrefixLength;
16  
17      private InterfaceAddress(Builder builder) {
18          this.address = builder.address;
19          this.broadcast = builder.broadcast;
20          this.networkPrefixLength = builder.networkPrefixLength;
21      }
22  
23      public static InterfaceAddress copyOf(java.net.InterfaceAddress mutable) {
24          Builder builder = builder();
25          builder.withAddress(InetAddress.copyOf(mutable.getAddress()));
26          if (mutable.getBroadcast() != null) {
27              InetAddress broadcast = InetAddress.copyOf(mutable.getBroadcast());
28              builder.withBroadcast(Optional.of(broadcast));
29          }
30          builder.withNetworkPrefixLength(mutable.getNetworkPrefixLength());
31          return builder.build();
32      }
33  
34      public static Builder builder() {
35          return new Builder();
36      }
37  
38      public static class Builder implements org.apache.commons.lang3.builder.Builder<InterfaceAddress> {
39  
40          private InetAddress address;
41          private Optional<InetAddress> broadcast = absent();
42          private int networkPrefixLength = -1;
43  
44          public Builder withNetworkPrefixLength(int networkPrefixLength) {
45              this.networkPrefixLength = networkPrefixLength;
46              return this;
47          }
48  
49          public Builder withAddress(InetAddress address) {
50              this.address = address;
51              return this;
52          }
53  
54          public Builder withBroadcast(Optional<InetAddress> broadcast) {
55              this.broadcast = broadcast;
56              return this;
57          }
58  
59          @Override
60          public InterfaceAddress build() {
61              return validate(new InterfaceAddress(this));
62          }
63  
64          private static InterfaceAddress validate(InterfaceAddress instance) {
65              checkNoNulls(instance);
66              checkMin(instance.networkPrefixLength, 0, "networkPrefixLength");
67              return instance;
68          }
69      }
70  
71      public InetAddress getAddress() {
72          return address;
73      }
74  
75      public Optional<InetAddress> getBroadcast() {
76          return broadcast;
77      }
78  
79      public int getNetworkPrefixLength() {
80          return networkPrefixLength;
81      }
82  
83  }