ArrayFilter.java revision 1571:fd97b9047199
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 double 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 setEmpty(final int index) {
120        underlying.setEmpty(index);
121        return this;
122    }
123
124    @Override
125    public ArrayData setEmpty(final long lo, final long hi) {
126        underlying.setEmpty(lo, hi);
127        return this;
128    }
129
130    @Override
131    public Type getOptimisticType() {
132        return underlying.getOptimisticType();
133    }
134
135    @Override
136    public int getInt(final int index) {
137        return underlying.getInt(index);
138    }
139
140    @Override
141    public int getIntOptimistic(final int index, final int programPoint) {
142        return underlying.getIntOptimistic(index, programPoint);
143    }
144
145    @Override
146    public double getDouble(final int index) {
147        return underlying.getDouble(index);
148    }
149
150    @Override
151    public double getDoubleOptimistic(final int index, final int programPoint) {
152        return underlying.getDoubleOptimistic(index, programPoint);
153    }
154
155    @Override
156    public Object getObject(final int index) {
157        return underlying.getObject(index);
158    }
159
160    @Override
161    public boolean has(final int index) {
162        return underlying.has(index);
163    }
164
165    @Override
166    public ArrayData delete(final int index) {
167        underlying = underlying.delete(index);
168        setLength(underlying.length());
169        return this;
170    }
171
172    @Override
173    public ArrayData delete(final long from, final long to) {
174        underlying = underlying.delete(from, to);
175        setLength(underlying.length());
176        return this;
177    }
178
179    @Override
180    public ArrayData convert(final Class<?> type) {
181        underlying = underlying.convert(type);
182        setLength(underlying.length());
183        return this;
184    }
185
186    @Override
187    public Object pop() {
188        final Object value = underlying.pop();
189        setLength(underlying.length());
190        return value;
191    }
192
193    @Override
194    public long nextIndex(final long index) {
195        return underlying.nextIndex(index);
196    }
197
198    static Object convertUndefinedValue(final Class<?> targetType) {
199        return invoke(Bootstrap.getLinkerServices().getTypeConverter(Undefined.class, targetType),
200                ScriptRuntime.UNDEFINED);
201    }
202}
203