1 /*
   2  * Copyright (c) 2016, 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 
  24 package jdk.internal.nicl.types;
  25 
  26 import java.util.Arrays;
  27 import java.util.stream.Stream;
  28 import jdk.internal.nicl.Platform;
  29 
  30 public class Container implements Type {
  31     final Type[] members;
  32     final boolean isUnion;
  33 
  34     public Container(boolean isUnion, Type... members) {
  35         this.members = members;
  36         this.isUnion = isUnion;
  37     }
  38 
  39     @Override
  40     public long getSize() {
  41         return Platform.getInstance().getABI().sizeof(this);
  42     }
  43 
  44     public boolean isUnion() {
  45         return isUnion;
  46     }
  47 
  48     public Stream<Type> getMembers() {
  49         return Stream.of(members);
  50     }
  51 
  52     public int memberCount() {
  53         return members.length;
  54     }
  55 
  56     public Type getMember(int index) {
  57         return members[index];
  58     }
  59 
  60     @Override
  61     public int hashCode() {
  62         return (isUnion ? 0x40000000 : 0x60000000) | Arrays.hashCode(members);
  63     }
  64 
  65     @Override
  66     public boolean equals(Object o) {
  67         if (!(o instanceof Container)) {
  68             return false;
  69         }
  70 
  71         Container other = (Container) o;
  72         if (other.isUnion != isUnion) {
  73             return false;
  74         }
  75         if (other.members.length != members.length) {
  76             return false;
  77         }
  78         for (int i = 0; i < members.length; i++) {
  79             if (!members[i].equals(other.members[i])) {
  80                 return false;
  81             }
  82         }
  83         return true;
  84     }
  85 
  86     @Override
  87     public String toString() {
  88         StringBuffer sb = new StringBuffer();
  89         sb.append("[");
  90         for (Type t : members) {
  91             sb.append(t);
  92             if (isUnion) {
  93                 sb.append('|');
  94             }
  95         }
  96         if (isUnion) {
  97             sb.setCharAt(sb.length() - 1, ']');
  98         } else {
  99             sb.append(']');
 100         }
 101         return sb.toString();
 102     }
 103 }