Skip to content

Commit

Permalink
Data 1.0.7b
Browse files Browse the repository at this point in the history
  • Loading branch information
Petersburg2020 committed Mar 22, 2023
1 parent 7e23af7 commit c86cd53
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/main/java/nx/peter/java/util/data/Data.java
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,11 @@ public <O> int comparesTo(O data) {
return comparesTo(String.valueOf(data));
}

@Override
public <A extends IData> A castTo(Class<A> clazz) {
return DataCreator.createData(clazz, data, CharSet.English);
}

@Override
public int comparesTo(IData<?> data) {
return comparesTo(data != null ? data.get() : "");
Expand Down
85 changes: 85 additions & 0 deletions src/main/java/nx/peter/java/util/data/DataCreator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package nx.peter.java.util.data;

import java.util.Objects;

public class DataCreator {
private DataCreator() {}

public static <D extends IData> D createData(Class<D> clazz, CharSequence source, IData.CharSet charSet) {
return clazz.equals(Sentence.class) ? (D) new Sentence(charSet, source) :
clazz.equals(Paragraph.class) ? (D) new Paragraph(charSet, source) :
clazz.equals(Texts.class) ? (D) new Texts(charSet, source) :
clazz.equals(Word.class) ? (D) new Word(charSet, source) :
clazz.equals(Line.class) ? (D) new Line(1, source) :
clazz.equals(Number.class) ? (D) new Number(source) :
clazz.equals(Operator.class) ? (D) new Operator(source) :
clazz.equals(Subscript.class) ? (D) new Subscript(source) :
clazz.equals(Superscript.class) ? (D) new Subscript(source) :
clazz.equals(Alphabet.class) ? (D) new Alphabet(charSet, source) :
clazz.equals(Character.class) ? (D) new Character(source) :
clazz.equals(FractionData.class) ? (D) new FractionData(source) :
clazz.equals(Index.class) ? (D) new Index(source) : null;
}

public static <S extends ISentence> S createSentence(Class<S> clazz, CharSequence source, IData.CharSet charSet) {
return createLetters(clazz, source, charSet);
}

public static <L extends Letters> L createLetters(Class<L> clazz, CharSequence source, IData.CharSet charSet) {
return createData(clazz, source, charSet);
}

public static <L extends Letter> L createLetter(Class<L> clazz, CharSequence source, IData.CharSet charSet) {
return createData(clazz, source, charSet);
}

public static Alphabet createAlphabet(IData.CharSet charSet, CharSequence source) {
return createLetter(Alphabet.class, source, charSet);
}

public static Index createIndex(CharSequence source) {
return createData(Index.class, source, null);
}

public static FractionData createFraction(CharSequence source) {
return createData(FractionData.class, source, null);
}

public static Sentence createSentence(IData.CharSet charSet, CharSequence source) {
return createSentence(Sentence.class, source, charSet);
}

public static Character createCharacter(CharSequence source) {
return createLetter(Character.class, source, null);
}

public static Operator createOperator(CharSequence source) {
return createData(Operator.class, source, null);
}

public static Subscript createSubscript(CharSequence source) {
return createData(Subscript.class, source, null);
}

public static Superscript createSuperscript(CharSequence source) {
return createData(Superscript.class, source, null);
}

public static Paragraph createParagraph(IData.CharSet charSet, CharSequence source) {
return createSentence(Paragraph.class, source, charSet);
}

public static Texts createText(IData.CharSet charSet, CharSequence source) {
return createSentence(Texts.class, source, charSet);
}

public static Word createWord(IData.CharSet charSet, CharSequence source) {
return createLetters(Word.class, source, charSet);
}

public static Line createLine(int line, CharSequence source) {
return Objects.requireNonNull(createData(Line.class, source, null)).setNumber(line);
}


}
2 changes: 2 additions & 0 deletions src/main/java/nx/peter/java/util/data/IData.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ public interface IData<D extends IData> extends CharSequence, Comparable<D>, Ser

boolean contains(CharSequence data);

<A extends IData> A castTo(Class<A> clazz);

<O> int comparesTo(O data);

int comparesTo(IData<?> data);
Expand Down
7 changes: 6 additions & 1 deletion src/main/java/nx/peter/java/util/data/Letter.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ public L reset() {
setCharSet(CharSet.English);
return (L) this;
}


@Override
public <A extends IData> A castTo(Class<A> clazz) {
return DataCreator.createData(clazz, data, charSet);
}

public L set(char data) {
return super.set(data);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/nx/peter/java/util/data/Letters.java
Original file line number Diff line number Diff line change
Expand Up @@ -1286,6 +1286,10 @@ public int lastIndexOf(CharSequence letters) {
return data != null ? data.lastIndexOf(letters.toString()) : -1;
}

@Override
public <A extends IData> A castTo(Class<A> clazz) {
return DataCreator.createData(clazz, data, charSet);
}

public interface OnWordsExtractedListener {
void onExtracted(Words words, long durationInMillis);
Expand Down

0 comments on commit c86cd53

Please sign in to comment.