Skip to content

Instantly share code, notes, and snippets.

@stefanofago73
Last active February 10, 2023 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanofago73/0becfdba21fedf302fd8ffad14e6075b to your computer and use it in GitHub Desktop.
Save stefanofago73/0becfdba21fedf302fd8ffad14e6075b to your computer and use it in GitHub Desktop.
Reasoning about CharSequence: my weird dreams.
Starting point:
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/CharSequence.html
Points:
- This interface provides uniform, read-only access to many different kinds of char sequences.
- This interface does not refine the general contracts of the equals and hashCode methods. The result of testing two objects that implement CharSequence for equality is therefore, in general, undefined.
- toString method: Returns a string containing the characters in this sequence in the same order as this sequence.
So... Those point, in someway, dont' make me happy also if I consider what are the implementation... CharBuffer, StringBuilder, String...
(maybe String is the more coherent part)... So if I want only a char sequence as snapshot of reversed sequence of original one? If I want
a sequence as snapshot of only wovel from original sequence? And so on...
Why CharSequence.toString need to be a string containing the characters in this sequence in the same order as this sequence
instead of the classical JVM object representation?
Ok... toString note about the API, report:
<< ...In general, the toString method returns a string that "textually represents" this object. The result should be a
concise but informative representation that is easy for a person to read. It is recommended that all subclasses
override this method. The string output is not necessarily stable over time or across JVM invocations... >>
...but something doesn't work for me: i prefer to separate what is JVM related from what is a representation choosen by Dev...
Other then this, toString and other object method can't be overriden by default method or lambda...
..and so a "stupid" question... A CharSequence, can be a lambda?
The immediate response is no:
- it's not SAM compliant
- maybe you need to change Object method ( toString? and you can't)
- normally, also in the case of snapshot you can use a inner/anonymous class
Can we do something about it?
Maybe but, as often happen, can be weird...
We can play mixing with Formattable or Supplier...
Long story short... a first frankenstein can be what follow:
interface ExcCharSequence
{
interface Stringifier extends Supplier<char[]>
{
default String stringify()
{
return String.valueOf(this.get());
}
}
public static String asString(CharSequence sequence)
{
return sequence instanceof ExcCharSequence.Stringifier?
((ExcCharSequence.Stringifier)sequence).stringify():
sequence.toString();
}
public static CharSequence from(String data)
{
return from(data.toCharArray());
}
public static CharSequence from(char [] data)
{
var buffer = data.clone();
interface ICharSequence extends CharSequence, Supplier<char[]>
{
@Override
default CharSequence subSequence(int start, int end) {
// TODO a real implementation
return (ICharSequence)()-> new char[end-start];
}
//
// WARNING: lenght need to be related to the effective length of the
// sequence, since chars (transform sequence in streams) use
// internally lenght() method... so play fair!...
//
@Override
default int length() {
return get().length;
}
default char charAt(int index)
{
return get()[index];
}
}
return ((ICharSequence&Stringifier)()->buffer);
}
}
public class MyClass {
public static void main(String args[]) {
var elem = ExcCharSequence.from("HELLO!");
System.out.printf("%s\n", ExcCharSequence.asString(elem));
}
}//END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment