StoredScript.java revision 1032:52752e15fe18
1/*
2 * Copyright (c) 2010, 2014, 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;
27
28import java.io.Serializable;
29import java.util.Arrays;
30import java.util.LinkedHashMap;
31import java.util.Map;
32
33/**
34 * Class representing a persistent compiled script.
35 */
36public final class StoredScript implements Serializable {
37
38    /** Compilation id */
39    private final int compilationId;
40
41    /** Main class name. */
42    private final String mainClassName;
43
44    /** Map of class names to class bytes. */
45    private final Map<String, byte[]> classBytes;
46
47    /** Constants array. */
48    private final Object[] constants;
49
50    /** Function initializers */
51    private final Map<Integer, FunctionInitializer> initializers;
52
53    private static final long serialVersionUID = 2958227232195298340L;
54
55    /**
56     * Constructor.
57     *
58     * @param mainClassName main class name
59     * @param classBytes map of class names to class bytes
60     * @param constants constants array
61     */
62    public StoredScript(final int compilationId, final String mainClassName, final Map<String, byte[]> classBytes, final Map<Integer, FunctionInitializer> initializers, final Object[] constants) {
63        this.compilationId = compilationId;
64        this.mainClassName = mainClassName;
65        this.classBytes = classBytes;
66        this.constants = constants;
67        this.initializers = initializers;
68    }
69
70    public int getCompilationId() {
71        return compilationId;
72    }
73
74    /**
75     * Returns the main class name.
76     * @return the main class name
77     */
78    public String getMainClassName() {
79        return mainClassName;
80    }
81
82    /**
83     * Returns a map of class names to class bytes.
84     * @return map of class bytes
85     */
86    public Map<String, byte[]> getClassBytes() {
87        final Map<String, byte[]> clonedMap = new LinkedHashMap<>();
88        for (final Map.Entry<String, byte[]> entry : classBytes.entrySet()) {
89            clonedMap.put(entry.getKey(), entry.getValue().clone());
90        }
91        return clonedMap;
92    }
93
94    /**
95     * Returns the constants array.
96     * @return constants array
97     */
98    public Object[] getConstants() {
99        return constants.clone();
100    }
101
102    /**
103     * Returns the function initializers map.
104     * @return The initializers map.
105     */
106    public Map<Integer, FunctionInitializer> getInitializers() {
107        final Map<Integer, FunctionInitializer> clonedMap = new LinkedHashMap<>();
108        for (final Map.Entry<Integer, FunctionInitializer> entry : initializers.entrySet()) {
109            clonedMap.put(entry.getKey(), new FunctionInitializer(entry.getValue()));
110        }
111        return clonedMap;
112    }
113
114    @Override
115    public int hashCode() {
116        int hash = mainClassName.hashCode();
117        hash = 31 * hash + classBytes.hashCode();
118        hash = 31 * hash + Arrays.hashCode(constants);
119        return hash;
120    }
121
122    @Override
123    public boolean equals(final Object obj) {
124        if (obj == this) {
125            return true;
126        }
127        if (!(obj instanceof StoredScript)) {
128            return false;
129        }
130
131        final StoredScript cs = (StoredScript) obj;
132        return mainClassName.equals(cs.mainClassName)
133                && classBytes.equals(cs.classBytes)
134                && Arrays.equals(constants, cs.constants);
135    }
136}
137