001    package example;
002    
003    /**
004     * The class to demonstrate ComTest as a test and design tool.
005     * In this example we have a bit more side effect that makes
006     * the testing more complex.
007     * 
008     * Suppose our problem is to do some linguistic research 
009     * and find what are the  most popular three first vowels from
010     * every verse in Finish national poetry Kalevala.
011     * The first song of Kalevala begins like:
012     * <pre>
013     * Mieleni minun tekevi, aivoni ajattelevi
014     * lähteäni laulamahan, saa'ani sanelemahan,
015     * sukuvirttä suoltamahan, lajivirttä laulamahan.
016     * Sanat suussani sulavat, puhe'et putoelevat,
017     * kielelleni kerkiävät, hampahilleni hajoovat.
018     * </pre>
019     * At the beginning we will need to read the file kalevala.txt
020     * and then take every verse and extract there 3 first vowels. 
021     * To extract the 3 first vowels we might need a function
022     * extract3FirstVowels(s).  But to make it a bit more generic
023     * we may send the number 3 as a parameter.  And to make it more general
024     * we might send also the list of chars to extract as a parameter.
025     * So we need a function extractChars(s,n,letters)
026     * @author vesal
027     * @version 8.6.2010
028     * 
029     *
030     */
031    public class TakeChars {
032    
033            /**
034             * Take from s the n first chars that are in the list of letters and return them.
035             * @param s where to take the letters
036             * @param n how many letters to take at max
037             * @param letters set of letters we are looking
038             * @return first chars belonging to set of letters      
039             * 
040             * @example
041             * <pre name="test">
042             *   StringBuilder s = new StringBuilder("Mieleni minun tekevi");
043             *   extractChars(s,3,"aeiouyäåö") === "iee"; s.toString() === "Mlni minun tekevi";
044             *   extractChars(s,3,"xyz") === "";          s.toString() === "Mlni minun tekevi";
045             *   extractChars(s,4,"klmn") === "Mlnm";     s.toString() === "i inun tekevi";
046             *   extractChars(s,4,"e") === "ee";          s.toString() === "i inun tkvi";
047             * </pre>
048             */
049            public static String extractChars(StringBuilder s, int n, String letters) {
050                    StringBuilder result = new StringBuilder();
051                    String uletters = letters; //.toUpperCase();
052                    int i = 0;
053                    while ( i < s.length() && result.length() < n ) {
054                            char c = s.charAt(i);
055                            //if ( uletters.indexOf(Character.toUpperCase(c)) >= 0 ) { // found the letter
056                            if ( uletters.indexOf(c) >= 0 ) { // found the letter
057                                    result.append(c);
058                                    s.deleteCharAt(i);
059                            } else { // not found, move the next one
060                                    i++;
061                            }
062                    }
063                    return result.toString();
064            }
065            
066            /**
067             * @param args not used
068             */
069            public static void main(String[] args) {
070                    StringBuilder s = new StringBuilder("Mieleni minun tekevi");
071                    String vowels3 = extractChars(s,3,"aeiouyåäö");
072                    System.out.println("Three first vowels are " + vowels3 +
073                                    "and there are left " + s);
074            }
075    
076    }