1+ package xyz .paradoxv5 ;
2+ import org .jetbrains .annotations .*;
3+ import java .util .*;
4+
5+ /**
6+ A {@link CharSequence} implementation backed by {@linkplain #chars a Character List}
7+ <p>
8+ The backing list determines whether the instance is mutable (i.e. writeable).
9+
10+ @author
11+ <a href="https://github.com/ParadoxV5/">ParadoxV5</a>.
12+ <a href="https://www.boost.org/LICENSE_1_0.txt">Boost Software License 1.0</a>
13+ @version
14+ {@value #serialVersionUID}.0
15+ */
16+ public class MyString implements CharSequence , Comparable <@ NotNull CharSequence >, Cloneable , java .io .Serializable {
17+ @ java .io .Serial private static final long serialVersionUID = 1 ;
18+
19+ private @ NotNull List <Character > chars ;
20+ @ Contract (value = "-> _" , pure = true )
21+ public @ NotNull List <Character > getChars () { return chars ; }
22+ @ Contract (mutates ="this" )
23+ public void setChars (@ NotNull List <Character > chars ) { this .chars = Objects .requireNonNull (chars ); }
24+ @ Contract (mutates ="this" )
25+ public MyString (@ NotNull List <Character > chars ) { this .chars = Objects .requireNonNull (chars ); }
26+
27+ @ Contract (mutates ="this" ) public MyString (@ NotNull CharSequence charSeq ) {
28+ this (charSeq instanceof MyString myString
29+ ? myString .getChars ()
30+ : charSeq .chars ().mapToObj (e -> Character .valueOf ((char )e )).toList ()
31+ );
32+ }
33+ @ Contract (mutates ="this" ) public MyString (char @ NotNull ... charArray ) {
34+ this (String .valueOf (charArray ));
35+ }
36+
37+ @ Contract (value = "_ -> _" , pure = true ) @ Override
38+ public char charAt (int index ) { return getChars ().get (index ).charValue (); }
39+ @ Contract (value = "-> _" , pure = true ) @ Override
40+ public int length () { return getChars ().size (); }
41+ @ Contract (value = "_, _ -> new" , pure = true ) @ Override
42+ public MyString subSequence (int start , int end ) { return new MyString (getChars ().subList (start , end )); }
43+ @ Contract (value = "_ -> _" , pure = true ) @ Override
44+ public int compareTo (@ NotNull CharSequence other ) { return CharSequence .compare (this , other ); }
45+
46+ @ Contract (value = "-> new" , pure = true ) public char @ NotNull [] toCharArray () {
47+ List <Character > chars = getChars ();
48+ char [] charArray = new char [chars .size ()];
49+ @ NotNull ListIterator <Character > iter = chars .listIterator ();
50+ while (iter .hasNext ())
51+ charArray [iter .nextIndex ()] = iter .next ().charValue ();
52+ return charArray ;
53+ }
54+ @ Contract (value = "-> _" , pure = true ) @ Override public @ NotNull String toString () {
55+ return String .valueOf (toCharArray ());
56+ }
57+ @ Contract (value = "-> new" , pure = true ) public MyString deepClone () {
58+ return new MyString (new LinkedList <>(getChars ()));
59+ }
60+
61+ @ Contract (value = "null -> false; !null -> _" , pure = true ) @ Override
62+ public boolean equals (@ Nullable Object obj ) {
63+ return super .equals (obj )
64+ || obj instanceof MyString other
65+ && getChars ().equals (other .getChars ());
66+ }
67+ @ Contract (value = "-> _" , pure = true ) @ Override public int hashCode () {
68+ return getChars ().hashCode ();
69+ }
70+ @ Contract (value = "-> new" , pure = true ) @ Override
71+ public @ NotNull MyString clone () throws CloneNotSupportedException { return (MyString )(super .clone ()); }
72+ }
0 commit comments