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 jdk.internal.foreign.LayoutPathsImpl;
  29 
  30 import java.lang.invoke.VarHandle;
  31 import java.util.function.Predicate;
  32 import java.util.stream.Stream;
  33 
  34 /**
  35  * A layout path.
  36  */
  37 public interface LayoutPath {
  38 
  39     /**
  40      * The layout associated with this path.
  41      * @return the layout associated with this path.
  42      */
  43     Layout layout();
  44 
  45     /**
  46      * The enclosing path of this layout path.
  47      * @return The enclosing path of this layout path.
  48      */
  49     LayoutPath enclosing();
  50 
  51     /**
  52      * The offset of this layout path (relative to the root element).
  53      * @return The offset of this layout path (relative to the root element).
  54      */
  55     long offset();
  56 
  57     /**
  58      * A var handle that can be used to dereference memory at this path.
  59      * @param carrier the var handle carrier type.
  60      * @return a var handle which can be used to dereference memory at this path.
  61      */
  62     VarHandle dereferenceHandle(Class<?> carrier);
  63 
  64     /**
  65      * Lookup a layout path for elements with given name.
  66      * @param name the name of the matching layout subelements.
  67      * @return a stream of matching layout subelements.
  68      */
  69     default Stream<LayoutPath> lookup(String name) {
  70         return lookup(l -> l.name().map(n -> n.equals(name)).orElse(false));
  71     }
  72 
  73     /**
  74      * Lookup a layout path with given matching predicate.
  75      * @param condition the predicate describing matching layout subelements.
  76      * @return a stream of matching layout subelements.
  77      */
  78     Stream<LayoutPath> lookup(Predicate<? super Layout> condition);
  79 
  80     /**
  81      * Create a new layout path rooted in the given layout.
  82      * @param layout the root of the layout path to be created.
  83      * @return a new layout path.
  84      */
  85     static LayoutPath of(Layout layout) {
  86         return LayoutPathsImpl.of(layout);
  87     }
  88 }