[1658] in java-interest

home help back first fref pref prev next nref lref last post

Re: extending string functionality.

daemon@ATHENA.MIT.EDU (Patrick Simpson)
Wed Sep 13 14:39:43 1995

Date: Wed, 13 Sep 1995 07:56:08 -0800
To: Graham Matthews <graham@pell.anu.edu.au>, java-interest@java.sun.com
From: psimpson@qualcomm.com (Patrick Simpson)

>I have a Java Language question. It probably has a simple answer, but I
>have not found it.
>
>I have a collection of string parsing routines written in a language called
>Python (these routines don't change the strings in any way, rather they
>return information from within the string (eg. maximal length substrings
>satisfying patterns, etc). Now I have translated them into Java, and would
>like to allow them to be user on Java Strings (i.e. objects of class String).
>There does not seem to be a convenient way of doing this however.
>
>I first tried to put all these routines in a file, and just compile up
>that file. That didn't work as it seems that all routines have to be
>inside class definitions (ie. have to be methods).
>
>So I thought I would extend class String - can't do that since there are
>no extension mechanisms (other than subclassing in Java).
>
>So I thought I would subclass String, making a class MyString, which
>contained my routines, and which inherited the String methods. Can't do
>that since String is final.
>
>There seems to be no way to add my routines to the existing functionality
>of class String.
>
>graham
>                  A cat named Easter
>              He say "will you ever learn"
>              Its just an empty cage girl
>                 If you kill the bird
>
>-
>Note to Sun employees: this is an EXTERNAL mailing list!
>Info: send 'help' to java-interest-request@java.sun.com

The most `easily' and probabily not the best way to solve your situtation,
create MyString subclassed from Object. Then provide as methods a String
contructor and a toString function. Put some kind of representation of a
String in your class. Now comes to tricky part, do your function need
access to the internal representations of String ( for the most part you
can probably get away with using the provided implementation of String). If
so then implement all of the normal String methods on your new data
representation as well as your new functions. Otherwise implement the
String methods in your class as pass throughs to you r internal String.
Example:

class MyString { // subclass from Object
        String myString;

        MyString() { ... };
        MyString(String str) { ,,, };

        charAt(int index) { // this is a string method.
                if (myString != null) {
                        return myString.charAt(index);
                }
                return -1;
        }

        yourMethod(...) { ... } ;

        .
        .
        .

}


-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post