Results 1 to 12 of 12
  1. #1
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default [SOLVED] CharSequence interface

    Can we do this?
    Java Code:
    private static TreeMap<Integer,java.lang.CharSequence>

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    What happens when you try to compile it? Let the compiler give you the answer.

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default yes, I know about that

    Quote Originally Posted by Norm View Post
    What happens when you try to compile it? Let the compiler give you the answer.
    Not to dissuade you, a CharacterSequence is an interface. Those work in ways I do not understand yet. I will wait on a more advanced answer.

    In general, what they are trying to do is KISS, but the way they get used suggests relying soley on compiler is not a sufficient study. I will test anyway, as you direct, after I get some more advanced answers.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  4. #4
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    For me that's fine. Because Charsequence is a valid mapped value for TreeMap.

    I don't know how far my answer is valuable to you. ;)

  5. #5
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default a great help

    Quote Originally Posted by Eranga View Post
    ...I don't know how far my answer is valuable to you. ;)
    sounds like it should work in other words. I will do a test like Norm directs, I just wanted someone else's opinon on the matter and am greatly appreciative.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    Here's a Q&D implementation of the CharSequence class. It just needs some real guts to the various required methods, but should compile and give you a real object to satisfy the compiler with the TreeMap constructor.

    Java Code:
       class Tester implements java.lang.CharSequence {
        public char 	charAt(int index){return 'a';}
    //          Returns the char value at the specified index.
        public int 	length() {return 1;}
    //          Returns the length of this character sequence.
        public CharSequence 	subSequence(int start, int end) { return this;}
    //          Returns a new CharSequence that is a subsequence of this sequence.
        public String 	toString()  {return "A";}
    //          Returns a string containing the ch
       } // end class
    Then to use the above:
    Java Code:
       private static TreeMap<Integer,java.lang.CharSequence> myTree;
            ....
    
         myTree = new TreeMap<Integer, CharSequence>();
         myTree.put(new Integer(1), new Tester());
    Last edited by Norm; 07-10-2008 at 05:57 PM.

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Talking preliminary implementation of your template

    Quote Originally Posted by Norm View Post
    ...It just needs some real guts to the various required methods,
    We will be taking some heat on this as shop practices that are normative ( sorry ) even at the trade school level have been dropped about a decade ago from the contemporary instructional horizon because of everything having rubber bumpers in all the places except where they really need them.

    This is close to the buffer I actually want to use in the TreeMap.

    Java Code:
    // A compiled representation of a regular expression.
    import java.util.regex.Pattern;
    
    // Matcher: An engine that performs match operations
    // on a character sequence by interpreting a Pattern.
    import java.util.regex.Matcher;//
    
    /**
     * Discussion of an opinion in a technical forum.
     * Reader assumes the risk under any rework, the 
     * code here is known to be incomplete and has no
     * legitimate derivative use beyond further development.
     * If you read this far you are like us and need to join our forum.
     */
     
    public class Buffer implements java.lang.CharSequence 
    {
        private int POSITION; // internal - not Java Naming style conventions.
        private Pattern pattern;
        private Matcher matcher;
    
        // Use of all caps to denote something which
        // is not intended to change during Object use.
        private static Integer BUFFER_LIMIT;
    
        // The actual buffer.
        private char[] INTERNAL_BUFFER;
    
        // Public constructor for use.
        public Buffer()
        {
            // 4096 is enough line length
            this(new java.lang.Integer(4096));
        }
    
        // Public constructor for use.
        public Buffer(Integer size)
        {
            // I think this looks for words ....
            this.pattern = new Pattern("\\w??");
            // We use an Object type to avoid auto-boxing
            this.BUFFER_LIMIT = new java.lang.Integer(size);//
            this.INTERNAL_BUFFER = new char[BUFFER_LIMIT];//
        }
    
        // Public constructor for use.
        public Buffer(Integer size,Pattern searchPattern)
        {
            this.pattern = new Pattern(searchPattern);
            // We use an Object type to avoid auto-boxing
            // since that may generate hidden nulls.
            this.BUFFER_LIMIT = new java.lang.Integer(size);//
            this.INTERNAL_BUFFER = new char[BUFFER_LIMIT];//
        }
        public String insertString(String suppliedString)
        {
            // Hey kids, does your computer have registers?
            int index = suppliedString.length();
            do
            {
                // Norm, this is a normal copy operation.
                // Repetive thus .....
                // Register <-   RAM { location source }
                // Register   -> RAM { location destination }
                INTERNAL_BUFFER[--index] = suppliedString.charAt(index);//
            }
            while(index > 0x0000);//
        }
        // Returns the char value at the specified index.
        public Character charAt(int index)
        {
            // Need exception trapping and bounds checking.
            return new Character(INTERNAL_BUFFER[index]);
        }
        // Returns the length of this character sequence.
        public int length()
        {
            return POSITION;
        }
        // Returns a new CharSequence that is a subsequence of this sequence.
        public CharSequence subSequence(int start, int end) 
        {
            // Replacing this pointer with a String because I don't
            // have all the buffer logic worked sufficiently well.
            // We could return the this pointer later.
            char[] temp = new char[end - start];//Bounds checking?
            int index = temp.length;//
            do
            {
                // Norm, this can be re-implemented as
                // a traditional for loop or while() '
                // I do it this way because I understand the loop logic.
                temp[--index] = INTERNAL_BUFFER[index];//
            }
            while(index > 0x0000);
            // Return a string from their copy, not ours.
            return new String(temp);//
        }
        // Use our own subSequence to generate a distinct string
        public String toString()
        {
            return subSequence(0, POSITION);
        }
           
    } // end Buffer
    The work here is first-draft prior to preliminary compiler pass.
    What is Q&D? Mabye: Archaeologists to Demonstrate Ancient Brewing
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  8. #8
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default build log .... we have work to do

    Java Code:
    Buffer.java:54: charAt(int) in Buffer 
    cannot implement charAt(int) in java.lang.CharSequence; 
    attempting to use incompatible return type
    
    found   : java.lang.Character
    required: char
    public Character charAt(int index)
    
    Buffer.java:25: cannot find symbol
    symbol  : constructor Pattern(java.lang.String)
    location: class java.util.regex.Pattern
            this.pattern = new Pattern("\\w??");
    
    
    Buffer.java:33: cannot find symbol
    symbol  : constructor Pattern(java.util.regex.Pattern)
    location: class java.util.regex.Pattern
    
            this.pattern = new Pattern(searchPattern);
    Buffer.java:86: incompatible types
    found   : java.lang.CharSequence
    required: java.lang.String
            return subSequence(0, POSITION);
    
    5 errors
    
    BUILD FAILED (total time: 2 seconds)
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    Quick and Dirty.
    Java Code:
    // A compiled representation of a regular expression.
    import java.util.regex.Pattern;
    
    // Matcher: An engine that performs match operations
    // on a character sequence by interpreting a Pattern.
    import java.util.regex.Matcher;//
    
    /**
     * Discussion of an opinion in a technical forum.
     * Reader assumes the risk under any rework, the 
     * code here is known to be incomplete and has no
     * legitimate derivative use beyond further development.
     * If you read this far you are like us and need to join our forum.
     */
     
    public class Buffer implements java.lang.CharSequence 
    {
        private int POSITION; // internal - not Java Naming style conventions.
        private Pattern pattern;
        private Matcher matcher;
    
        // Use of all caps to denote something which
        // is not intended to change during Object use.
        private static Integer BUFFER_LIMIT;
    
        // The actual buffer.
        private char[] INTERNAL_BUFFER;
    
        // Public constructor for use.
        public Buffer()
        {
            // 4096 is enough line length
            this(new java.lang.Integer(4096));
        }
    
        // Public constructor for use.
        public Buffer(Integer size)
        {
            // I think this looks for words ....
            this.pattern = Pattern.compile/*new Pattern*/("\\w??"); // no Constructor
            // We use an Object type to avoid auto-boxing
            this.BUFFER_LIMIT = new java.lang.Integer(size);//
            this.INTERNAL_BUFFER = new char[BUFFER_LIMIT];//
        }
    
        // Public constructor for use.
        public Buffer(Integer size,Pattern searchPattern)
        {
            this.pattern = /*new Pattern*/(searchPattern);
            // We use an Object type to avoid auto-boxing
            // since that may generate hidden nulls.
            this.BUFFER_LIMIT = new java.lang.Integer(size);//
            this.INTERNAL_BUFFER = new char[BUFFER_LIMIT];//
        }
        public String insertString(String suppliedString)
        {
            // Hey kids, does your computer have registers?
            int index = suppliedString.length();
            do
            {
                // Norm, this is a normal copy operation.
                // Repetive thus .....
                // Register <-   RAM { location source }
                // Register   -> RAM { location destination }
                INTERNAL_BUFFER[--index] = suppliedString.charAt(index);//
            }
            while(index > 0x0000);//
            return "???";
        }
        // Returns the char value at the specified index.
        public /*Character*/ char charAt(int index) // returns char not Char
        {
            // Need exception trapping and bounds checking.
            return new Character(INTERNAL_BUFFER[index]);
        }
        // Returns the length of this character sequence.
        public int length()
        {
            return POSITION;
        }
        // Returns a new CharSequence that is a subsequence of this sequence.
        public CharSequence subSequence(int start, int end) 
        {
            // Replacing this pointer with a String because I don't
            // have all the buffer logic worked sufficiently well.
            // We could return the this pointer later.
            char[] temp = new char[end - start];//Bounds checking?
            int index = temp.length;//
            do
            {
                // Norm, this can be re-implemented as
                // a traditional for loop or while() '
                // I do it this way because I understand the loop logic.
                temp[--index] = INTERNAL_BUFFER[index];//
            }
            while(index > 0x0000);
            // Return a string from their copy, not ours.
            return new String(temp);//
        }
        // Use our own subSequence to generate a distinct string
        public String toString()
        {
            // What to return ???
            return "???";                                     // subSequence doesn't return a String  
    //        return subSequence(0, POSITION);    //??  public CharSequence subSequence(int start, int end) 
        }
           
    } // end Buffer
    Last edited by Norm; 07-11-2008 at 04:44 AM.

  10. #10
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post clean compile - ready for test harness

    Quote Originally Posted by Norm View Post
    Quick and Dirty.
    Aw gee, thought it meant quality and delivered homebrew.

    Java Code:
    Tool completed successfully
    Issues remain, and as well to design a test harness.
    Additionally, we probably should work on implementing
    matcher.find() which would be boolean find() return matcher.find();//

    I reworked constructor to work towards this, see link to docs at constructor taking an Integer. I made several changes after getting one clean compile, did not do a second build.

    Pattern.compile("\\w??"); is a static method and may be called directly at any point to get an instance of a Pattern. I have seen this approach in some remarkably skilled work. Google for "Why Johnny can't crypto" or ask me. These are called static factory methods in the cs literature. I found that using the design avoids several subtle constructor issues that are not apparent and have no ready approach visible when first encountered in studies.

    I added one.

    Java Code:
    // A compiled representation of a regular expression.
    import java.util.regex.Pattern;
    
    // Matcher: An engine that performs match operations
    // on a character sequence by interpreting a Pattern.
    import java.util.regex.Matcher;//
    
    /**
     * Discussion of an opinion in a technical forum.
     * Reader assumes the risk under any rework, the
     * code here is known to be incomplete and has no
     * legitimate derivative use beyond further development.
     * If you read this far you are like us and need to join our forum.
     */
    
    public class Buffer implements java.lang.CharSequence
    {
        // Member variables.
        private int POSITION; // internal - not Java Naming style conventions.
        private Pattern pattern;
        private Matcher matcher;
    
        // Should be final, that fixes buffer size.
        // Making buffer size a constructor parameter 
        // and buffer size final brings some clumsy 
        // constructor syntax or non-conformant approaches.
        private static Integer BUFFER_LIMIT;
    
        // The actual buffer.
        private char[] INTERNAL_BUFFER;
    
        // Public constructor for use.
        public Buffer()
        {
            // 4096 is enough line length
            this(new java.lang.Integer(4096));
        }
    
        // Public constructor for use.
        public Buffer(Integer size)
        {
            // http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html
            this.pattern = Pattern.compile("\\w??"); // no Constructor needed.
            this.matcher = pattern.matcher(new String(INTERNAL_BUFFER));
            // We use an Object type to avoid compiler working
            // in unanticipated optimizations that break our design.
            this.BUFFER_LIMIT = new java.lang.Integer(size);//
            this.INTERNAL_BUFFER = new char[BUFFER_LIMIT];//
        }
    
        // Public constructor for use.
        public Buffer(Integer size,String searchPattern)
        {
            this.pattern = Pattern.compile(searchPattern);
            this.matcher = pattern.matcher(new String(INTERNAL_BUFFER));
            // We use an Object type to avoid auto-boxing
            // since that may generate hidden nulls.
            this.BUFFER_LIMIT = new java.lang.Integer(size);//
            this.INTERNAL_BUFFER = new char[BUFFER_LIMIT];//
        }
        
        // Static Factory method:
        public static Buffer getInstance()
        {
            return new Buffer();
        }
        
        // Rework likely.
        public String insertString(String suppliedString)
        {
            // Hey kids, does your computer have registers?
            int index = suppliedString.length();
            do
            {
                // Norm, this is a normal copy operation.
                // Repetive thus .....
                // Register <-   RAM { location source }
                // Register   -> RAM { location destination }
                INTERNAL_BUFFER[--index] = suppliedString.charAt(index);//
            }
            while(index > 0x0000);
            // The contents of the character array are copied;
            // subsequent modification of the character array
            // does not affect the newly created string. We
            // should design test harness to insure the inversion
            // of the design contract spec'd by the String class
            // does or does not give the caller a reference to
            // our buffer. Probably correct, and if so renders
            // our initial design weak if used in a core loop:
            // Two copy operations for one copy operation.
            return new String(INTERNAL_BUFFER);// move to ...?
        }
        
        // Returns the char value at the specified index.
        public char charAt(int index) // returns char not Char
        {
            // Need exception trapping and bounds checking.
            char c = INTERNAL_BUFFER[index];
            return c;
        }
        
        // Returns the length of this character sequence.
        public int length()
        {
            return POSITION;
        }
        
        // Returns a new CharSequence that is a subsequence of this sequence.
        public CharSequence subSequence(int start, int end)
        {
            // Replacing this pointer with a String because I don't
            // have all the buffer logic worked sufficiently well.
            // We could return the this pointer later.
            char[] temp = new char[end - start];//Bounds checking?
            int index = temp.length;//
            do
            {
                // Norm, this can be re-implemented as
                // a traditional for loop or while() '
                // I do it this way because I understand the loop logic.
                temp[--index] = INTERNAL_BUFFER[index];//
            }
            while(index > 0x0000);
            // Return a string from their copy, not ours.
            return  (CharSequence)  new String(temp);//
        }
        
        // Use public String(char[] value) ?
        // There is an internal that is not published:
        // If the char[] has a size of 4096 but we have
        // only placed valid characters in some range 0 - ?
        // then does the string have a length of INTERNAL_BUFFER ?
        // If so, our fundamental design approach has to be rethunk.
        public String toString()
        {
            // We can probably do this:
            return new String(INTERNAL_BUFFER);
        }
    
    } // end Buffer
    BTW: More on Collection FrameWork | Java Tips Blog
    Last edited by Nicholas Jordan; 07-11-2008 at 04:15 PM.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  11. #11
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,812
    Rep Power
    25

    Default

    Ques re do{} while():
    What does your code do if String length = 0?

    What is the purpose and use of the Buffer class? Why does it implement CharSequence?

  12. #12
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Post read write area for session master

    Quote Originally Posted by Norm View Post
    Ques re do{} while():
    What does your code do if String length = 0?
    rework;
    Java Code:
        // First rework.
        public String insertString(String suppliedString)
        {
            if(suppliedString != null)
            {
                try
                {
                    if(suppliedString.length() > 0x0000)
                    {
                        // Hey kids, does your computer have registers?
                        POSITION = suppliedString.length();
                        int index = suppliedString.length();
                        do
                        {
                            // .......
                            INTERNAL_BUFFER[--index] = suppliedString.charAt(index);//
                        }
                        while(index > 0x0000);
                            // .......
                        return new String(INTERNAL_BUFFER);// move to ...?
                    }
                    else
                    {
                        return suppliedString;
                    }
                }
                catch(ArrayIndexOutOfBoundsException aobe)
                {
                    // add import java.io.PrintWriter;
                    // add import java.io.FIleWriter;
                    PrintWriter pw = new PrintWriter(new FileWriter("error.log"));//
                    pw.print(e.getMessage());
                    return new String("");
                }
            }
            else
           {
                return new String("");//
           }
        }
    What is the purpose and use of the Buffer class? Why does it implement CharSequence?
    Holds mutable character strings while someone makes up their mind, I am working on generating AES keys right now. Once I have a session key we also have crypto keys independentlyly of what we key on here. As well maybe even some client side js so client is not involved with weak password generation. We get a real crypto key, and a - I do not have a word for this that the cryptologists will not hit me on - a final Integer that is and always is the map key && session key for this transaction. We try to match that up in the logs using tools designed for that purpose. At some point, what customer decided would be shipped to a site with stronger skills in verification, but I keep a transaction log of everything I can think of. We have an immutable session key for the transaction, but a mutable buffer for a while. The point of the design is the mutable in the SessionMaster mutable dtata is a CharSequence, not the classes I see in the libraries.

    Several approaches are available, what I want to do here is not unlike StringBuffer. It is just that I have several fields.Perhaps I may want to do zip code crossed to town and whatever comes within skills. The primary idea is that of only allocating one char[] per field so that scalability is not broken my multiple needless new String calls. Often the libs will introduce hidden gotchas, this core area will be fine toothed ~ no wiggle room.

    See Dov Bulka, PhD: Efficient C++
    ISBN:0201379503
    Dov Bulka has spent fifteen years in the trenches of software development delivering large-scale software products to market. He was the performance architect of the IBM Domino-Go Web server that has powered some of the biggest Web sites ever hosted on the Internet, including that of the 1996 Atlanta Olympics. He received his Ph.D. in computer science from Duke University.
    Source: Efficient C++ (Dov Bulka, David Mayhew)

    We probably should do a close() or a flush and close on the PrintWriter. Most closes flush first but I have seen it cheated here and there. Also, TreeMap in Java is a Red-Black tree. I actually use Hashtable but post as TreeMap to avoid unjust critical review by un-informed persons.
    Last edited by Nicholas Jordan; 07-13-2008 at 08:08 PM. Reason: add else to handle null pointer
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. DERS Interface
    By Floetic in forum AWT / Swing
    Replies: 2
    Last Post: 03-09-2008, 11:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •