rt/vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 26 Feb 2013 16:54:16 +0100
changeset 772 d382dacfd73f
parent 714 vm/src/test/java/org/apidesign/vm4brwsr/StringSample.java@ef50c4f07e4f
child 933 0cb657a2b888
permissions -rw-r--r--
Moving modules around so the runtime is under one master pom and can be built without building other modules that are in the repository
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, version 2 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://opensource.org/licenses/GPL-2.0.
    17  */
    18 package org.apidesign.vm4brwsr;
    19 
    20 import java.io.UnsupportedEncodingException;
    21 
    22 /**
    23  *
    24  * @author Jaroslav Tulach <jtulach@netbeans.org>
    25  */
    26 public class StringSample {
    27     public static final String HELLO = "Hello World!";
    28     private static int counter;
    29     
    30     private final int cnt;
    31     public StringSample() {
    32         cnt = ++counter;
    33     }
    34     
    35     
    36     public static char sayHello(int indx) {
    37         return HELLO.charAt(indx);
    38     }
    39     
    40     public static boolean equalToHello(int from, int to) {
    41         return "Hello".equals(HELLO.substring(from, to));
    42     }
    43     
    44     public static String fromChars(char a, char b, char c) {
    45         char[] arr = { a, b, c };
    46         return new String(arr).toString();
    47     }
    48     
    49     public static String charsFromNumbers() {
    50         return chars((char)65, (char)66, (char)67);
    51     }
    52 
    53     public static String charsFromChars() {
    54         return chars('A', 'B', 'C');
    55     }
    56 
    57     public static String chars(char a, char b, char c) {
    58         return ("" + a + b +c).toString();
    59     }
    60     
    61     public static String replace(String s, char a, char b) {
    62         return s.replace(a, b);
    63     }
    64     
    65     public static int hashCode(String h) {
    66         return h.hashCode();
    67     }
    68     
    69     public static boolean isStringInstance() {
    70         return chars('a', (char)30, 'b') instanceof String;
    71     }
    72     
    73     public static String getBytes(String s) throws UnsupportedEncodingException {
    74         byte[] arr = s.getBytes("UTF-8");
    75         StringBuilder sb = new StringBuilder();
    76         for (int i = 0; i < arr.length; i++) {
    77             sb.append(arr[i]).append(" ");
    78         }
    79         return sb.toString().toString();
    80     }
    81     
    82     public static String insertBuffer() {
    83         StringBuilder sb = new StringBuilder();
    84         sb.append("Jardo!");
    85         sb.insert(0, "Ahoj ");
    86         sb.delete(4, 8);
    87         return sb.toString().toString();
    88     }
    89     
    90     public static int countAB(String txt) {
    91         int cnt = 0;
    92         for (int i = 0; i < txt.length(); i++) {
    93             switch (txt.charAt(i)) {
    94                 case 'A': cnt++; break;
    95                 case 'B': cnt += 2; break;
    96             }
    97         }
    98         return cnt;
    99     }
   100 
   101     public static int stringSwitch(String txt) {
   102         switch (txt) {
   103             case "jedna": return 1;
   104             case "dve": return 2;
   105             case "tri": return 3;
   106             case "ctyri": return 4;
   107         }
   108         return -1;
   109     }
   110     
   111     public static String toStringTest(int howMuch) {
   112         counter = 0;
   113         StringSample ss = null;
   114         for (int i = 0; i < howMuch; i++) {
   115             ss = new StringSample();
   116         }
   117         return ss.toString().toString();
   118     }
   119     
   120     public static String concatStrings() {
   121         return (toStringTest(1) + "\\\n\r\t").toString();
   122     }
   123     
   124     public static int compare(String a, String b) {
   125         return a.compareTo(b);
   126     }
   127 
   128     @Override
   129     public String toString() {
   130         return HELLO + cnt;
   131     }
   132 }