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

com.am.job.core.HashMapParams Maven / Gradle / Ivy

Go to download

The basic core of thread asynchrony is independent of the platform and facilitates the further realization of thread asynchrony in Android or HarmonyOS.

There is a newer version: 2.0.3
Show newest version
/*
 * Copyright (C) 2021 AlexMofer
 *
 * 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.am.job.core;

import java.lang.ref.WeakReference;
import java.util.HashMap;

/**
 * 使用HashMap实现的任务参数
 * Created by Alex on 2021/3/12.
 */
class HashMapParams extends HashMapDataArray implements BaseJob.Params {

    private final HashMap> mWeakParams = new HashMap<>();

    private boolean contains(int key) {
        return mWeakParams.containsKey(key);
    }

    @Override
    public void clearWeak() {
        mWeakParams.clear();
    }

    @Override
    public void putWeak(int key, Object value) {
        if (value == null) {
            mWeakParams.remove(key);
            return;
        }
        mWeakParams.put(key, new WeakReference<>(value));
    }

    @Override
    public  V getWeak(int key, V defaultValue) {
        if (contains(key)) {
            final WeakReference reference = mWeakParams.get(key);
            final Object value = reference == null ? null : reference.get();
            //noinspection unchecked
            return value == null ? defaultValue : (V) value;
        } else {
            return defaultValue;
        }
    }
}