rt/vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java
changeset 772 d382dacfd73f
parent 714 ef50c4f07e4f
child 933 0cb657a2b888
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,132 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.vm4brwsr;
    1.22 +
    1.23 +import java.io.UnsupportedEncodingException;
    1.24 +
    1.25 +/**
    1.26 + *
    1.27 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.28 + */
    1.29 +public class StringSample {
    1.30 +    public static final String HELLO = "Hello World!";
    1.31 +    private static int counter;
    1.32 +    
    1.33 +    private final int cnt;
    1.34 +    public StringSample() {
    1.35 +        cnt = ++counter;
    1.36 +    }
    1.37 +    
    1.38 +    
    1.39 +    public static char sayHello(int indx) {
    1.40 +        return HELLO.charAt(indx);
    1.41 +    }
    1.42 +    
    1.43 +    public static boolean equalToHello(int from, int to) {
    1.44 +        return "Hello".equals(HELLO.substring(from, to));
    1.45 +    }
    1.46 +    
    1.47 +    public static String fromChars(char a, char b, char c) {
    1.48 +        char[] arr = { a, b, c };
    1.49 +        return new String(arr).toString();
    1.50 +    }
    1.51 +    
    1.52 +    public static String charsFromNumbers() {
    1.53 +        return chars((char)65, (char)66, (char)67);
    1.54 +    }
    1.55 +
    1.56 +    public static String charsFromChars() {
    1.57 +        return chars('A', 'B', 'C');
    1.58 +    }
    1.59 +
    1.60 +    public static String chars(char a, char b, char c) {
    1.61 +        return ("" + a + b +c).toString();
    1.62 +    }
    1.63 +    
    1.64 +    public static String replace(String s, char a, char b) {
    1.65 +        return s.replace(a, b);
    1.66 +    }
    1.67 +    
    1.68 +    public static int hashCode(String h) {
    1.69 +        return h.hashCode();
    1.70 +    }
    1.71 +    
    1.72 +    public static boolean isStringInstance() {
    1.73 +        return chars('a', (char)30, 'b') instanceof String;
    1.74 +    }
    1.75 +    
    1.76 +    public static String getBytes(String s) throws UnsupportedEncodingException {
    1.77 +        byte[] arr = s.getBytes("UTF-8");
    1.78 +        StringBuilder sb = new StringBuilder();
    1.79 +        for (int i = 0; i < arr.length; i++) {
    1.80 +            sb.append(arr[i]).append(" ");
    1.81 +        }
    1.82 +        return sb.toString().toString();
    1.83 +    }
    1.84 +    
    1.85 +    public static String insertBuffer() {
    1.86 +        StringBuilder sb = new StringBuilder();
    1.87 +        sb.append("Jardo!");
    1.88 +        sb.insert(0, "Ahoj ");
    1.89 +        sb.delete(4, 8);
    1.90 +        return sb.toString().toString();
    1.91 +    }
    1.92 +    
    1.93 +    public static int countAB(String txt) {
    1.94 +        int cnt = 0;
    1.95 +        for (int i = 0; i < txt.length(); i++) {
    1.96 +            switch (txt.charAt(i)) {
    1.97 +                case 'A': cnt++; break;
    1.98 +                case 'B': cnt += 2; break;
    1.99 +            }
   1.100 +        }
   1.101 +        return cnt;
   1.102 +    }
   1.103 +
   1.104 +    public static int stringSwitch(String txt) {
   1.105 +        switch (txt) {
   1.106 +            case "jedna": return 1;
   1.107 +            case "dve": return 2;
   1.108 +            case "tri": return 3;
   1.109 +            case "ctyri": return 4;
   1.110 +        }
   1.111 +        return -1;
   1.112 +    }
   1.113 +    
   1.114 +    public static String toStringTest(int howMuch) {
   1.115 +        counter = 0;
   1.116 +        StringSample ss = null;
   1.117 +        for (int i = 0; i < howMuch; i++) {
   1.118 +            ss = new StringSample();
   1.119 +        }
   1.120 +        return ss.toString().toString();
   1.121 +    }
   1.122 +    
   1.123 +    public static String concatStrings() {
   1.124 +        return (toStringTest(1) + "\\\n\r\t").toString();
   1.125 +    }
   1.126 +    
   1.127 +    public static int compare(String a, String b) {
   1.128 +        return a.compareTo(b);
   1.129 +    }
   1.130 +
   1.131 +    @Override
   1.132 +    public String toString() {
   1.133 +        return HELLO + cnt;
   1.134 +    }
   1.135 +}