Created
March 1, 2011 12:39
-
-
Save Cellane/849065 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.StringTokenizer; | |
/** | |
* Yes, the name of this class is not optimal | |
* But I've been naming all classes created by me by this convention | |
* since I started my studies on this university | |
* | |
* Search "Cviceni2" and replace it with "SuperAwesomeString" if you wish :-) | |
*/ | |
public class Cviceni2 { | |
private StringBuilder string = new StringBuilder (); | |
public Cviceni2 (StringBuilder string) { | |
this.string = string; | |
} | |
public Cviceni2 (char[] array) { | |
for (char character : array) { | |
string.append (character); | |
} | |
} | |
public void setString (StringBuilder string) { | |
this.string = string; | |
} | |
public String toString () { | |
return (string.toString ()); | |
} | |
public int getLength () { | |
return (string.length ()); | |
} | |
public int getWordCount () { | |
return (getWordCount (" ")); | |
} | |
public int getWordCount (String delimiter) { | |
StringTokenizer tokenizer = new StringTokenizer (this.toString (), delimiter); | |
return (tokenizer.countTokens ()); | |
} | |
public void removeDiactics () { | |
String string = this.toString (); | |
String[][] map = {{"á", "a"}, {"č", "c"}, {"ď", "d"}, {"é", "e",}, {"ě", "e"}, {"í", "i"}, | |
{"ň", "n"}, {"ó", "o"}, {"ř", "r"}, {"š", "s"}, {"ť", "t"}, {"ú", "u"}, {"ů", "u"}, | |
{"ý", "y"}, {"ž", "z"}}; | |
for (String[] replacement : map) { | |
string = string.replaceAll (replacement[0], replacement[1]); | |
string = string.replaceAll (replacement[0].toUpperCase (), replacement[1].toUpperCase ()); | |
} | |
this.setString (new StringBuilder (string)); | |
} | |
public void reverse () { | |
string.reverse (); | |
} | |
public String[] split () { | |
return (split (160)); | |
} | |
public String[] split (int count) { | |
StringBuilder part = new StringBuilder (); | |
StringTokenizer tokenizer = new StringTokenizer (this.toString (), " "); | |
String[] array = new String[(this.toString ().length () / count) + 1]; | |
String token; | |
int length = 0; | |
int currentPart = 0; | |
do { | |
token = tokenizer.nextToken (); | |
if ((length + token.length ()) < count) { | |
part.append (token).append (" "); | |
length += token.length () + 1; | |
} else { | |
if (part.charAt (part.length () - 1) == ' ') { | |
part.deleteCharAt (part.length () - 1); // remove last whitespace | |
} | |
array[currentPart] = part.toString (); | |
currentPart++; | |
length = 0; | |
part = new StringBuilder (token).append (" "); | |
} | |
} while (tokenizer.hasMoreTokens ()); | |
return (array); | |
} | |
public void compress () { | |
StringBuilder result = new StringBuilder (""); | |
StringTokenizer tokenizer = new StringTokenizer (this.toString ()); | |
boolean upperCase = true; | |
do { | |
if (upperCase) { | |
result.append (tokenizer.nextToken ().toUpperCase ()); | |
upperCase = false; | |
} else { | |
result.append (tokenizer.nextToken ().toLowerCase ()); | |
upperCase = true; | |
} | |
} while (tokenizer.hasMoreTokens ()); | |
this.setString (result); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.*; | |
import static org.junit.Assert.*; | |
public class Cviceni2Test { | |
public Cviceni2 instance; | |
public Cviceni2 instance2; | |
public Cviceni2 instance3; | |
public String firstPart, shortFirstPart; | |
public String secondPart, shortSecondPart; | |
public char[] array = {'A', 'h', 'ó', 'j'}; | |
@Before | |
public void setUp () throws Exception { | |
instance = new Cviceni2 (new StringBuilder ("Řetězec, řetězec")); | |
instance2 = new Cviceni2 (array); | |
instance3 = new Cviceni2 (new StringBuilder ("Lorem ipsum dolor sit amet, consectetur adipiscing elit. " + | |
"Sed a dolor sed urna auctor bibendum quis ut lacus. Nulla in enim quam, eget varius tellus. " + | |
"Quisque sed arcu ac nibh metus. Quisque volutpat molestie massa a mattis. Nam cursus, nulla " + | |
"quis commodo gravida, lacus ligula lobortis nisl, a placerat metus odio eu magna. Phasellus " + | |
"est metus, porta non nullam.")); | |
firstPart = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a dolor sed urna auctor bibendum " + | |
"quis ut lacus. Nulla in enim quam, eget varius tellus. Quisque"; | |
secondPart = "sed arcu ac nibh metus. Quisque volutpat molestie massa a mattis. Nam cursus, nulla quis " + | |
"commodo gravida, lacus ligula lobortis nisl, a placerat metus odio eu"; | |
shortFirstPart = "Lorem ipsum"; | |
shortSecondPart = "dolor sit amet,"; | |
} | |
@After | |
public void tearDown () throws Exception { | |
instance = null; | |
instance2 = null; | |
} | |
@Test | |
public void testToString () throws Exception { | |
assertEquals ("Řetězec, řetězec", instance.toString ()); | |
assertEquals ("Ahój", instance2.toString ()); | |
} | |
@Test | |
public void testGetLength () throws Exception { | |
assertEquals (16, instance.getLength ()); | |
assertEquals (4, instance2.getLength ()); | |
} | |
@Test | |
public void testGetWordCount () throws Exception { | |
assertEquals (2, instance.getWordCount ()); | |
assertEquals (1, instance2.getWordCount ()); | |
assertEquals (5, instance.getWordCount ("e")); | |
assertEquals (2, instance2.getWordCount ("ó")); | |
} | |
@Test | |
public void testRemoveDiacritics () throws Exception { | |
instance.removeDiactics (); | |
instance2.removeDiactics (); | |
assertEquals ("Retezec, retezec", instance.toString ()); | |
assertEquals ("Ahoj", instance2.toString ()); | |
} | |
@Test | |
public void testReverse () throws Exception { | |
instance.reverse (); | |
instance2.reverse (); | |
assertEquals ("cezěteř ,cezěteŘ", instance.toString ()); | |
assertEquals ("jóhA", instance2.toString ()); | |
} | |
@Test | |
public void testSplit () throws Exception { | |
assertEquals (firstPart, instance3.split ()[0]); | |
assertEquals (secondPart, instance3.split ()[1]); | |
assertEquals (shortFirstPart, instance3.split (15)[0]); | |
assertEquals (shortSecondPart, instance3.split (15)[1]); | |
} | |
@Test | |
public void testCompress () throws Exception { | |
instance.compress (); | |
assertEquals ("ŘETĚZEC,řetězec", instance.toString ()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment