vm/src/test/java/org/apidesign/vm4brwsr/Array.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Mon, 14 Jan 2013 14:20:49 +0100
brancharithmetic
changeset 446 5b3d5ed03014
parent 402 1fb46c65f030
child 457 b0e82dcf51fb
permissions -rw-r--r--
Fill object arrays with null, otherwise use 0
     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 /**
    21  *
    22  * @author Jaroslav Tulach <jtulach@netbeans.org>
    23  */
    24 public class Array {
    25     byte[] bytes = { 1 };
    26     short[] shorts = { 2, 3 };
    27     int[] ints = { 4, 5, 6 };
    28     float[] floats = { 7, 8, 9, 10 };
    29     double[][] doubles = { {11}, {12}, {13}, {14}, {15} };
    30     char[] chars = { 'a', 'b' };
    31     
    32     private Array() {
    33     }
    34     
    35     byte bytes() {
    36         return bytes[0];
    37     }
    38     short shorts() {
    39         return shorts[1];
    40     }
    41     
    42     int[] ints() {
    43         return ints;
    44     }
    45     
    46     float floats() {
    47         return floats[3];
    48     }
    49     
    50     double doubles() {
    51         return doubles[4][0];
    52     }
    53     
    54     private static final Array[] ARR = { new Array(), new Array(), new Array() };
    55     
    56     private static Array[][] arr() {
    57         Array[][] matrix = new Array[3][3];
    58         for (int i = 0; i < ARR.length; i++) {
    59             matrix[i][i] = ARR[i];
    60         }
    61         return matrix;
    62     }
    63     private static <T> T[] filter(T[] in) {
    64         return in;
    65     }
    66     
    67     public static double sum() {
    68         double sum = 0.0;
    69         for (Array[] row : arr()) {
    70             int indx = -1;
    71             for (Array a : row) {
    72                 indx++;
    73                 if (a == null) {
    74                     continue;
    75                 }
    76                 sum += a.bytes();
    77                 sum += a.shorts();
    78                 sum += a.ints()[2];
    79                 sum += a.floats();
    80                 sum += filter(row)[indx].doubles();
    81             }
    82         }
    83         return sum;
    84     }
    85     private static final int[] arr = { 0, 1, 2, 3, 4, 5 };
    86     public static int simple(boolean clone) {
    87         int[] ar;
    88         if (clone) {
    89             ar = arr.clone();
    90         } else {
    91             ar = arr;
    92         }
    93         
    94         int sum = 0;
    95         for (int a : ar) {
    96             sum += a;
    97         }
    98         return sum;
    99     }
   100     
   101     public static int sum(int size) {
   102         int[] arr = new int[size];
   103         return arr[0] + arr[1];
   104     }
   105     
   106     static void arraycopy(char[] value, int srcBegin, char[] dst, int dstBegin, int count) {
   107         while (count-- > 0) {
   108             dst[dstBegin++] = value[srcBegin++];
   109         }
   110     }
   111 
   112     public static char copyArray() {
   113         char[] arr = { '0' };
   114         arraycopy(arr()[0][0].chars, 0, arr, 0, 1);
   115         return arr[0];
   116     }
   117 }