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 
  26 package java.foreign.layout;
  27 
  28 import java.util.Map;
  29 import java.util.Optional;
  30 
  31 /**
  32  * This interface models a native descriptor. There are two kinds of descriptors: (i) layouts and functions.
  33  * Layouts are used to model layout of some memory region (see {@link Layout}, while functions are used to model
  34  * native function signatures. Descriptors can be annotated, as a way to embed custom information. A special 'name'
  35  * annotation is reserved (see {@link Descriptor#NAME}); this annotation is useful when referencing descriptors by name
  36  * (see {@link Unresolved}).
  37  *
  38  * @see Layout
  39  * @see Function
  40  */
  41 public interface Descriptor {
  42     /**
  43      * The key of the predefined 'name' annotation.
  44      */
  45     String NAME = "name";
  46 
  47     /**
  48      * Does this descriptor contain unresolved layouts?
  49      * @return true if this layout contains (possibly nested) unresolved layouts.
  50      */
  51     boolean isPartial();
  52 
  53     /**
  54      * Return the key-value annotations map associated with this object.
  55      * @return the key-value annotations map.
  56      */
  57     Map<String, String> annotations();
  58 
  59     /**
  60      * Return the value of the 'name' annotation (if any) associated with this object.
  61      * @return the descriptor name (if any).
  62      */
  63     default Optional<String> name() {
  64         return Optional.ofNullable(annotations().get(NAME));
  65     }
  66 
  67     /**
  68      * Add annotation to descriptor.
  69      * @param name the annotation name.
  70      * @param value the annotation value.
  71      * @return the annotated layout.
  72      */
  73     Descriptor withAnnotation(String name, String value);
  74 
  75     /**
  76      * Attach name annotation to given descriptor.
  77      * @param name name annotation.
  78      * @return a new descriptor with desired name annotation.
  79      */
  80     Descriptor withName(String name);
  81 
  82     /**
  83      * Strip all annotations from this (possibly annotated) descriptor.
  84      * @return the unannotated descriptor.
  85      */
  86     Descriptor stripAnnotations();
  87 
  88     @Override
  89     String toString();
  90 }