// Path: hegemon-core/src/main/java/com/cueup/hegemon/compilation/CachedScriptCompilation.java
// public class CachedScriptCompilation implements ScriptCompilation {
// 
//   /**
//    * We cache compiled scripts to save time during initialization and tests.
//    */
//   private class CompilationKey {
// 
//     private final WeakReference<Context> context;
//     private final String source;
//     private final String name;
//     private final int hash;
// 
//     public CompilationKey(Context context, String source, String name) {
//       this.context = new WeakReference<Context>(context);
//       this.source = source;
//       this.name = name;
//       this.hash = Objects.hashCode(source, name);
//     }
// 
// 
//     @Override
//     public boolean equals(Object o) {
//       if (this == o) {
//         return true;
//       }
//       if (o == null || getClass() != o.getClass()) {
//         return false;
//       }
// 
//       CompilationKey other = (CompilationKey) o;
// 
//       return this.hash == other.hash
//           && this.name.equals(other.name)
//           && this.source.equals(other.source);
//     }
// 
// 
//     @Override
//     public int hashCode() {
//       return this.hash;
//     }
//   }
// 
//   private final LoadingCache<CompilationKey, Script> compilationCache;
// 
// 
//   public CachedScriptCompilation(final ScriptCompilation compilation) {
//     this.compilationCache = CacheBuilder.newBuilder()
//         .maximumSize(500)
//         .build(new CacheLoader<CompilationKey, Script>() {
//           @Override
//           public org.mozilla.javascript.Script load(CompilationKey key) throws Exception {
//             return compilation.compile(key.context.get(), key.name, key.source);
//           }
//         });
//   }
// 
// 
//   @Override
//   public Script compile(Context c, String name, String source) {
//     return this.compilationCache.getUnchecked(new CompilationKey(c, source, name));
//   }
// 
// }
// 
// Path: hegemon-core/src/main/java/com/cueup/hegemon/compilation/ScriptCompilation.java
// public interface ScriptCompilation {
// 
//   Script compile(Context c, String name, String source);
// 
// }
// 
// Path: hegemon-core/src/main/java/com/cueup/hegemon/compilation/SimpleScriptCompilation.java
// public class SimpleScriptCompilation implements ScriptCompilation {
// 
//   private final int optimizationLevel;
// 
// 
//   public SimpleScriptCompilation(int optimizationLevel) {
//     this.optimizationLevel = optimizationLevel;
//   }
// 
// 
//   @Override
//   public Script compile(Context c, String name, String source) {
//     int oldLevel = c.getOptimizationLevel();
//     c.setOptimizationLevel(this.optimizationLevel);
//     Script result = c.compileString(source, name, 1, null);
//     c.setOptimizationLevel(oldLevel);
//     return result;
//   }
// 
// }


import com.cueup.hegemon.compilation.CachedScriptCompilation;
import com.cueup.hegemon.compilation.ScriptCompilation;
import com.cueup.hegemon.compilation.SimpleScriptCompilation;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import javax.script.ScriptException;
import java.util.concurrent.ExecutionException;
/*
 * Copyright 2012 the hegemon authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cueup.hegemon;



/**
 * Cache of scripts.
 */
public class ScriptCache {

  private final LoadingCache<String, Script> cache;


  private static final ScriptCompilation DEFAULT_COMPILATION = new CachedScriptCompilation(
      new SimpleScriptCompilation(9));

  /**
   * Create a ScriptCache that loads scripts from the given LoadPath.
   * @param loadPath the LoadPath to load files from.
   */
  public ScriptCache(final LoadPath loadPath) {
    this(loadPath, DEFAULT_COMPILATION);
  }


