All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.gh.bmd.jrt.android.v4.routine.DefaultAndroidRoutineBuilder Maven / Gradle / Ivy

There is a newer version: 6.0.0
Show newest version
/**
 * 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.gh.bmd.jrt.android.v4.routine;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;

import com.gh.bmd.jrt.android.builder.AndroidRoutineBuilder;
import com.gh.bmd.jrt.android.invocation.AndroidInvocation;
import com.gh.bmd.jrt.android.runner.Runners;
import com.gh.bmd.jrt.builder.RoutineConfiguration;
import com.gh.bmd.jrt.builder.RoutineConfiguration.Builder;
import com.gh.bmd.jrt.common.ClassToken;
import com.gh.bmd.jrt.log.Logger;
import com.gh.bmd.jrt.routine.Routine;
import com.gh.bmd.jrt.runner.Runner;
import com.gh.bmd.jrt.time.TimeDuration;

import java.lang.ref.WeakReference;
import java.lang.reflect.Constructor;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import static com.gh.bmd.jrt.common.Reflection.findConstructor;

/**
 * Default implementation of an Android routine builder.
 * 

* Created by davide on 12/9/14. * * @param the input data type. * @param the output data type. */ class DefaultAndroidRoutineBuilder implements AndroidRoutineBuilder { private final Constructor> mConstructor; private final WeakReference mContext; private CacheStrategy mCacheStrategy; private ClashResolution mClashResolution; private RoutineConfiguration mConfiguration; private int mLoaderId = AndroidRoutineBuilder.AUTO; /** * Constructor. * * @param activity the context activity. * @param classToken the invocation class token. * @throws java.lang.NullPointerException if the activity or class token are null. */ DefaultAndroidRoutineBuilder(@Nonnull final FragmentActivity activity, @Nonnull final ClassToken> classToken) { this((Object) activity, classToken); } /** * Constructor. * * @param fragment the context fragment. * @param classToken the invocation class token. * @throws java.lang.NullPointerException if the fragment or class token are null. */ DefaultAndroidRoutineBuilder(@Nonnull final Fragment fragment, @Nonnull final ClassToken> classToken) { this((Object) fragment, classToken); } /** * Constructor. * * @param context the context instance. * @param classToken the invocation class token. * @throws java.lang.NullPointerException if the context or class token are null. */ @SuppressWarnings("ConstantConditions") private DefaultAndroidRoutineBuilder(@Nonnull final Object context, @Nonnull final ClassToken> classToken) { if (context == null) { throw new NullPointerException("the routine context must not be null"); } mContext = new WeakReference(context); mConstructor = findConstructor(classToken.getRawClass()); } @Nonnull @Override public Routine buildRoutine() { final RoutineConfiguration configuration = RoutineConfiguration.notNull(mConfiguration); warn(configuration); final Builder builder = configuration.builderFrom(); return new AndroidRoutine(builder.withRunner(Runners.mainRunner()) .withInputSize(Integer.MAX_VALUE) .withInputTimeout(TimeDuration.INFINITY) .withOutputSize(Integer.MAX_VALUE) .withOutputTimeout(TimeDuration.INFINITY) .buildConfiguration(), mContext, mLoaderId, mClashResolution, mCacheStrategy, mConstructor); } @Nonnull @Override public AndroidRoutineBuilder onClash( @Nullable final ClashResolution resolution) { mClashResolution = resolution; return this; } @Nonnull @Override public AndroidRoutineBuilder onComplete( @Nullable final CacheStrategy cacheStrategy) { mCacheStrategy = cacheStrategy; return this; } @Nonnull @Override public AndroidRoutineBuilder withConfiguration( @Nullable final RoutineConfiguration configuration) { mConfiguration = configuration; return this; } @Nonnull @Override public AndroidRoutineBuilder withId(final int id) { mLoaderId = id; return this; } /** * Logs any warning related to ignored options in the specified configuration. * * @param configuration the routine configuration. */ private void warn(@Nonnull final RoutineConfiguration configuration) { Logger logger = null; final Runner runner = configuration.getRunnerOr(null); if (runner != null) { logger = Logger.newLogger(configuration, this); logger.wrn("the specified runner will be ignored: %s", runner); } final int inputSize = configuration.getInputSizeOr(RoutineConfiguration.DEFAULT); if (inputSize != RoutineConfiguration.DEFAULT) { if (logger == null) { logger = Logger.newLogger(configuration, this); } logger.wrn("the specified maximum input size will be ignored: %d", inputSize); } final TimeDuration inputTimeout = configuration.getInputTimeoutOr(null); if (inputTimeout != null) { if (logger == null) { logger = Logger.newLogger(configuration, this); } logger.wrn("the specified input timeout will be ignored: %s", inputTimeout); } final int outputSize = configuration.getOutputSizeOr(RoutineConfiguration.DEFAULT); if (outputSize != RoutineConfiguration.DEFAULT) { if (logger == null) { logger = Logger.newLogger(configuration, this); } logger.wrn("the specified maximum output size will be ignored: %d", outputSize); } final TimeDuration outputTimeout = configuration.getOutputTimeoutOr(null); if (outputTimeout != null) { if (logger == null) { logger = Logger.newLogger(configuration, this); } logger.wrn("the specified output timeout will be ignored: %s", outputTimeout); } } }