1 /*
   2  * Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package runtime.valhalla.valuetypes;
  24 
  25 import java.nio.ByteBuffer;
  26 
  27 final class ContainerValue4 {
  28     static TestValue4.box staticValueField;
  29     TestValue4.val nonStaticValueField;
  30     TestValue4[] valueArray;
  31 }
  32 
  33 public value final class TestValue4 {
  34 
  35     static TestValue4.box staticValue = getInstance();
  36 
  37     final byte b1;
  38     final byte b2;
  39     final byte b3;
  40     final byte b4;
  41     final short s1;
  42     final short s2;
  43     final int i;
  44     final long l;
  45     final String val;
  46 
  47     private TestValue4() {
  48         i = (int)System.nanoTime();
  49         val = Integer.valueOf(i).toString();
  50         l = ((long)i) << Integer.SIZE | i;
  51         s1 = (short)(i & ~Short.MIN_VALUE);
  52         s2 = (short)(i >> Short.SIZE);
  53         b1 = (byte)(i & ~Byte.MIN_VALUE);
  54         b2 = (byte)((i >> Byte.SIZE) & ~Byte.MIN_VALUE);
  55         b3 = (byte)((i >> (2 * Byte.SIZE)) & ~Byte.MIN_VALUE);
  56         b4 = (byte)((i >> (3 * Byte.SIZE)) & ~Byte.MIN_VALUE);
  57     }
  58 
  59     public static TestValue4 create(int i) {
  60         TestValue4 v = TestValue4.default;
  61         v = __WithField(v.i, i);
  62         v = __WithField(v.val, Integer.valueOf(i).toString());
  63         ByteBuffer bf = ByteBuffer.allocate(8);
  64         bf.putInt(0, i);
  65         bf.putInt(4, i);
  66         v = __WithField(v.l, bf.getLong(0));
  67         v = __WithField(v.s1, bf.getShort(2));
  68         v = __WithField(v.s2, bf.getShort(0));
  69         v = __WithField(v.b1, bf.get(3));
  70         v = __WithField(v.b2, bf.get(2));
  71         v = __WithField(v.b3, bf.get(1));
  72         v = __WithField(v.b4, bf.get(0));
  73         return v;
  74     }
  75 
  76     public static TestValue4 create() {
  77         return create((int)System.nanoTime());
  78     }
  79 
  80     public static TestValue4 getInstance() {
  81         return create();
  82     }
  83 
  84     public static TestValue4 getNonBufferedInstance() {
  85         return staticValue;
  86     }
  87 
  88     public boolean verify() {
  89         if (val == null) {
  90             return i == 0 && l == 0 && b1 == 0 && b2 == 0 && b3 == 0 && b4 == 0
  91                     && s1 == 0 && s2 == 0;
  92         }
  93         ByteBuffer bf = ByteBuffer.allocate(8);
  94         bf.putInt(0, i);
  95         bf.putInt(4, i);
  96         long nl =  bf.getLong(0);
  97         bf.clear();
  98         bf.putShort(0, s2);
  99         bf.putShort(2, s1);
 100         int from_s = bf.getInt(0);
 101         bf.clear();
 102         bf.put(0, b4);
 103         bf.put(1, b3);
 104         bf.put(2, b2);
 105         bf.put(3, b1);
 106         int from_b = bf.getInt(0);
 107         return l == nl && Integer.valueOf(i).toString().compareTo(val) == 0
 108                 && from_s == i && from_b == i;
 109     }
 110 }