FileDocCategorySizeDatePackage
Connection.javaAPI DocphoneME MR2 API (J2ME)4314Wed May 02 18:00:00 BST 2007com.sun.midp.io.j2me.push

Connection

public final class Connection extends Object
Connection string parser.

Fields Summary
private final String
protocol
Protocol name.

Invariant: never null or empty.

private final String
target
Target.

Invariant: never null.

private final String
params
Params.
Constructors Summary
private Connection(String protocol, String target, String params)
Constructs a connection.

param
protocol protocol
param
target target
param
params params

        // assert (protocol != null) && (protocol.length() > 0);
        this.protocol = protocol;
        // assert (target != null);
        this.target = target;
        this.params = params;
    
Methods Summary
public java.lang.StringgetConnection()
Returns original, unmodifed connection string.

return
connection string

        final String s = protocol + ":" + target;
        return (params != null) ? s + params : s;
    
public java.lang.StringgetParams()
Gets params.

Params (if any) are prefixed with ';'.

return
params

        return params;
    
public java.lang.StringgetProtocol()
Gets a protocol.

Invariant: never null or empty.

return
protocol

        return protocol;
    
public java.lang.StringgetTarget()
Gets a target.

Invariant: never null.

return
target

        return target;
    
public static com.sun.midp.io.j2me.push.Connectionparse(java.lang.String connection)
Parses a connection string.

param
connection connection string to parse
return
Connection object
throws
IllegalArgumentException if connection is invalid

        if (connection == null) {
            throw new IllegalArgumentException("connection is null");
        }

        // Parse a connection string partially (fetching scheme and the rest)
        // Actually, I'd rather factor it into a class
        final int colonPos = connection.indexOf(':");
        if (colonPos == -1) {
            throw new IllegalArgumentException("connection is invalid");
        }
        if (colonPos < 1) {
            throw new IllegalArgumentException("invalid protocol");
        }

        final String protocol = connection.substring(0, colonPos);
        final String targetAndParams = connection.substring(colonPos + 1);

        final int paramsPos = targetAndParams.indexOf(';");
        if (paramsPos == -1) {
            return new Connection(protocol, targetAndParams, null);
        } else {
            final String target = targetAndParams.substring(0, paramsPos);
            final String params = targetAndParams.substring(paramsPos);
            return new Connection(protocol, target, params);
        }