Skip to content

Commit

Permalink
HDDS-751. Replace usage of Guava Optional with Java Optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
linyiqun committed Nov 2, 2018
1 parent 8fe85af commit d16d5f7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
Expand Up @@ -18,7 +18,6 @@

package org.apache.hadoop.hdds;

import com.google.common.base.Optional;
import com.google.common.base.Strings;
import com.google.common.net.HostAndPort;
import org.apache.hadoop.classification.InterfaceAudience;
Expand All @@ -45,6 +44,7 @@
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
import java.util.TimeZone;

import static org.apache.hadoop.hdfs.DFSConfigKeys
Expand Down Expand Up @@ -114,7 +114,7 @@ public static InetSocketAddress getScmAddressForClients(Configuration conf) {
ScmConfigKeys.OZONE_SCM_CLIENT_ADDRESS_KEY);

return NetUtils.createSocketAddr(host.get() + ":" + port
.or(ScmConfigKeys.OZONE_SCM_CLIENT_PORT_DEFAULT));
.orElse(ScmConfigKeys.OZONE_SCM_CLIENT_PORT_DEFAULT));
}

/**
Expand Down Expand Up @@ -162,7 +162,7 @@ public static InetSocketAddress getScmAddressForBlockClients(
ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_ADDRESS_KEY);

return NetUtils.createSocketAddr(host.get() + ":" + port
.or(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_PORT_DEFAULT));
.orElse(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_PORT_DEFAULT));
}

/**
Expand All @@ -186,7 +186,7 @@ public static Optional<String> getHostNameFromConfigKeys(Configuration conf,
return hostName;
}
}
return Optional.absent();
return Optional.empty();
}

/**
Expand All @@ -196,7 +196,7 @@ public static Optional<String> getHostNameFromConfigKeys(Configuration conf,
*/
public static Optional<String> getHostName(String value) {
if ((value == null) || value.isEmpty()) {
return Optional.absent();
return Optional.empty();
}
return Optional.of(HostAndPort.fromString(value).getHostText());
}
Expand All @@ -208,11 +208,11 @@ public static Optional<String> getHostName(String value) {
*/
public static Optional<Integer> getHostPort(String value) {
if ((value == null) || value.isEmpty()) {
return Optional.absent();
return Optional.empty();
}
int port = HostAndPort.fromString(value).getPortOrDefault(NO_PORT);
if (port == NO_PORT) {
return Optional.absent();
return Optional.empty();
} else {
return Optional.of(port);
}
Expand All @@ -239,7 +239,7 @@ public static Optional<Integer> getPortNumberFromConfigKeys(
return hostPort;
}
}
return Optional.absent();
return Optional.empty();
}

