Make refactoring, small improvements

This commit is contained in:
Zafar Khaja 2014-02-03 10:05:26 +04:00
parent 4ffac6d39f
commit 229a732976

View File

@ -309,8 +309,8 @@ class VersionParser implements Parser<Version> {
*/ */
private MetadataVersion parsePreRelease() { private MetadataVersion parsePreRelease() {
List<String> idents = new ArrayList<String>(); List<String> idents = new ArrayList<String>();
CharType end = closestEndpoint(PLUS, EOL); CharType end = nearestCharType(PLUS, EOL);
CharType before = closestEndpoint(DOT, end); CharType before = nearestCharType(DOT, end);
do { do {
checkForEmptyIdentifier(); checkForEmptyIdentifier();
if (chars.positiveLookaheadBefore(before, LETTER, HYPHEN)) { if (chars.positiveLookaheadBefore(before, LETTER, HYPHEN)) {
@ -320,7 +320,7 @@ class VersionParser implements Parser<Version> {
} }
if (before == DOT) { if (before == DOT) {
chars.consume(DOT); chars.consume(DOT);
before = closestEndpoint(DOT, end); before = nearestCharType(DOT, end);
} }
} while (!chars.positiveLookahead(end)); } while (!chars.positiveLookahead(end));
return new MetadataVersion(idents.toArray(new String[idents.size()])); return new MetadataVersion(idents.toArray(new String[idents.size()]));
@ -347,7 +347,7 @@ class VersionParser implements Parser<Version> {
private MetadataVersion parseBuild() { private MetadataVersion parseBuild() {
List<String> idents = new ArrayList<String>(); List<String> idents = new ArrayList<String>();
CharType end = EOL; CharType end = EOL;
CharType before = closestEndpoint(DOT, end); CharType before = nearestCharType(DOT, end);
do { do {
checkForEmptyIdentifier(); checkForEmptyIdentifier();
if (chars.positiveLookaheadBefore(before, LETTER, HYPHEN)) { if (chars.positiveLookaheadBefore(before, LETTER, HYPHEN)) {
@ -357,7 +357,7 @@ class VersionParser implements Parser<Version> {
} }
if (before == DOT) { if (before == DOT) {
chars.consume(DOT); chars.consume(DOT);
before = closestEndpoint(DOT, end); before = nearestCharType(DOT, end);
} }
} while (!chars.positiveLookahead(end)); } while (!chars.positiveLookahead(end));
return new MetadataVersion(idents.toArray(new String[idents.size()])); return new MetadataVersion(idents.toArray(new String[idents.size()]));
@ -425,17 +425,20 @@ class VersionParser implements Parser<Version> {
} }
/** /**
* Chooses the closest character. * Finds the nearest character type.
* *
* @param tryThis the character to try first * @param types the character types to choose from
* @param orThis the character to fallback to * @return the nearest character type or {@code EOL}
* @return the closest character
*/ */
private CharType closestEndpoint(CharType tryThis, CharType orThis) { private CharType nearestCharType(CharType... types) {
if (chars.positiveLookaheadBefore(orThis, tryThis)) { for (Character chr : chars) {
return tryThis; for (CharType type : types) {
if (type.isMatchedBy(chr)) {
return type;
}
}
} }
return orThis; return EOL;
} }
/** /**