int URI::compareTo()

in activemq-cpp/src/main/decaf/net/URI.cpp [259:364]


int URI::compareTo( const URI& uri ) const {

    int ret = 0;

    // compare schemes
    if (this->uri.getScheme() == "" && uri.getScheme() != "") {
        return -1;
    } else if (this->uri.getScheme() != "" && uri.getScheme() == "") {
        return 1;
    } else if (this->uri.getScheme() != "" && uri.getScheme() != "") {
        ret = StringUtils::compareIgnoreCase(this->uri.getScheme().c_str(), uri.getScheme().c_str());
        if (ret != 0) {
            return ret > 0 ? 1 : -1;
        }
    }

    // compare opacities
    if (!this->uri.isOpaque() && uri.isOpaque()) {
        return -1;
    } else if (this->uri.isOpaque() && !uri.isOpaque()) {
        return 1;
    } else if (this->uri.isOpaque() && uri.isOpaque()) {
        ret = StringUtils::compare(this->getSchemeSpecificPart().c_str(),
                                   uri.getSchemeSpecificPart().c_str());
        if (ret != 0) {
            return ret > 0 ? 1 : -1;
        }
    } else {

        // otherwise both must be hierarchical

        // compare authorities
        if (this->uri.getAuthority() != "" && uri.getAuthority() == "") {
            return 1;
        } else if (this->uri.getAuthority() == "" && uri.getAuthority() != "") {
            return -1;
        } else if (this->uri.getAuthority() != "" && uri.getAuthority() != "") {

            if (this->uri.getHost() != "" && uri.getHost() != "") {

                // both are server based, so compare userinfo, host, port
                if (this->getUserInfo() != "" && uri.getUserInfo() == "") {
                    return 1;
                } else if (this->getUserInfo() == "" && uri.getUserInfo() != "") {
                    return -1;
                } else if (this->getUserInfo() != "" && uri.getUserInfo() != "") {
                    ret = StringUtils::compare(this->getUserInfo().c_str(),
                                               uri.getUserInfo().c_str());
                    if (ret != 0) {
                        return ret > 0 ? 1 : -1;
                    }
                }

                // userinfo's are the same, compare hostname
                ret = StringUtils::compareIgnoreCase(this->uri.getHost().c_str(), uri.getHost().c_str());
                if (ret != 0) {
                    return ret > 0 ? 1 : -1;
                }

                // compare port
                if (this->getPort() != uri.getPort()) {
                    return (getPort() - uri.getPort()) > 0 ? 1 : -1;
                }
            } else {
                // one or both are registry based, compare the whole authority
                ret = StringUtils::compare(this->uri.getAuthority().c_str(), uri.getAuthority().c_str());
                if (ret != 0) {
                    return ret > 0 ? 1 : -1;
                }
            }
        }

        // authorities are the same, compare paths
        ret = StringUtils::compare(this->getPath().c_str(), uri.getPath().c_str());
        if (ret != 0) {
            return ret > 0 ? 1 : -1;
        }

        // compare queries
        if (this->getQuery() != "" && uri.getQuery() == "") {
            return 1;
        } else if (this->getQuery() == "" && uri.getQuery() != "") {
            return -1;
        } else if (this->getQuery() != "" && uri.getQuery() != "") {
            ret = StringUtils::compare(this->getQuery().c_str(), uri.getQuery().c_str());
            if (ret != 0) {
                return ret > 0 ? 1 : -1;
            }
        }
    }

    // everything else is identical, so compare fragments
    if (this->getFragment() != "" && uri.getFragment() == "") {
        return 1;
    } else if (this->getFragment() == "" && uri.getFragment() != "") {
        return -1;
    } else if (this->getFragment() != "" && uri.getFragment() != "") {
        ret = StringUtils::compare(this->getFragment().c_str(), uri.getFragment().c_str());
        if (ret != 0) {
            return ret > 0 ? 1 : -1;
        }
    }

    // identical
    return 0;
}