/*
 * Copyright (C) 2015 Mobile Jazz
 *
 *  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.mobilejazz.cacheio;

import android.content.Context;
import android.text.TextUtils;
import com.mobilejazz.cacheio.internal.helper.Preconditions;
import com.mobilejazz.cacheio.logging.AndroidLogger;
import com.mobilejazz.cacheio.logging.LogLevel;
import com.mobilejazz.cacheio.logging.Logger;
import com.mobilejazz.cacheio.persistence.Persistence;
import com.mobilejazz.cacheio.persistence.sqlbrite.PersistenceSQLBrite;
import com.mobilejazz.cacheio.serializer.Serializer;

public class CacheIO {

  private final Cache cache;

  private CacheIO(Persistence persistence, Serializer serializer, Logger logger,
      LogLevel logLevel) {
    this.cache = new CacheManager(persistence, serializer, logger, logLevel);
  }

  public Cache cacheDataSource() {
    Preconditions.checkNotNull(cache, "cache == null! Create one using Builder instance!");
    return cache;
  }

  public static Builder with(Context context) {
    return new Builder(context);
  }

  public static final class Builder {

    private Persistence persistence;
    private Serializer serializer;
    private Logger logger;
    private LogLevel logLevel;
    private String identifier;
    private Context context;

    public Builder(Context context) {
      Preconditions.checkArgument(context, "context == null");
      this.context = context;
    }

    public Builder serializer(Serializer serializer) {
      this.serializer = Preconditions.checkArgument(serializer, "serializer == null");
      return this;
    }

    public Builder identifier(String identifier) {
      if (TextUtils.isEmpty(identifier)) {
        throw new IllegalArgumentException("identifier == null OR database == empty");
      }

      this.identifier = identifier;
      return this;
    }

    public Builder logLevel(LogLevel logLevel) {
      this.logLevel = Preconditions.checkArgument(logLevel, "logLevel == null");
      return this;
    }

    public CacheIO build() {
      if (persistence == null) {
        persistence = new PersistenceSQLBrite(PersistenceSQLBrite.generate(context, identifier));
      }

      if (logger == null) {
        this.logger = new AndroidLogger();
      }

      return new CacheIO(persistence, serializer, logger, logLevel);
    }
  }
}

@@ -1374,100 +1374,8 @@
 ) %7B%0A
-    Preconditions.checkNotNull(cache, %22cache == null! Create one using Builder instance!%22);%0A
     
@@ -1751,71 +1751,8 @@
 ) %7B%0A
-      Preconditions.checkArgument(context, %22context == null%22);%0A
     
@@ -1867,69 +1867,18 @@
 r = 
-Preconditions.checkArgument(serializer, %22serializer == null%22)
+serializer
 ;%0A  
@@ -1956,147 +1956,8 @@
 ) %7B%0A
-      if (TextUtils.isEmpty(identifier)) %7B%0A        throw new IllegalArgumentException(%22identifier == null OR database == empty%22);%0A      %7D%0A%0A
     
@@ -2089,65 +2089,16 @@
 l = 
-Preconditions.checkArgument(logLevel, %22logLevel == null%22)
+logLevel
 ;%0A  
@@ -2146,24 +2146,49 @@
 O build() %7B%0A
+      checkArguments();%0A%0A
       if (pe
@@ -2469,15 +2469,394 @@
 );%0A    %7D
+%0A%0A    private void checkArguments() %7B%0A      Preconditions.checkArgument(context, %22context == null%22);%0A      Preconditions.checkArgument(logLevel, %22logLevel == null%22);%0A      Preconditions.checkArgument(serializer, %22serializer == null%22);%0A%0A      if (TextUtils.isEmpty(identifier)) %7B%0A        throw new IllegalArgumentException(%22identifier == null OR database == empty%22);%0A      %7D%0A    %7D
 %0A  %7D%0A%7D%0A

Refactor the check arguments of CacheIO