URI compareTo(URI) in Java

compareTo(URI): This method is available in the java.net.URI class of Java.

Syntax:

int java.net.URI.compareTo(URI that)

This method takes one argument. This method compares this URI to another object, which must be a URI.

When comparing corresponding components of two URIs, if one component is undefined but the other is defined then the first is considered to be less than the second.

The ordering of URIs is defined as follows:

1. Two URIs with different schemes are ordered according to the ordering of their schemes, without regard to the case.

2. A hierarchical URI is considered to be less than an opaque URI with an identical scheme.

3. Two opaque URIs with identical schemes are ordered according to the ordering of their scheme-specific parts.

4. Two opaque URIs with identical schemes and scheme-specific parts are ordered according to the ordering of their fragments.

5. Two hierarchical URIs with identical schemes are ordered according to the ordering of their authority components:

a) . If both authority components are server-based then the URIs are ordered according to their user-information components; if these components are identical then the URIs are ordered according to the ordering of their hosts, without regard to case; if the hosts are identical then the URIs are ordered according to the ordering of their ports.

b). If one or both authority components are registry-based then the URIs are ordered according to the ordering of their authority components.

6. Finally, two hierarchical URIs with identical schemes and authority components are ordered according to the ordering of their paths; if their paths are identical then they are ordered according to the ordering of their queries; if the queries are identical then they are ordered according to the order of their fragments.

Parameters: One parameter is required for this method.

that: The object to which this URI is to be compared.

Returns: negative integer, zero, or a positive integer as this URI is less than, equal to, or greater than the given URI.

Throws:

1. ClassCastException - If the given object is not a URI

Approach 1: When no exception

Java

package com.URI;

import java.net.URI;
import java.net.URISyntaxException;

public class URIcompareTo {
    public static void main(String[] args) throws URISyntaxException {
        String scheme = "https", userInfo = "userInfo", host = "localhost", path = "/hello", query = "param=value",
                fragment = "fragment";
        int port = 8080;

        URI uri = new URI(scheme, userInfo, host, port, path, query, fragment);

        System.out.println(uri.compareTo(uri));

    }
}

Output:

0


Approach 2: ClassCastException 

Java

package com.URI;

import java.net.URI;
import java.net.URISyntaxException;

public class URIcompareTo {
    public static void main(String[] args) throws URISyntaxException {
        String scheme = "https", userInfo = "userInfo", host = "localhost", path = "/hello", query = "param=value",
                fragment = "fragment";
        int port = 8080;

        URI uri = new URI(scheme, userInfo, host, port, path, query, fragment);

        Object obj = new Object();
        System.out.println(uri.compareTo((URI) obj));

    }
}

Output:

Exception in thread "main" java.lang.ClassCastException: class java.lang.Object cannot be cast to class java.net.URI (java.lang.Object and java.net.URI are in module java.base of loader 'bootstrap') at com.URI.URIcompareTo.main(URIcompareTo.java:15)


Some other methods of URI class

URI(String)This method constructs a URI by parsing the given string.

URI(String, String, String)This method constructs a URI from the given components. A component may be left undefined by passing null.

URI(String, String, String, String)This method constructs a hierarchical URI from the given components.

URI(String, String, String, String, String)This method constructs a hierarchical URI from the given components. If a scheme is given then the path, if also given, must either be empty or begin with a slash character ('/'). Otherwise, a component of the new URI may be left undefined by passing null for the corresponding parameter.

URI(String, String, String, int, String, String, String)This method constructs a hierarchical URI from the given components. If a scheme is given then the path, if also given, must either be empty or begin with a slash character ('/'). Otherwise, a component of the new URI may be left undefined by passing null for the corresponding parameter or, in the case of the port parameter, bypassing -1.

compareTo(URI)This method compares this URI to another object, which must be a URI.

URI.create(String)This method creates a URI by parsing the given string.

equals(Object)This method tests this URI for equality with another object.

getAuthority()This method returns the decoded authority component of this URI.

getFragment()This method returns the decoded fragment component of this URI.

getHost()This method returns the host component of this URI.

getPath()This method returns the decoded path component of this URI.

getPort()This method returns the port number of this URI.

getQuery()This method returns the decoded query component of this URI.

getRawAuthority()This method returns the raw authority component of this URI.

getRawFragment()This method returns the raw fragment component of this URI.

getRawPath()This method returns the raw path component of this URI.

getRawQuery()This method returns the raw query component of this URI.

getRawSchemeSpecificPart()This method returns the raw scheme-specific part of this URI. The scheme-specific part is never undefined, though it may be empty.

getRawUserInfo()This method returns the raw user-information component of this URI.

getScheme()This method returns the scheme component of this URI.

hashCode()This method returns a hash code value for this URI.

isAbsolute()This method tells whether or not this URI is absolute.

isOpaque()This method tells whether or not this URI is opaque.

normalize()This method normalizes this URI's path.

parseServerAuthority()This method attempts to parse this URI's authority component if defined, into user information, host, and port components.

relativize(URI) This method relativizes the given URI against this URI.

resolve(String)This method constructs a new URI by parsing the given string and then resolving it against this URI.

resolve(URI)This method resolves the given URI against this URI

toASCIIString()This method returns the content of this URI as a US-ASCII string.

toString()This method returns the content of this URI as a string.

toURL()This method constructs a URL from this URI.

No comments:

Post a Comment