Create CharType.forCharacter() method

This commit is contained in:
Zafar Khaja 2014-01-28 12:45:15 +04:00
parent 7d8b74608f
commit da3ce7c65f
2 changed files with 27 additions and 0 deletions

View File

@ -128,6 +128,21 @@ class VersionParser implements Parser<Version> {
return true;
}
};
/**
* Gets the type for a given character.
*
* @param chr the character to get the type for
* @return the type of the specified character
*/
static CharType forCharacter(Character chr) {
for (CharType type : values()) {
if (type.isMatchedBy(chr)) {
return type;
}
}
return null;
}
}
/**

View File

@ -23,6 +23,7 @@
*/
package com.github.zafarkhaja.semver;
import com.github.zafarkhaja.semver.VersionParser.CharType;
import org.junit.Test;
import static com.github.zafarkhaja.semver.VersionParser.CharType.*;
import static org.junit.Assert.*;
@ -88,4 +89,15 @@ public class VersionParserCharTypeTest {
assertFalse(ILLEGAL.isMatchedBy('a'));
assertFalse(ILLEGAL.isMatchedBy('0'));
}
@Test
public void shouldReturnCharTypeForCharacter() {
assertEquals(DIGIT, CharType.forCharacter('1'));
assertEquals(LETTER, CharType.forCharacter('a'));
assertEquals(DOT, CharType.forCharacter('.'));
assertEquals(HYPHEN, CharType.forCharacter('-'));
assertEquals(PLUS, CharType.forCharacter('+'));
assertEquals(EOL, CharType.forCharacter(null));
assertEquals(ILLEGAL, CharType.forCharacter('!'));
}
}