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

hivemall.mix.store.SessionObject Maven / Gradle / Ivy

The newest version!
/*
 * Hivemall: Hive scalable Machine Learning Library
 *
 * Copyright (C) 2015 Makoto YUI
 * Copyright (C) 2013-2015 National Institute of Advanced Industrial Science and Technology (AIST)
 *
 * 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 hivemall.mix.store;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;

import javax.annotation.Nonnull;
import javax.annotation.concurrent.ThreadSafe;

@ThreadSafe
public final class SessionObject {

    @Nonnull
    private final ConcurrentMap object;
    private volatile long lastAccessed; // being accessed by multiple threads

    private final AtomicLong num_requests;
    private final AtomicLong num_responses;

    public SessionObject(@Nonnull ConcurrentMap obj) {
        if (obj == null) {
            throw new IllegalArgumentException("obj is null");
        }
        this.object = obj;
        this.num_requests = new AtomicLong(0L);
        this.num_responses = new AtomicLong(0L);
    }

    @Nonnull
    public ConcurrentMap get() {
        return object;
    }

    /**
     * @return last accessed time in msec
     */
    public long getLastAccessed() {
        return lastAccessed;
    }

    public void incrRequest() {
        this.lastAccessed = System.currentTimeMillis();
        num_requests.getAndIncrement();
    }

    public void incrResponse() {
        num_responses.getAndIncrement();
    }

    public long getRequests() {
        return num_requests.get();
    }

    public long getResponses() {
        return num_responses.get();
    }

    public String getSessionInfo() {
        long requests = num_requests.get();
        long responses = num_responses.get();
        float percentage = ((float) ((double) responses / requests)) * 100.f;
        return "#requests: " + requests + ", #responses: " + responses + " ("
                + String.format("%,.2f", percentage) + "%)";
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy