/*
 * Copyright 2016 requery.io
 *
 * 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 io.requery.cache;

import io.requery.EntityCache;
import io.requery.meta.EntityModel;

import javax.cache.CacheManager;
import java.util.LinkedList;

public class EntityCacheBuilder {

    private final EntityModel model;
    private boolean useReferenceCache;
    private boolean useSerializableCache;
    private CacheManager cacheManager;

    public EntityCacheBuilder(EntityModel model) {
        if (model == null) {
            throw new IllegalArgumentException();
        }
        this.model = model;
    }

    public EntityCacheBuilder useReferenceCache(boolean enable) {
        this.useReferenceCache = enable;
        return this;
    }

    public EntityCacheBuilder useSerializableCache(boolean enable) {
        this.useSerializableCache = enable;
        return this;
    }

    public EntityCacheBuilder useCacheManager(CacheManager cacheManager) {
        this.cacheManager = cacheManager;
        return this;
    }

    public EntityCache build() {
        LinkedList<EntityCache> caches = new LinkedList<>();
        if (useReferenceCache) {
            caches.add(new WeakEntityCache());
        }
        if (useSerializableCache) {
            SerializationContext.map(model);
            caches.add(new SerializableEntityCache(model, cacheManager));
        }
        if (caches.isEmpty()) {
            return new EmptyEntityCache();
        } else {
            return new LayeredEntityCache(caches);
        }
    }
}

@@ -746,16 +746,133 @@
 dList;%0A%0A
+/**%0A * Builder for creating the appropriate %7B@link EntityCache%7D instance based on the type of caching%0A * needed.%0A */%0A
 public c
@@ -1056,16 +1056,117 @@
 nager;%0A%0A
+    /**%0A     * Creates a new %7B@link EntityCacheBuilder%7D instance.%0A     * @param model to use%0A     */%0A
     publ
@@ -1340,138 +1340,485 @@
     
-public EntityCacheBuilder useReferenceCache(boolean enable) %7B%0A        this.useReferenceCache = enable;%0A        return this;%0A    %7D%0A
+/**%0A     * Enables or disables reference (memory) caching.%0A     * @param enable true to enable reference caching false otherwise.%0A     * @return builder instance%0A     */%0A    public EntityCacheBuilder useReferenceCache(boolean enable) %7B%0A        this.useReferenceCache = enable;%0A        return this;%0A    %7D%0A%0A    /**%0A     * Enables or disables serialization caching.%0A     * @param enable true to enable serialization caching false otherwise.%0A     * @return builder instance%0A     */
 %0A   
@@ -1947,32 +1947,162 @@
 rn this;%0A    %7D%0A%0A
+    /**%0A     * Sets the %7B@link CacheManager%7D. to use.%0A     * @param cacheManager instance%0A     * @return builder instance%0A     */%0A
     public Entit
@@ -2226,24 +2226,106 @@
 his;%0A    %7D%0A%0A
+    /**%0A     * @return A %7B@link EntityCache%7D with the configured options.%0A     */%0A
     public E

Add javadoc