src/sample/nashorn/BufferArray.java
changeset 47216 71c04702a3d5
parent 26886 18c744ab4df2
equal deleted inserted replaced
47215:4ebc2e2fb97c 47216:71c04702a3d5
       
     1 /*
       
     2  * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
       
     3  *
       
     4  * Redistribution and use in source and binary forms, with or without
       
     5  * modification, are permitted provided that the following conditions
       
     6  * are met:
       
     7  *
       
     8  *   - Redistributions of source code must retain the above copyright
       
     9  *     notice, this list of conditions and the following disclaimer.
       
    10  *
       
    11  *   - Redistributions in binary form must reproduce the above copyright
       
    12  *     notice, this list of conditions and the following disclaimer in the
       
    13  *     documentation and/or other materials provided with the distribution.
       
    14  *
       
    15  *   - Neither the name of Oracle nor the names of its
       
    16  *     contributors may be used to endorse or promote products derived
       
    17  *     from this software without specific prior written permission.
       
    18  *
       
    19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
       
    20  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
       
    21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
       
    22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
       
    23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
       
    24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
       
    25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
       
    26  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
       
    27  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
       
    28  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
       
    29  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
       
    30  */
       
    31 
       
    32 import java.nio.DoubleBuffer;
       
    33 import jdk.nashorn.api.scripting.AbstractJSObject;
       
    34 
       
    35 /**
       
    36  * Simple class demonstrating pluggable script object
       
    37  * implementation. By implementing jdk.nashorn.api.scripting.JSObject
       
    38  * (or extending AbstractJSObject which implements it), you
       
    39  * can supply a friendly script object. Nashorn will call
       
    40  * 'magic' methods on such a class on 'obj.foo, obj.foo = 33,
       
    41  * obj.bar()' etc. from script.
       
    42  *
       
    43  * In this example, Java nio DoubleBuffer object is wrapped
       
    44  * as a friendly script object that provides indexed acces
       
    45  * to buffer content and also support array-like "length"
       
    46  * readonly property to retrieve buffer's capacity. This class
       
    47  * also demonstrates a function valued property called "buf".
       
    48  * On 'buf' method, we return the underlying nio buffer object
       
    49  * that is being wrapped.
       
    50  */
       
    51 public class BufferArray extends AbstractJSObject {
       
    52     // underlying nio buffer
       
    53     private final DoubleBuffer buf;
       
    54 
       
    55     /**
       
    56      * Constructor
       
    57      * @param size initial size
       
    58      */
       
    59     public BufferArray(final int size) {
       
    60         buf = DoubleBuffer.allocate(size);
       
    61     }
       
    62 
       
    63     /**
       
    64      * Constructur
       
    65      * @param buf {@link DoubleBuffer} to link to
       
    66      */
       
    67     public BufferArray(final DoubleBuffer buf) {
       
    68         this.buf = buf;
       
    69     }
       
    70 
       
    71     // called to check if indexed property exists
       
    72     @Override
       
    73     public boolean hasSlot(final int index) {
       
    74         return index > 0 && index < buf.capacity();
       
    75     }
       
    76 
       
    77     // get the value from that index
       
    78     @Override
       
    79     public Object getSlot(final int index) {
       
    80        return buf.get(index);
       
    81     }
       
    82 
       
    83     // set the value at that index
       
    84     @Override
       
    85     public void setSlot(final int index, final Object value) {
       
    86        buf.put(index, ((Number)value).doubleValue());
       
    87     }
       
    88 
       
    89     // do you have a property of that given name?
       
    90     @Override
       
    91     public boolean hasMember(final String name) {
       
    92        return "length".equals(name) || "buf".equals(name);
       
    93     }
       
    94 
       
    95     // get the value of that named property
       
    96     @Override
       
    97     public Object getMember(final String name) {
       
    98        switch (name) {
       
    99           case "length":
       
   100               return buf.capacity();
       
   101           case "buf":
       
   102               // return a 'function' value for this property
       
   103               return new AbstractJSObject() {
       
   104                   @Override
       
   105                   public Object call(final Object thiz, final Object... args) {
       
   106                       return BufferArray.this.buf;
       
   107                   }
       
   108 
       
   109                   // yes, I'm a function !
       
   110                   @Override
       
   111                   public boolean isFunction() {
       
   112                       return true;
       
   113                   }
       
   114               };
       
   115           default:
       
   116               break;
       
   117        }
       
   118        return null;
       
   119     }
       
   120 }