Skip to content
This repository was archived by the owner on Sep 24, 2022. It is now read-only.

Commit e976971

Browse files
committed
initial commit
0 parents  commit e976971

6 files changed

Lines changed: 202 additions & 0 deletions

File tree

.github/workflows/packages.yml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: GitHub Packages
2+
on: {release: {types: [published]}}
3+
jobs:
4+
packages:
5+
runs-on: ubuntu-latest
6+
timeout-minutes: 2
7+
steps:
8+
9+
- uses: actions/checkout@v3
10+
11+
- uses: actions/setup-java@v3
12+
with:
13+
distribution: temurin
14+
java-version: '17'
15+
cache: maven
16+
server-id: github
17+
18+
- name: Package with Maven
19+
run: mvn -batch-mode package --file pom.xml
20+
21+
- name: Publish to GitHub Packages
22+
run: mvn deploy
23+
env: {GITHUB_TOKEN: "${{github.token}}"}

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# JetBrains
2+
/.idea/
3+
4+
# Java
5+
/target/

LICENSE.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
Boost Software License - Version 1.0 - August 17th, 2003
2+
3+
Permission is hereby granted, free of charge, to any person or organization
4+
obtaining a copy of the software and accompanying documentation covered by
5+
this license (the "Software") to use, reproduce, display, distribute,
6+
execute, and transmit the Software, and to prepare derivative works of the
7+
Software, and to permit third-parties to whom the Software is furnished to
8+
do so, all subject to the following:
9+
10+
The copyright notices in the Software and this entire statement, including
11+
the above license grant, this restriction and the following disclaimer,
12+
must be included in all copies of the Software, in whole or in part, and
13+
all derivative works of the Software, unless such copies or derivative
14+
works are solely in the form of machine-executable object code generated by
15+
a source language processor.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
20+
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
21+
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
22+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23+
DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# `MyString`
2+
3+
A Java
4+
[`CharSequence`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/CharSequence.html)
5+
implementation backed by a
6+
[`Character`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/Character.html)
7+
[`List`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/util/List.html)
8+
9+
The backing list determines whether the instance is mutable (i.e. writeable).
10+
11+
12+
## About
13+
14+
This is the difference between `CharSequence` and
15+
[`String`](https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/lang/String.html)
16+
in a concrete piece of code.
17+
18+
I originally prepared this to keep my Java experience fresh, but as the project develops,
19+
[JetBrains annotations](https://github.com/JetBrains/java-annotations),
20+
[Maven (`pom.xml`)](https://maven.apache.org/pom.html) and
21+
[GitHub Actions & Packages](https://docs.github.com/en/actions/publishing-packages/publishing-java-packages-with-maven#publishing-packages-to-github-packages)
22+
joined the party too.
23+
24+
25+
## License
26+
27+
Copyright ParadoxV5 2022. Distributed under the
28+
[Boost Software License, Version 1.0](https://www.boost.org/users/license.html).

pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<project
3+
xmlns="http://maven.apache.org/POM/4.0.0"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
6+
>
7+
<modelVersion>4.0.0</modelVersion>
8+
<groupId>xyz.paradoxv5</groupId>
9+
<artifactId>mystring</artifactId>
10+
<name>mystring</name>
11+
<version>1.0</version>
12+
13+
<developers><developer>
14+
<id>ParadoxV5</id>
15+
<url>https://github.com/ParadoxV5/</url>
16+
</developer></developers>
17+
<licenses><license>
18+
<name>BSL-1.0</name>
19+
<comments>Boost Software License 1.0</comments>
20+
<url>https://www.boost.org/users/license.html</url>
21+
</license></licenses>
22+
23+
<build>
24+
<sourceDirectory>src</sourceDirectory>
25+
<testSourceDirectory>test</testSourceDirectory>
26+
<plugins><plugin>
27+
<groupId>org.apache.maven.plugins</groupId>
28+
<artifactId>maven-compiler-plugin</artifactId>
29+
<version>3.10.1</version>
30+
<configuration><release>17</release></configuration>
31+
</plugin></plugins>
32+
</build>
33+
<properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties>
34+
35+
<dependencies><dependency>
36+
<groupId>org.jetbrains</groupId>
37+
<artifactId>annotations</artifactId>
38+
<version>23.0.0</version>
39+
</dependency></dependencies>
40+
41+
<scm>
42+
<developerConnection>scm:git:https://github.com/ParadoxV5/java-mystring.git</developerConnection>
43+
<url>https://github.com/ParadoxV5/java-mystring</url>
44+
</scm>
45+
<distributionManagement><repository>
46+
<id>github</id>
47+
<name>GitHub Packages</name>
48+
<url>https://maven.pkg.github.com/ParadoxV5/java-mystring</url>
49+
</repository></distributionManagement>
50+
51+
</project>

src/xyz/paradoxv5/MyString.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

Comments
 (0)