StoredScript.java revision 1148:f340141c05f1
1141104Sharti/*
294589Sobrien * Copyright (c) 2010, 2014, Oracle and/or its affiliates. All rights reserved.
394589Sobrien * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
45814Sjkh *
51590Srgrimes * This code is free software; you can redistribute it and/or modify it
61590Srgrimes * under the terms of the GNU General Public License version 2 only, as
71590Srgrimes * published by the Free Software Foundation.  Oracle designates this
81590Srgrimes * particular file as subject to the "Classpath" exception as provided
91590Srgrimes * by Oracle in the LICENSE file that accompanied this code.
101590Srgrimes *
111590Srgrimes * This code is distributed in the hope that it will be useful, but WITHOUT
121590Srgrimes * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
131590Srgrimes * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
141590Srgrimes * version 2 for more details (a copy is included in the LICENSE file that
151590Srgrimes * accompanied this code).
161590Srgrimes *
171590Srgrimes * You should have received a copy of the GNU General Public License version
181590Srgrimes * 2 along with this work; if not, write to the Free Software Foundation,
191590Srgrimes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
201590Srgrimes *
211590Srgrimes * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
221590Srgrimes * or visit www.oracle.com if you need additional information or have any
231590Srgrimes * questions.
241590Srgrimes */
251590Srgrimes
261590Srgrimespackage jdk.nashorn.internal.runtime;
271590Srgrimes
281590Srgrimesimport java.io.Serializable;
291590Srgrimesimport java.util.Arrays;
301590Srgrimesimport java.util.LinkedHashMap;
311590Srgrimesimport java.util.Map;
321590Srgrimes
331590Srgrimes/**
341590Srgrimes * Class representing a persistent compiled script.
351590Srgrimes */
361590Srgrimespublic final class StoredScript implements Serializable {
371590Srgrimes
3862833Swsanchez    /** Compilation id */
3962833Swsanchez    private final int compilationId;
401590Srgrimes
411590Srgrimes    /** Main class name. */
4262833Swsanchez    private final String mainClassName;
4394587Sobrien
441590Srgrimes    /** Map of class names to class bytes. */
4535483Simp    private final Map<String, byte[]> classBytes;
46103503Sjmallett
4735483Simp    /** Constants array. */
4835483Simp    private final Object[] constants;
491590Srgrimes
501590Srgrimes    /** Function initializers */
511590Srgrimes    private final Map<Integer, FunctionInitializer> initializers;
521590Srgrimes
531590Srgrimes    private static final long serialVersionUID = 2958227232195298340L;
541590Srgrimes
551590Srgrimes    /**
561590Srgrimes     * Constructor.
571590Srgrimes     *
581590Srgrimes     * @param compilationId compilation id
591590Srgrimes     * @param mainClassName main class name
601590Srgrimes     * @param classBytes map of class names to class bytes
611590Srgrimes     * @param initializers initializer map, id -&gt; FunctionInitializer
621590Srgrimes     * @param constants constants array
631590Srgrimes     */
641590Srgrimes    public StoredScript(final int compilationId, final String mainClassName, final Map<String, byte[]> classBytes, final Map<Integer, FunctionInitializer> initializers, final Object[] constants) {
651590Srgrimes        this.compilationId = compilationId;
661590Srgrimes        this.mainClassName = mainClassName;
671590Srgrimes        this.classBytes = classBytes;
681590Srgrimes        this.constants = constants;
691590Srgrimes        this.initializers = initializers;
701590Srgrimes    }
711590Srgrimes
721590Srgrimes    /**
731590Srgrimes     * Get the compilation id for this StoredScript
741590Srgrimes     * @return compilation id
751590Srgrimes     */
761590Srgrimes    public int getCompilationId() {
771590Srgrimes        return compilationId;
781590Srgrimes    }
791590Srgrimes
801590Srgrimes    /**
811590Srgrimes     * Returns the main class name.
821590Srgrimes     * @return the main class name
831590Srgrimes     */
841590Srgrimes    public String getMainClassName() {
851590Srgrimes        return mainClassName;
8694594Sobrien    }
871590Srgrimes
881590Srgrimes    /**
891590Srgrimes     * Returns a map of class names to class bytes.
901590Srgrimes     * @return map of class bytes
911590Srgrimes     */
921590Srgrimes    public Map<String, byte[]> getClassBytes() {
931590Srgrimes        final Map<String, byte[]> clonedMap = new LinkedHashMap<>();
941590Srgrimes        for (final Map.Entry<String, byte[]> entry : classBytes.entrySet()) {
951590Srgrimes            clonedMap.put(entry.getKey(), entry.getValue().clone());
961590Srgrimes        }
971590Srgrimes        return clonedMap;
981590Srgrimes    }
991590Srgrimes
1001590Srgrimes    /**
1011590Srgrimes     * Returns the constants array.
1021590Srgrimes     * @return constants array
1031590Srgrimes     */
1041590Srgrimes    public Object[] getConstants() {
1051590Srgrimes        return constants.clone();
106141104Sharti    }
1071590Srgrimes
108107447Sru    /**
109104475Sphk     * Returns the function initializers map.
110107447Sru     * @return The initializers map.
1111590Srgrimes     */
112141104Sharti    public Map<Integer, FunctionInitializer> getInitializers() {
11394506Scharnier        final Map<Integer, FunctionInitializer> clonedMap = new LinkedHashMap<>();
1145814Sjkh        for (final Map.Entry<Integer, FunctionInitializer> entry : initializers.entrySet()) {
1151590Srgrimes            clonedMap.put(entry.getKey(), new FunctionInitializer(entry.getValue()));
1165814Sjkh        }
117141104Sharti        return clonedMap;
11880381Ssheldonh    }
11994506Scharnier
120141104Sharti    @Override
121141104Sharti    public int hashCode() {
122141104Sharti        int hash = mainClassName.hashCode();
1231590Srgrimes        hash = 31 * hash + classBytes.hashCode();
124141104Sharti        hash = 31 * hash + Arrays.hashCode(constants);
125141104Sharti        return hash;
1261590Srgrimes    }
127141104Sharti
128141104Sharti    @Override
1291590Srgrimes    public boolean equals(final Object obj) {
130141104Sharti        if (obj == this) {
131141104Sharti            return true;
132141104Sharti        }
133141104Sharti        if (!(obj instanceof StoredScript)) {
1341590Srgrimes            return false;
135137202Sharti        }
136137202Sharti
1371590Srgrimes        final StoredScript cs = (StoredScript) obj;
1388874Srgrimes        return mainClassName.equals(cs.mainClassName)
1391590Srgrimes                && classBytes.equals(cs.classBytes)
1401590Srgrimes                && Arrays.equals(constants, cs.constants);
1411590Srgrimes    }
142103503Sjmallett}
143103503Sjmallett