View Javadoc
1   /*
2    * Copyright (c) 2002-2025 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * https://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package org.htmlunit;
16  
17  import org.htmlunit.javascript.JavaScriptEngine;
18  
19  /**
20   * This object contains the result of executing a chunk of script code.
21   *
22   * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
23   * @author Marc Guillemot
24   */
25  public final class ScriptResult {
26  
27      /** The object that was returned from the script engine. */
28      private final Object javaScriptResult_;
29  
30      /**
31       * Creates a new instance.
32       *
33       * @param javaScriptResult the object that was returned from the script engine
34       */
35      public ScriptResult(final Object javaScriptResult) {
36          javaScriptResult_ = javaScriptResult;
37      }
38  
39      /**
40       * Returns the object that was the output of the script engine.
41       * @return the result from the script engine
42       */
43      public Object getJavaScriptResult() {
44          return javaScriptResult_;
45      }
46  
47      /**
48       * {@inheritDoc}
49       */
50      @Override
51      public String toString() {
52          return "ScriptResult[result=" + javaScriptResult_ + "]";
53      }
54  
55      /**
56       * Utility method testing if a script result is {@code false}.
57       * @param scriptResult a script result (may be {@code null})
58       * @return {@code true} if <code>scriptResult</code> is {@code false}
59       */
60      public static boolean isFalse(final ScriptResult scriptResult) {
61          return scriptResult != null && Boolean.FALSE.equals(scriptResult.getJavaScriptResult());
62      }
63  
64      /**
65       * Utility method testing if a script result is undefined (there was no return value).
66       * @param scriptResult a script result (may be {@code null})
67       * @return {@code true} if <code>scriptResult</code> is undefined (there was no return value)
68       */
69      public static boolean isUndefined(final ScriptResult scriptResult) {
70          return scriptResult != null && JavaScriptEngine.isUndefined(scriptResult.getJavaScriptResult());
71      }
72  }