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.Map;
  28 
  29 /**
  30  * An unresolved layout acts as a placeholder for another layout. Unresolved layouts can be resolved, which yields
  31  * a new layout whose size is known. The resolution process is typically driven by the annotations attached to the
  32  * unresolved layout.
  33  */
  34 public class Unresolved extends AbstractDescriptor<Unresolved> implements Layout {
  35     private final String layoutExpression;
  36 
  37     Unresolved(String layoutExpression, Map<String, String> annotations) {
  38         super(annotations);
  39         this.layoutExpression = layoutExpression;
  40     }
  41 
  42      /**
  43       * Create a new unresolved layout from given layout expression.
  44       * @param layoutExpression the layout expression.
  45       * @return the new unresolved layout.
  46       */
  47     public static Unresolved of(String layoutExpression) {
  48         return new Unresolved(layoutExpression, NO_ANNOS);
  49     }
  50 
  51     /**
  52      * The layout expression associated with this unresolved layout.
  53      * @return The layout expression associated with this unresolved layout.
  54      */
  55     public String layoutExpression() {
  56         return layoutExpression;
  57     }
  58 
  59     @Override
  60     public boolean equals(Object other) {
  61         if (this == other) {
  62             return true;
  63         }
  64         if (!super.equals(other)) {
  65             return false;
  66         }
  67         if (!(other instanceof Unresolved)) {
  68             return false;
  69         }
  70         return layoutExpression.equals(((Unresolved)other).layoutExpression);
  71     }
  72 
  73     @Override
  74     public int hashCode() {
  75         return super.hashCode() ^ layoutExpression.hashCode();
  76     }
  77 
  78     @Override
  79     public long bitsSize() {
  80         throw new UnsupportedOperationException("bitsSize on Unresolved");
  81     }
  82 
  83     @Override
  84     public boolean isPartial() {
  85         return true;
  86     }
  87 
  88     @Override
  89     public String toString() {
  90         return wrapWithAnnotations(String.format("${%s}", layoutExpression));
  91     }
  92 
  93     @Override
  94     Unresolved withAnnotations(Map<String, String> annotations) {
  95         return new Unresolved(layoutExpression, annotations);
  96     }
  97 }