Repository: apache/dubbo
File: dubbo-common/src/main/java/org/apache/dubbo/common/utils/NetUtils.java (Java)

Code before:
    public synchronized static int getAvailablePort() {
        int randomPort = getRandomPort();
        return getAvailablePort(randomPort);
    }

    public synchronized static int getAvailablePort(int port) {
        if (port < MIN_PORT) {
            port = MIN_PORT;
        }
        for (int i = port; i < MAX_PORT; i++) {
            if (USED_PORT.get(i)) {
                continue;
            }
            try (ServerSocket ignored = new ServerSocket(i)) {
                USED_PORT.set(i);
                return i;
            } catch (IOException e) {
                // continue
            }
        }
        return port;
    }

    /**
     * Check the port whether is in use in os
     * @param port
     * @return
     */
    public static boolean isPortInUsed(int port) {
        try (ServerSocket ignored = new ServerSocket(port)) {
            return false;
        } catch (IOException e) {
            // continue
        }
        return true;
    }

    public static boolean isInvalidPort(int port) {
        return port < MIN_PORT || port > MAX_PORT;
    }

    public static boolean isValidAddress(String address) {
        return ADDRESS_PATTERN.matcher(address).matches();
    }

    public static boolean isLocalHost(String host) {
        return host != null
                && (LOCAL_IP_PATTERN.matcher(host).matches()
                || host.equalsIgnoreCase(LOCALHOST_KEY));
    }


Review comment (suggestion):
Please improve the javadoc so `@param port` and `@return` is complete

I suggest to complete it with:
`@param port   the port to check if in use`
`@return is the given port in use or not`

Code after:
    public synchronized static int getAvailablePort() {
        int randomPort = getRandomPort();
        return getAvailablePort(randomPort);
    }

    public synchronized static int getAvailablePort(int port) {
        if (port < MIN_PORT) {
            port = MIN_PORT;
        }
        for (int i = port; i < MAX_PORT; i++) {
            if (USED_PORT.get(i)) {
                continue;
            }
            try (ServerSocket ignored = new ServerSocket(i)) {
                USED_PORT.set(i);
                return i;
            } catch (IOException e) {
                // continue
            }
        }
        return port;
    }

    /**
     * Check the port whether is in use in os
     * 
     * @param port the port to check if in use
     * @return is the given port in use or not
     */
    public static boolean isPortInUsed(int port) {
        try (ServerSocket ignored = new ServerSocket(port)) {
            return false;
        } catch (IOException e) {
            // continue
        }
        return true;
    }

    public static boolean isInvalidPort(int port) {
        return port < MIN_PORT || port > MAX_PORT;
    }

    public static boolean isValidAddress(String address) {
        return ADDRESS_PATTERN.matcher(address).matches();
    }

    public static boolean isLocalHost(String host) {
        return host != null
                && (LOCAL_IP_PATTERN.matcher(host).matches()
                || host.equalsIgnoreCase(LOCALHOST_KEY));
    }