ArrayFilter.java revision 1101:be3f5ca1edbf
1/*
2 * Copyright (c) 2010, 2013, 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
26package jdk.nashorn.internal.runtime.arrays;
27
28import jdk.nashorn.internal.codegen.types.Type;
29import jdk.nashorn.internal.runtime.ScriptRuntime;
30import jdk.nashorn.internal.runtime.Undefined;
31import jdk.nashorn.internal.runtime.linker.Bootstrap;
32
33/**
34 * Base class for array filters. Implements all core routines so that the
35 * filter only has to implement those needed.
36 */
37abstract class ArrayFilter extends ArrayData {
38    /** Underlying array. */
39    protected ArrayData underlying;
40
41    ArrayFilter(final ArrayData underlying) {
42        super(underlying.length());
43        this.underlying = underlying;
44    }
45
46    /**
47     * Get the underlying {@link ArrayData} that this filter wraps
48     * @return array data
49     */
50    protected ArrayData getUnderlying() {
51        return underlying;
52    }
53
54    @Override
55    public void setLength(final long length) {
56        super.setLength(length);
57        underlying.setLength(length);
58    }
59
60    @Override
61    public Object[] asObjectArray() {
62        return underlying.asObjectArray();
63    }
64
65    @Override
66    public Object asArrayOfType(final Class<?> componentType) {
67        return underlying.asArrayOfType(componentType);
68    }
69
70    @Override
71    public void shiftLeft(final int by) {
72        underlying.shiftLeft(by);
73        setLength(underlying.length());
74    }
75
76    @Override
77    public ArrayData shiftRight(final int by) {
78        underlying = underlying.shiftRight(by);
79        setLength(underlying.length());
80        return this;
81    }
82
83    @Override
84    public ArrayData ensure(final long safeIndex) {
85        underlying = underlying.ensure(safeIndex);
86        setLength(underlying.length());
87        return this;
88    }
89
90    @Override
91    public ArrayData shrink(final long newLength) {
92        underlying = underlying.shrink(newLength);
93        setLength(underlying.length());
94        return this;
95    }
96
97    @Override
98    public ArrayData set(final int index, final Object value, final boolean strict) {
99        underlying = underlying.set(index, value, strict);
100        setLength(underlying.length());
101        return this;
102    }
103
104    @Override
105    public ArrayData set(final int index, final int value, final boolean strict) {
106        underlying = underlying.set(index, value, strict);
107        setLength(underlying.length());
108        return this;
109    }
110
111    @Override
112    public ArrayData set(final int index, final long value, final boolean strict) {
113        underlying = underlying.set(index, value, strict);
114        setLength(underlying.length());
115        return this;
116    }
117
118    @Override
119    public ArrayData set(final int index, final double value, final boolean strict) {
120        underlying = underlying.set(index, value, strict);
121        setLength(underlying.length());
122        return this;
123    }
124
125    @Override
126    public ArrayData setEmpty(final int index) {
127        underlying.setEmpty(index);
128        return this;
129    }
130
131    @Override
132    public ArrayData setEmpty(final long lo, final long hi) {
133        underlying.setEmpty(lo, hi);
134        return this;
135    }
136
137    @Override
138    public Type getOptimisticType() {
139        return underlying.getOptimisticType();
140    }
141
142    @Override
143    public int getInt(final int index) {
144        return underlying.getInt(index);
145    }
146
147    @Override
148    public int getIntOptimistic(final int index, final int programPoint) {
149        return underlying.getIntOptimistic(index, programPoint);
150    }
151
152    @Override
153    public long getLong(final int index) {
154        return underlying.getLong(index);
155    }
156
157    @Override
158    public long getLongOptimistic(final int index, final int programPoint) {
159        return underlying.getLongOptimistic(index, programPoint);
160    }
161
162    @Override
163    public double getDouble(final int index) {
164        return underlying.getDouble(index);
165    }
166
167    @Override
168    public double getDoubleOptimistic(final int index, final int programPoint) {
169        return underlying.getDoubleOptimistic(index, programPoint);
170    }
171
172    @Override
173    public Object getObject(final int index) {
174        return underlying.getObject(index);
175    }
176
177    @Override
178    public boolean has(final int index) {
179        return underlying.has(index);
180    }
181
182    @Override
183    public ArrayData delete(final int index) {
184        underlying = underlying.delete(index);
185        setLength(underlying.length());
186        return this;
187    }
188
189    @Override
190    public ArrayData delete(final long from, final long to) {
191        underlying = underlying.delete(from, to);
192        setLength(underlying.length());
193        return this;
194    }
195
196    @Override
197    public ArrayData convert(final Class<?> type) {
198        underlying = underlying.convert(type);
199        setLength(underlying.length());
200        return this;
201    }
202
203    @Override
204    public Object pop() {
205        final Object value = underlying.pop();
206        setLength(underlying.length());
207        return value;
208    }
209
210    @Override
211    public long nextIndex(final long index) {
212        return underlying.nextIndex(index);
213    }
214
215    static Object convertUndefinedValue(final Class<?> targetType) {
216        return invoke(Bootstrap.getLinkerServices().getTypeConverter(Undefined.class, targetType),
217                ScriptRuntime.UNDEFINED);
218    }
219}
220