com.mfinatti.truckfinder.data.TrucksServiceApiImpl.java Source code

Java tutorial

Introduction

Here is the source code for com.mfinatti.truckfinder.data.TrucksServiceApiImpl.java

Source

/*
 * Copyright 2015, The Android Open Source Project
 *
 * 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.mfinatti.truckfinder.data;

import android.os.Handler;
import android.support.v4.util.ArrayMap;

import java.util.ArrayList;
import java.util.List;

/**
 * Implementation of the Trucks Service API that adds a latency simulating network.
 */
public class TrucksServiceApiImpl implements TrucksServiceApi {

    private static final int SERVICE_LATENCY_IN_MILLIS = 2000;
    private static final ArrayMap<String, Truck> TRUCKS_SERVICE_DATA = TrucksServiceApiEndpoint
            .loadPersistedTrucks();

    @Override
    public void getAllTrucks(final TrucksServiceCallback callback) {
        // Simulate network by delaying the execution.
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                List<Truck> Trucks = new ArrayList<>(TRUCKS_SERVICE_DATA.values());
                callback.onLoaded(Trucks);
            }
        }, SERVICE_LATENCY_IN_MILLIS);
    }

    @Override
    public void getTruck(final String truckId, final TrucksServiceCallback callback) {
        //TODO: Add network latency here too.
        Truck Truck = TRUCKS_SERVICE_DATA.get(truckId);
        callback.onLoaded(Truck);
    }

    @Override
    public void saveTruck(Truck Truck) {
        TRUCKS_SERVICE_DATA.put(Truck.getId(), Truck);
    }

}