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

com.maxifier.mxcache.impl.caches.batch.BatchObjectObjectStorage Maven / Gradle / Ivy

/*
 * Copyright (c) 2008-2014 Maxifier Ltd. All Rights Reserved.
 */
package com.maxifier.mxcache.impl.caches.batch;

import com.maxifier.mxcache.caches.Calculable;
import com.maxifier.mxcache.caches.ObjectObjectCalculatable;
import com.maxifier.mxcache.storage.CalculableInterceptor;
import com.maxifier.mxcache.storage.ObjectObjectStorage;
import javax.annotation.Nullable;

/**
 * @author Alexander Kochurov ([email protected])
 */
public class BatchObjectObjectStorage
        implements ObjectObjectStorage, CalculableInterceptor, ObjectObjectCalculatable {

    private final ObjectObjectStorage storage;

    private final KeyStrategy keyStrategy;
    private final ValueStrategy valueStrategy;

    private final Class valueType;

    private ObjectObjectCalculatable realCalculable;

    private C composition;
    private K unknownKey;
    private V knownValue;

    protected BatchObjectObjectStorage(ObjectObjectStorage storage,
                                       KeyStrategy keyStrategy,
                                       ValueStrategy valueStrategy,

                                       Class valueType) {
        this.storage = storage;
        this.keyStrategy = keyStrategy;
        this.valueStrategy = valueStrategy;
        this.valueType = valueType;
    }

    @Override
    public Object load(K key) {
        decompose(key);
        if (unknownKey == null) {
            V res = knownValue;
            reset();
            return res;
        }
        return UNDEFINED;
    }
    
    protected Object loadElement(KeyElement element) {
        return storage.load(element);
    }

    @Override
    public Calculable createInterceptedCalculable(Calculable calculable) {
        //noinspection unchecked
        this.realCalculable = (ObjectObjectCalculatable) calculable;
        return this;
    }

    @Override
    public void save(K key, Object value) {
        reset();
    }

    void set(V result, K unknownKey, @Nullable C composition) {
        this.knownValue = result;
        this.unknownKey = unknownKey;
        this.composition = composition;
    }

    void reset() {
        knownValue = null;
        unknownKey = null;
        composition = null;
    }

    protected void decompose(K key) {
        KeyStrategy keyStrategy = this.keyStrategy;
        ValueStrategy valueStrategy = this.valueStrategy;

        K unknownKey = keyStrategy.create(key);
        int n = keyStrategy.size(key);
        V knownValues = valueStrategy.createValue(valueType, n);
        C composer = valueStrategy.createComposer(n);

        int unknownCount = 0;

        KeyIterator it = keyStrategy.iterator(key);
        for (int i = 0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy