DefaultTopCodeCacheFactory.java

  1. /*
  2.    Copyright 2014-now by Alain Stalder. Made in Switzerland.

  3.    Licensed under the Apache License, Version 2.0 (the "License");
  4.    you may not use this file except in compliance with the License.
  5.    You may obtain a copy of the License at

  6.        https://www.apache.org/licenses/LICENSE-2.0

  7.    Unless required by applicable law or agreed to in writing, software
  8.    distributed under the License is distributed on an "AS IS" BASIS,
  9.    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10.    See the License for the specific language governing permissions and
  11.    limitations under the License.
  12. */

  13. package ch.grengine.load;

  14. import ch.grengine.code.CompilerFactory;
  15. import ch.grengine.code.groovy.DefaultGroovyCompilerFactory;

  16. import static java.util.Objects.requireNonNull;


  17. /**
  18.  * Factory for instances of {@link DefaultTopCodeCache}
  19.  *
  20.  * @since 1.0
  21.  *
  22.  * @author Alain Stalder
  23.  * @author Made in Switzerland.
  24.  */
  25. public class DefaultTopCodeCacheFactory implements TopCodeCacheFactory {
  26.    
  27.     private final Builder builder;
  28.     private final CompilerFactory compilerFactory;
  29.    
  30.     /**
  31.      * constructor from builder.
  32.      *
  33.      * @param builder builder
  34.      *
  35.      * @since 1.0
  36.      */
  37.     protected DefaultTopCodeCacheFactory(final Builder builder) {
  38.         this.builder = builder.commit();
  39.         compilerFactory = builder.getCompilerFactory();
  40.     }
  41.    
  42.     /**
  43.      * constructor from defaults for all settings.
  44.      *
  45.      * @since 1.0
  46.      */
  47.     public DefaultTopCodeCacheFactory() {
  48.         this(new Builder());
  49.     }
  50.    
  51.     /**
  52.      * constructor from given compiler factory and defaults for all other settings.
  53.      *
  54.      * @param compilerFactory compiler factory
  55.      *
  56.      * @throws NullPointerException if the compiler factory is null
  57.      *
  58.      * @since 1.0
  59.      */
  60.     public DefaultTopCodeCacheFactory(CompilerFactory compilerFactory) {
  61.         this(new Builder().setCompilerFactory(compilerFactory));
  62.         requireNonNull(compilerFactory, "Compiler factory is null.");
  63.     }
  64.    
  65.     @Override
  66.     public TopCodeCache newTopCodeCache(final ClassLoader parent) {
  67.         return new DefaultTopCodeCache.Builder(parent)
  68.                 .setCompilerFactory(compilerFactory)
  69.                 .build();
  70.     }

  71.     /**
  72.      * gets the builder.
  73.      *
  74.      * @return builder
  75.      *
  76.      * @since 1.0
  77.      */
  78.     public Builder getBuilder() {
  79.         return builder;
  80.     }

  81.     /**
  82.      * gets the compiler factory.
  83.      *
  84.      * @return compiler factory
  85.      *
  86.      * @since 1.0
  87.      */
  88.     public CompilerFactory getCompilerFactory() {
  89.         return compilerFactory;
  90.     }
  91.    
  92.    
  93.     /**
  94.      * Builder for instances of {@link DefaultTopCodeCacheFactory}.
  95.      *
  96.      * @since 1.0
  97.      *
  98.      * @author Alain Stalder
  99.      * @author Made in Switzerland.
  100.      */
  101.     public static class Builder {
  102.        
  103.         private boolean isCommitted;
  104.        
  105.         private CompilerFactory compilerFactory;
  106.        
  107.         /**
  108.          * constructor.
  109.          *
  110.          * @since 1.0
  111.          */
  112.         public Builder() {
  113.             isCommitted = false;
  114.         }
  115.        
  116.         /**
  117.          * sets the compiler factory,
  118.          * default is a new instance of {@link DefaultGroovyCompilerFactory} with default settings.
  119.          *
  120.          * @param compilerFactory compiler factory
  121.          *
  122.          * @return this, for chaining calls
  123.          *
  124.          * @since 1.0
  125.          */
  126.         public Builder setCompilerFactory(final CompilerFactory compilerFactory) {
  127.             check();
  128.             this.compilerFactory = compilerFactory;
  129.             return this;
  130.         }
  131.        
  132.         /**
  133.          * gets the compiler factory.
  134.          *
  135.          * @return compiler factory
  136.          *
  137.          * @since 1.0
  138.          */
  139.         public CompilerFactory getCompilerFactory() {
  140.             return compilerFactory;
  141.         }
  142.        
  143.         private Builder commit() {
  144.             if (!isCommitted) {
  145.                 if (compilerFactory == null) {
  146.                     compilerFactory = new DefaultGroovyCompilerFactory();
  147.                 }
  148.                 isCommitted = true;
  149.             }
  150.             return this;
  151.         }
  152.        
  153.         /**
  154.          * builds a new instance of {@link DefaultTopCodeCacheFactory}.
  155.          *
  156.          * @return new instance
  157.          *
  158.          * @since 1.0
  159.          */
  160.         public DefaultTopCodeCacheFactory build() {
  161.             commit();
  162.             return new DefaultTopCodeCacheFactory(this);
  163.        }
  164.        
  165.         private void check() {
  166.             if (isCommitted) {
  167.                 throw new IllegalStateException("Builder already used.");
  168.             }
  169.         }

  170.     }

  171.    
  172. }