/**
Expand All @@ -261,20 +261,17 @@ public static Collection<InetSocketAddress> getSCMAddresses(
+ " Null or empty address list found.");
}

final com.google.common.base.Optional<Integer>
defaultPort = com.google.common.base.Optional.of(ScmConfigKeys
.OZONE_SCM_DEFAULT_PORT);
final Optional<Integer> defaultPort = Optional
.of(ScmConfigKeys.OZONE_SCM_DEFAULT_PORT);
for (String address : names) {
com.google.common.base.Optional<String> hostname =
getHostName(address);
Optional<String> hostname = getHostName(address);
if (!hostname.isPresent()) {
throw new IllegalArgumentException("Invalid hostname for SCM: "
+ hostname);
}
com.google.common.base.Optional<Integer> port =
getHostPort(address);
Optional<Integer> port = getHostPort(address);
InetSocketAddress addr = NetUtils.createSocketAddr(hostname.get(),
port.or(defaultPort.get()));
port.orElse(defaultPort.get()));
addresses.add(addr);
}
return addresses;
Expand Down
Expand Up @@ -17,7 +17,6 @@

package org.apache.hadoop.hdds.scm;

import com.google.common.base.Optional;
import com.google.common.base.Strings;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdds.HddsConfigKeys;
Expand All @@ -32,6 +31,7 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

import static org.apache.hadoop.hdds.HddsConfigKeys
Expand Down Expand Up @@ -114,7 +114,7 @@ public static InetSocketAddress getScmAddressForDataNodes(
ScmConfigKeys.OZONE_SCM_DATANODE_ADDRESS_KEY);

InetSocketAddress addr = NetUtils.createSocketAddr(host.get() + ":" +
port.or(ScmConfigKeys.OZONE_SCM_DATANODE_PORT_DEFAULT));
port.orElse(ScmConfigKeys.OZONE_SCM_DATANODE_PORT_DEFAULT));

return addr;
}
Expand All @@ -135,8 +135,8 @@ public static InetSocketAddress getScmClientBindAddress(
ScmConfigKeys.OZONE_SCM_CLIENT_ADDRESS_KEY);

return NetUtils.createSocketAddr(
host.or(ScmConfigKeys.OZONE_SCM_CLIENT_BIND_HOST_DEFAULT) + ":" +
port.or(ScmConfigKeys.OZONE_SCM_CLIENT_PORT_DEFAULT));
host.orElse(ScmConfigKeys.OZONE_SCM_CLIENT_BIND_HOST_DEFAULT) + ":" +
port.orElse(ScmConfigKeys.OZONE_SCM_CLIENT_PORT_DEFAULT));
}

/**
Expand All @@ -155,8 +155,9 @@ public static InetSocketAddress getScmBlockClientBindAddress(
ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_ADDRESS_KEY);

return NetUtils.createSocketAddr(
host.or(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_BIND_HOST_DEFAULT) +
":" + port.or(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_PORT_DEFAULT));
host.orElse(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_BIND_HOST_DEFAULT)
+ ":"
+ port.orElse(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_PORT_DEFAULT));
}

/**
Expand All @@ -176,8 +177,8 @@ public static InetSocketAddress getScmDataNodeBindAddress(
ScmConfigKeys.OZONE_SCM_DATANODE_ADDRESS_KEY);

return NetUtils.createSocketAddr(
host.or(ScmConfigKeys.OZONE_SCM_DATANODE_BIND_HOST_DEFAULT) + ":" +
port.or(ScmConfigKeys.OZONE_SCM_DATANODE_PORT_DEFAULT));
host.orElse(ScmConfigKeys.OZONE_SCM_DATANODE_BIND_HOST_DEFAULT) + ":" +
port.orElse(ScmConfigKeys.OZONE_SCM_DATANODE_PORT_DEFAULT));
}


Expand Down
Expand Up @@ -17,7 +17,6 @@

package org.apache.hadoop.hdds.server;

import com.google.common.base.Optional;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdfs.DFSConfigKeys;
import org.apache.hadoop.hdfs.DFSUtil;
Expand All @@ -32,6 +31,7 @@
import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.Optional;

import static org.apache.hadoop.hdds.HddsUtils.getHostNameFromConfigKeys;
import static org.apache.hadoop.hdds.HddsUtils.getPortNumberFromConfigKeys;
Expand Down Expand Up @@ -118,10 +118,13 @@ protected InetSocketAddress getBindAddress(String bindHostKey,
final Optional<String> addresHost =
getHostNameFromConfigKeys(conf, addressKey);

String hostName = bindHost.or(addresHost).or(bindHostDefault);
String hostName = bindHost.orElse(addresHost.get());
if (hostName == null || hostName.isEmpty()) {
hostName = bindHostDefault;
}

return NetUtils.createSocketAddr(
hostName + ":" + addressPort.or(bindPortdefault));
hostName + ":" + addressPort.orElse(bindPortdefault));
}

/**
Expand Down
Expand Up @@ -20,13 +20,13 @@
import java.io.File;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.Optional;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hdds.HddsConfigKeys;
import org.apache.hadoop.hdds.server.ServerUtils;
import org.apache.hadoop.net.NetUtils;

import com.google.common.base.Optional;
import org.apache.hadoop.ozone.om.OMConfigKeys;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -60,7 +60,7 @@ public static InetSocketAddress getOmAddress(
OZONE_OM_ADDRESS_KEY);

return NetUtils.createSocketAddr(
host.or(OZONE_OM_BIND_HOST_DEFAULT) + ":" +
host.orElse(OZONE_OM_BIND_HOST_DEFAULT) + ":" +
getOmRpcPort(conf));
}

Expand Down Expand Up @@ -90,15 +90,15 @@ public static int getOmRpcPort(Configuration conf) {
// If no port number is specified then we'll just try the defaultBindPort.
final Optional<Integer> port = getPortNumberFromConfigKeys(conf,
OZONE_OM_ADDRESS_KEY);
return port.or(OZONE_OM_PORT_DEFAULT);
return port.orElse(OZONE_OM_PORT_DEFAULT);
}

public static int getOmRestPort(Configuration conf) {
// If no port number is specified then we'll just try the default
// HTTP BindPort.
final Optional<Integer> port =
getPortNumberFromConfigKeys(conf, OZONE_OM_HTTP_ADDRESS_KEY);
return port.or(OZONE_OM_HTTP_BIND_PORT_DEFAULT);
return port.orElse(OZONE_OM_HTTP_BIND_PORT_DEFAULT);
}

/**
Expand Down

0 comments on commit d16d5f7

Please sign in to comment.