br.com.objectos.lang.ToJava Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of objectos-lang Show documentation
Show all versions of objectos-lang Show documentation
A set of utilities for classes of the java.lang and the java.util packages.
The newest version!
/*
* Copyright 2021 Objectos Software LTDA.
*
* Reprodução parcial ou total proibida.
*/
package br.com.objectos.lang;
import static br.com.objectos.lang.Lang.checkNotNull;
abstract class ToJava {
static final ToJava TO_JAVA_IDENTIFIER = new ToJavaIdentifier();
static final ToJava TO_JAVA_UPPER_NAME = new ToJavaName(State.UPPER_NAME_START);
static final ToJava TO_JAVA_VARIABLE_NAME = new ToJavaName(State.VARIABLE_NAME_START);
private final State startingState;
ToJava(State startingState) {
this.startingState = startingState;
}
public final String apply(String string) {
checkNotNull(string, "string == null");
if (string.isEmpty()) {
return "";
}
State state;
state = startingState;
int[] points;
points = Strings.toCodePoints(string);
StringBuilder result;
result = new StringBuilder(points.length);
for (int point : points) {
state = consume(state, point, result);
}
return result.toString();
}
abstract State consume(State state, int point, StringBuilder result);
private enum State {
IDENTIFIER_PART,
IDENTIFIER_START,
NAME_PART,
NAME_UPPER,
UPPER_NAME_START,
VARIABLE_NAME_START;
}
private static class ToJavaIdentifier extends ToJava {
ToJavaIdentifier() {
super(State.IDENTIFIER_START);
}
@Override
final State consume(State state, int point, StringBuilder result) {
switch (state) {
case IDENTIFIER_PART:
if (Character.isJavaIdentifierPart(point)) {
result.appendCodePoint(point);
}
return state;
case IDENTIFIER_START:
if (Character.isJavaIdentifierStart(point)) {
result.appendCodePoint(point);
return State.IDENTIFIER_PART;
} else {
return state;
}
default:
throw new UnsupportedOperationException("Implement me @ " + state);
}
}
}
private static class ToJavaName extends ToJava {
ToJavaName(State startingState) {
super(startingState);
}
@Override
final State consume(State state, int point, StringBuilder result) {
int converted;
switch (state) {
case UPPER_NAME_START:
if (Character.isJavaIdentifierStart(point)) {
converted = Character.toUpperCase(point);
result.appendCodePoint(converted);
return State.NAME_PART;
} else {
return state;
}
case NAME_PART:
if (Character.isJavaIdentifierPart(point)) {
result.appendCodePoint(point);
return state;
} else {
return State.NAME_UPPER;
}
case NAME_UPPER:
if (Character.isJavaIdentifierPart(point)) {
converted = Character.toUpperCase(point);
result.appendCodePoint(converted);
return State.NAME_PART;
} else {
return state;
}
case VARIABLE_NAME_START:
if (Character.isJavaIdentifierStart(point)) {
converted = Character.toLowerCase(point);
result.appendCodePoint(converted);
return State.NAME_PART;
} else {
return state;
}
default:
throw new UnsupportedOperationException("Implement me @ " + state);
}
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy