StoredScript.java revision 971:c93b6091b11e
1144513Simp/*
2144513Simp * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
3144513Simp * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4144513Simp *
5144513Simp * This code is free software; you can redistribute it and/or modify it
6144513Simp * 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.Map;
31
32/**
33 * Class representing a persistent compiled script.
34 */
35public final class StoredScript implements Serializable {
36
37    /** Compilation id */
38    private final int compilationId;
39
40    /** Main class name. */
41    private final String mainClassName;
42
43    /** Map of class names to class bytes. */
44    private final Map<String, byte[]> classBytes;
45
46    /** Constants array. */
47    private final Object[] constants;
48
49    /** Function initializers */
50    private final Map<Integer, FunctionInitializer> initializers;
51
52    private static final long serialVersionUID = 2958227232195298340L;
53
54    /**
55     * Constructor.
56     *
57     * @param mainClassName main class name
58     * @param classBytes map of class names to class bytes
59     * @param constants constants array
60     */
61    public StoredScript(final int compilationId, final String mainClassName, final Map<String, byte[]> classBytes, final Map<Integer, FunctionInitializer> initializers, final Object[] constants) {
62        this.compilationId = compilationId;
63        this.mainClassName = mainClassName;
64        this.classBytes = classBytes;
65        this.constants = constants;
66        this.initializers = initializers;
67    }
68
69    public int getCompilationId() {
70        return compilationId;
71    }
72
73    /**
74     * Returns the main class name.
75     * @return the main class name
76     */
77    public String getMainClassName() {
78        return mainClassName;
79    }
80
81    /**
82     * Returns a map of class names to class bytes.
83     * @return map of class bytes
84     */
85    public Map<String, byte[]> getClassBytes() {
86        return classBytes;
87    }
88
89    /**
90     * Returns the constants array.
91     * @return constants array
92     */
93    public Object[] getConstants() {
94        return constants;
95    }
96
97    Map<Integer, FunctionInitializer> getInitializers() {
98        return initializers;
99    }
100
101    @Override
102    public int hashCode() {
103        int hash = mainClassName.hashCode();
104        hash = 31 * hash + classBytes.hashCode();
105        hash = 31 * hash + Arrays.hashCode(constants);
106        return hash;
107    }
108
109    @Override
110    public boolean equals(final Object obj) {
111        if (obj == this) {
112            return true;
113        }
114        if (!(obj instanceof StoredScript)) {
115            return false;
116        }
117
118        final StoredScript cs = (StoredScript) obj;
119        return mainClassName.equals(cs.mainClassName)
120                && classBytes.equals(cs.classBytes)
121                && Arrays.equals(constants, cs.constants);
122    }
123}
124