package org.cache2k.benchmark;

/*
 * #%L
 * zoo
 * %%
 * Copyright (C) 2013 - 2016 headissue GmbH, Munich
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public 
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/gpl-3.0.html>.
 * #L%
 */

import org.cache2k.Cache;
import org.cache2k.Cache2kBuilder;
import org.cache2k.CacheBuilder;
import org.cache2k.integration.CacheLoader;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author Jens Wilke; created: 2013-12-08
 */
public class Cache2kFactory extends BenchmarkCacheFactory {

  AtomicInteger counter = new AtomicInteger();

  @Override
  public BenchmarkCache<Integer, Integer> create(final int _maxElements) {
    CacheBuilder<Integer, Integer> b =
    CacheBuilder.newCache(Integer.class, Integer.class)
      .name("testCache-" + counter.incrementAndGet())
      .expiryDuration(withExpiry ? 5 * 60 : Integer.MAX_VALUE, TimeUnit.SECONDS)
      .entryCapacity(_maxElements)
      .refreshAhead(false);
    if (withExpiry) {
      b.expiryDuration(5 * 60, TimeUnit.SECONDS);
    } else {
      b.eternal(true);
    }

    final Cache<Integer, Integer> c = b.build();
    return new BenchmarkCache<Integer, Integer>() {

      @Override
      public int getCacheSize() {
        return _maxElements;
      }

      @Override
      public Integer getIfPresent(Integer key) {
        return c.peek(key);
      }

      @Override
      public void put(Integer key, Integer value) {
        c.put(key, value);
      }

      @Override
      public void destroy() {
        c.destroy();
      }

      @Override
      public String getStatistics() {
        return c.toString();
      }

      @Override
      public void checkIntegrity() {
        c.toString();
      }

      @Override
      public Object getOriginalCache() {
        return c;
      }
    };

  }

}

@@ -847,85 +847,8 @@
 der;
-%0Aimport org.cache2k.CacheBuilder;%0Aimport org.cache2k.integration.CacheLoader;
 %0A%0Aim
@@ -1092,16 +1092,52 @@
 teger();
+%0A  boolean disableStatistics = true;
 %0A%0A  @Ove
@@ -1222,24 +1222,26 @@
  %7B%0A    Cache
+2k
 Builder%3CInte
@@ -1266,43 +1266,34 @@
     
+new 
 Cache
+2k
 Builder
-.newCache(
+%3C
 Integer
-.class
 , In
@@ -1301,15 +1301,13 @@
 eger
-.class)
+%3E()%7B%7D
 %0A   
@@ -1361,89 +1361,8 @@
 ())%0A
-      .expiryDuration(withExpiry ? 5 * 60 : Integer.MAX_VALUE, TimeUnit.SECONDS)%0A
     
@@ -1418,16 +1418,44 @@
 d(false)
+%0A      .strictEviction(true)
 ;%0A    if
@@ -1487,17 +1487,19 @@
 xpir
-yDuration
+eAfterWrite
 (5 *
@@ -1560,24 +1560,91 @@
 rue);%0A    %7D%0A
+    if (disableStatistics) %7B%0A      b.disableStatistics(true);%0A    %7D
 %0A    final C

disable statistics by default