Results 1 to 12 of 12
Thread: [SOLVED] CharSequence interface
- 07-10-2008, 02:19 AM #1
- 07-10-2008, 03:14 AM #2
What happens when you try to compile it? Let the compiler give you the answer.
- 07-10-2008, 03:49 AM #3
yes, I know about that
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
- 07-10-2008, 05:44 AM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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. ;)
- 07-10-2008, 03:50 PM #5
a great help
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-10-2008, 04:50 PM #6
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.
Then to use the above: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 classJava 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 04:57 PM.
- 07-11-2008, 02:03 AM #7
preliminary implementation of your template
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.
The work here is first-draft prior to preliminary compiler pass.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
What is Q&D? Mabye: Archaeologists to Demonstrate Ancient BrewingIntroduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-11-2008, 02:30 AM #8
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
- 07-11-2008, 03:32 AM #9
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 BufferLast edited by Norm; 07-11-2008 at 03:44 AM.
- 07-11-2008, 03:04 PM #10
clean compile - ready for test harness
Aw gee, thought it meant quality and delivered homebrew.
Issues remain, and as well to design a test harness.Java Code:Tool completed successfully
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.
BTW: More on Collection FrameWork | Java Tips BlogJava 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 BufferLast edited by Nicholas Jordan; 07-11-2008 at 03:15 PM.
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 07-11-2008, 04:44 PM #11
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?
- 07-11-2008, 08:49 PM #12
read write area for session master
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("");// } }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.What is the purpose and use of the Buffer class? Why does it implement CharSequence?
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
Source: Efficient C++ (Dov Bulka, David Mayhew)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.
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 07: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
-
DERS Interface
By Floetic in forum AWT / SwingReplies: 2Last Post: 03-09-2008, 10:46 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks