package com.openxc.interfaces;

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

import android.util.Log;

import com.openxc.sources.DataSourceException;
import com.openxc.sources.DataSourceResourceException;

public class UriBasedVehicleInterfaceMixin {
    private final static String TAG = "UriBasedVehicleInterfaceMixin";

    /**
     * Return true if the given address and port match those currently in use by
     * the network data source.
     *
     * @return true if the address and port match the current in-use values.
     */
    public static boolean sameResource(URI uri, String otherResource) {
        try {
            return createUri(otherResource).equals(uri);
        } catch(DataSourceException e) {
            return false;
        }
    }

    /**
     * Return true if the address and port are valid.
     *
     * @return true if the address and port are valid.
     */
    public static boolean validateResource(String uriString) {
        if(uriString == null) {
            return false;
        }

        try {
            return validateResource(createUri(uriString));
        } catch(DataSourceException e) {
            Log.d(TAG, "URI is not valid", e);
            return false;
        }
    }

    public static boolean validateResource(URI uri) {
        return uri != null && uri.getPort() != -1 && uri.getHost() != null;
    }

    public static URI createUri(String uriString) throws DataSourceException {
        try {
            return new URI(uriString);
        } catch(URISyntaxException e) {
            throw new DataSourceResourceException("Not a valid URI: " +
                    uriString, e);
        }
    }

}

@@ -212,16 +212,123 @@
 ption;%0A%0A
+/**%0A * A collection of utilites for vehicle interfaces that reference a physical%0A * device with a URI.%0A */%0A
 public c
@@ -455,112 +455,179 @@
   * 
-Return true if the given address and port match those currently in use by%0A     * the network data source
+Determine if two URIs refer to the same resource.%0A     *%0A     * The function safely attempts to convert the otherResource parameter to a%0A     * URI object before comparing
 .%0A  
@@ -962,53 +962,95 @@
   * 
-Return true if the address and port are valid
+Convert the parameter to a URI and validate the correctness of its host%0A     * and port
 .%0A  
@@ -1461,140 +1461,523 @@
     
-public static boolean validateResource(URI uri) %7B%0A        return uri != null && uri.getPort() != -1 && uri.getHost() != null;%0A    %7D%0A
+/**%0A     * Validate the correctness of the host and port in a given URI.%0A     *%0A     * @return true if the address and port are valid.%0A     */%0A    public static boolean validateResource(URI uri) %7B%0A        return uri != null && uri.getPort() != -1 && uri.getHost() != null;%0A    %7D%0A%0A    /**%0A     * Attempt to construct an instance of URI from the given String.%0A     *%0A     * @param uriString the String representation of the possible URI.%0A     * @throws DataSourceException if the parameter is not a valid URI.%0A     */
 %0A   
@@ -2268,11 +2268,10 @@
 %7D%0A    %7D%0A
-%0A
 %7D%0A

Document UriBasedVehicleInterfaceMixin's methods.