  /**
   * Create a ScriptCache that loads scripts from the given LoadPath.
   * @param loadPath the LoadPath to load files from.
   * @param scriptCompilation the strategy to use to compile scripts.
   */
// Path: hegemon-core/src/main/java/com/cueup/hegemon/compilation/CachedScriptCompilation.java
// public class CachedScriptCompilation implements ScriptCompilation {
// 
//   /**
//    * We cache compiled scripts to save time during initialization and tests.
//    */
//   private class CompilationKey {
// 
//     private final WeakReference<Context> context;
//     private final String source;
//     private final String name;
//     private final int hash;
// 
//     public CompilationKey(Context context, String source, String name) {
//       this.context = new WeakReference<Context>(context);
//       this.source = source;
//       this.name = name;
//       this.hash = Objects.hashCode(source, name);
//     }
// 
// 
//     @Override
//     public boolean equals(Object o) {
//       if (this == o) {
//         return true;
//       }
//       if (o == null || getClass() != o.getClass()) {
//         return false;
//       }
// 
//       CompilationKey other = (CompilationKey) o;
// 
//       return this.hash == other.hash
//           && this.name.equals(other.name)
//           && this.source.equals(other.source);
//     }
// 
// 
//     @Override
//     public int hashCode() {
//       return this.hash;
//     }
//   }
// 
//   private final LoadingCache<CompilationKey, Script> compilationCache;
// 
// 
//   public CachedScriptCompilation(final ScriptCompilation compilation) {
//     this.compilationCache = CacheBuilder.newBuilder()
//         .maximumSize(500)
//         .build(new CacheLoader<CompilationKey, Script>() {
//           @Override
//           public org.mozilla.javascript.Script load(CompilationKey key) throws Exception {
//             return compilation.compile(key.context.get(), key.name, key.source);
//           }
//         });
//   }
// 
// 
//   @Override
//   public Script compile(Context c, String name, String source) {
//     return this.compilationCache.getUnchecked(new CompilationKey(c, source, name));
//   }
// 
// }
// 
// Path: hegemon-core/src/main/java/com/cueup/hegemon/compilation/ScriptCompilation.java
// public interface ScriptCompilation {
// 
//   Script compile(Context c, String name, String source);
// 
// }
// 
// Path: hegemon-core/src/main/java/com/cueup/hegemon/compilation/SimpleScriptCompilation.java
// public class SimpleScriptCompilation implements ScriptCompilation {
// 
//   private final int optimizationLevel;
// 
// 
//   public SimpleScriptCompilation(int optimizationLevel) {
//     this.optimizationLevel = optimizationLevel;
//   }
// 
// 
//   @Override
//   public Script compile(Context c, String name, String source) {
//     int oldLevel = c.getOptimizationLevel();
//     c.setOptimizationLevel(this.optimizationLevel);
//     Script result = c.compileString(source, name, 1, null);
//     c.setOptimizationLevel(oldLevel);
//     return result;
//   }
// 
// }


// Path: hegemon-core/src/main/java/com/cueup/hegemon/ScriptCache.java
import com.cueup.hegemon.compilation.CachedScriptCompilation;
import com.cueup.hegemon.compilation.ScriptCompilation;
import com.cueup.hegemon.compilation.SimpleScriptCompilation;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import javax.script.ScriptException;
import java.util.concurrent.ExecutionException;

/*
 * Copyright 2012 the hegemon authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cueup.hegemon;



/**
 * Cache of scripts.
 */
public class ScriptCache {

  private final LoadingCache<String, Script> cache;


  private static final ScriptCompilation DEFAULT_COMPILATION = new CachedScriptCompilation(
      new SimpleScriptCompilation(9));

  /**
   * Create a ScriptCache that loads scripts from the given LoadPath.
   * @param loadPath the LoadPath to load files from.
   */
  public ScriptCache(final LoadPath loadPath) {
    this(loadPath, DEFAULT_COMPILATION);
  }


  /**
   * Create a ScriptCache that loads scripts from the given LoadPath.
   * @param loadPath the LoadPath to load files from.
   * @param scriptCompilation the strategy to use to compile scripts.
   */
  public ScriptCache(final LoadPath loadPath, final ScriptCompilation scriptCompilation) {