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