- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Question on "inner Objects" (as opposed to "inner Classes")
Tue, 2011-05-17, 20:28
I'm defining a Document class, and two characteristics of a Document instance are that it should have exactly one Cursor, and exactly one Root (which is basically a range of the document that encompasses the entire document.) In my current coding, my handling those cases by declaring Cursor and Root as inner Objects, not classes. However, I still have a fair bit of coding to do before I can really try anything that will test if this works or not, so I thought I would post the relevant code to see if anyone can tell me if I might be shooting myself in the foot.
I've edited out most of the actual "action" code, what's left are (commented) classes/objects that should, I hope, be easy to understand.
/** A data structure containing all of the content of a document. */class Document { private val contents = new ArrayBuffer[Node]
/** A Node is a single entry in the document; a character, image, or some other "non-divisible" piece of information. */ class Node { def text = "" def length = 0 }
/** A single character in a document. */ class Char(override val text: String) extends Node { assert(text.length == 1) override def length = text.length }
/**A Marker indicates a location in the document, but does not possess any content itself. * Markers are used as a base classes for other classes that also indicate a location in * the document but add additional functionality */ abstract class Marker extends Node
/**A Position is a location in a document that can be used in various ways. Positions can be * created, inserted, moved, or used as the basis for operations such as inserting content. */ class Position extends Marker { /**Insert a string either before or after this Position. * @param str The string to be inserted. * @param before True (the default) if the string is to be inserted before the * Position, false if the string is to be inserted after the Position. The default * cause corresponds to typing and expecting the Position (cursor) to end at the end * of the typed text. */ def insert(str: String, before: Boolean = true) { } }
/** There is exactly one Cursor associated with each Document object Cursor extends Position
/**A Fragment denotes a structured piece of the document. It possesses Start and End markers * that indicate where in the Document the Fragment content starts and ends. */ class Fragment { val start = new Start val end = new End
/**A Start is a Marker that indicates a location in the document where some fragment * starts. */ class Start extends Marker /**A End is a Marker that indicates a location in the document where some fragment * end. */ class End extends Marker }
/**A Root is a special Fragment. There is exactly one Root per Document, and its Start marker is at * the start of the Document's contents, while its End marker is at the end of the Document's contents. */ object Root extends Fragment}
As you can see, the only top-level class is Document. Feel free to point out other mistakes I might have made, but my big questions right now are, can I create Cursor and Root in this manner, and then (presumably) refer to them as Cursor and Root to invoke methods on them?
Thanks,Ken
I've edited out most of the actual "action" code, what's left are (commented) classes/objects that should, I hope, be easy to understand.
/** A data structure containing all of the content of a document. */class Document { private val contents = new ArrayBuffer[Node]
/** A Node is a single entry in the document; a character, image, or some other "non-divisible" piece of information. */ class Node { def text = "" def length = 0 }
/** A single character in a document. */ class Char(override val text: String) extends Node { assert(text.length == 1) override def length = text.length }
/**A Marker indicates a location in the document, but does not possess any content itself. * Markers are used as a base classes for other classes that also indicate a location in * the document but add additional functionality */ abstract class Marker extends Node
/**A Position is a location in a document that can be used in various ways. Positions can be * created, inserted, moved, or used as the basis for operations such as inserting content. */ class Position extends Marker { /**Insert a string either before or after this Position. * @param str The string to be inserted. * @param before True (the default) if the string is to be inserted before the * Position, false if the string is to be inserted after the Position. The default * cause corresponds to typing and expecting the Position (cursor) to end at the end * of the typed text. */ def insert(str: String, before: Boolean = true) { } }
/** There is exactly one Cursor associated with each Document object Cursor extends Position
/**A Fragment denotes a structured piece of the document. It possesses Start and End markers * that indicate where in the Document the Fragment content starts and ends. */ class Fragment { val start = new Start val end = new End
/**A Start is a Marker that indicates a location in the document where some fragment * starts. */ class Start extends Marker /**A End is a Marker that indicates a location in the document where some fragment * end. */ class End extends Marker }
/**A Root is a special Fragment. There is exactly one Root per Document, and its Start marker is at * the start of the Document's contents, while its End marker is at the end of the Document's contents. */ object Root extends Fragment}
As you can see, the only top-level class is Document. Feel free to point out other mistakes I might have made, but my big questions right now are, can I create Cursor and Root in this manner, and then (presumably) refer to them as Cursor and Root to invoke methods on them?
Thanks,Ken