Hello everyone!
Background story:
There are these Source and Result interfaces for XML. These are adapters between different XML technologies in Java. Instances of these classes represent DOM, SAX, JAXB, XML streams, XML events (and even more?).
The question:
So, is there something comparable for plain old strings? Some generalization between the following?
[Input|Output]Stream
Reader|Writer
StringBuffer
StringBuilder
CharBuffer
(from NIO)File
(orPath
for the JDK7-fans among us)- (and finally)
CharSequence
Perhaps there is some common API (Apache commons something...?) which provides such functionality?
Clarifying example:
Usage with classic approach:
An interface needs to be able to read (write) characters from (to) all possible sources (results):
interface SomeInterface {
readFrom(CharacterSequence source);
readFrom(InputStream source);
readFrom(Reader source);
readFrom(File source);
// ...
writeTo(CharacterSequence result);
writeTo(OutputStream result);
writeTo(Writer result);
writeTo(File result);
// ...
}
Usage with intended approach:
With some imaginary CharacterSource
and CharacterResult
interfaces, read/write now possible with one method each:
interface SomeInterface {
readFrom(CharacterSource source);
writeTo(CharacterResult result);
}
Intended approach implementation, possible hierarchy:
interface CharacterSource
+ class CharBufferSource
+ class InputStreamSource
+ class ReaderSource
+ class FileSource
+ ...
interface CharacterResult
+ class CharBufferResult
+ class OutputStreamResult
+ class WriterResult
+ class FileResult
+ ...
If such functionality is not present, should I write an own mini-API? (for a larger API, I'm currently involved at)
What's about this?