1 /*
   2  * Copyright (c) 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 package java.foreign.layout;
  26 
  27 import java.util.Collections;
  28 import java.util.Map;
  29 
  30 /**
  31  * A sequence layout. A sequence layout is a special case of a group layout, made up of an element layout and a repetition count.
  32  * The repetition count can be zero if the sequence contains no elements. A sequence layout of the kind e.g. {@code [ 4i32 ]},
  33  * always induces a group layout e.g. {@code [i32 i32 i32 i32]} - that is, the group associated with a sequence is
  34  * a 'struct' group (see {@link Group.Kind#STRUCT}), where a given layout element is repeated a number of times that
  35  * is equal to the sequence size.
  36  */
  37 public final class Sequence extends Group {
  38 
  39     private final long size;
  40     private final Layout elementLayout;
  41 
  42     private Sequence(long size, Layout elementLayout, Map<String, String> annotations) {
  43         super(Kind.STRUCT, Collections.nCopies((int)size, elementLayout), annotations);
  44         this.size = size;
  45         this.elementLayout = elementLayout;
  46     }
  47 
  48     /**
  49      * The element layout associated with this sequence layout.
  50      * @return element layout.
  51      */
  52     public Layout element() {
  53         return elementLayout;
  54     }
  55 
  56     /**
  57      * Create a new sequence layout with given element layout and size.
  58      * @param elementLayout the element layout.
  59      * @param size the array repetition count.
  60      * @return the new sequence layout.
  61      */
  62     public static Sequence of(long size, Layout elementLayout) {
  63         return new Sequence(size, elementLayout, NO_ANNOS);
  64     }
  65 
  66     /**
  67      * Returns the repetition count associated with this sequence layout.
  68      * @return the repetition count (can be zero if array size is unspecified).
  69      */
  70     public long elementsSize() {
  71         return size;
  72     }
  73 
  74     @Override
  75     public String toString() {
  76         return wrapWithAnnotations(String.format("[%d%s]",
  77                 size, elementLayout));
  78     }
  79 
  80     @Override
  81     public boolean equals(Object other) {
  82         if (this == other) {
  83             return true;
  84         }
  85         if (!super.equals(other)) {
  86             return false;
  87         }
  88         if (!(other instanceof Sequence)) {
  89             return false;
  90         }
  91         Sequence s = (Sequence)other;
  92         return size == s.size && elementLayout.equals(s.elementLayout);
  93     }
  94 
  95     @Override
  96     public int hashCode() {
  97         return super.hashCode() ^ Long.hashCode(size) ^ elementLayout.hashCode();
  98     }
  99 
 100     @Override
 101     Sequence withAnnotations(Map<String, String> annotations) {
 102         return new Sequence(elementsSize(), elementLayout, annotations);
 103     }
 104 
 105     @Override
 106     public Sequence stripAnnotations() {
 107         return (Sequence)super.stripAnnotations();
 108     }
 109 
 110     @Override
 111     public Sequence withAnnotation(String name, String value) {
 112         return (Sequence) super.withAnnotation(name, value);
 113     }
 114 }