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

org.javers.core.commit.HandedOutIds Maven / Gradle / Ivy

There is a newer version: 7.6.1
Show newest version
package org.javers.core.commit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * @author bartosz.walacik
 */
class HandedOutIds {
    private static final Logger logger = LoggerFactory.getLogger(HandedOutIds.class);

    private int qLimit = 50;

    private List handedOutList = new ArrayList<>();

    void put (CommitId handedOut) {

        int found = findExistingIndex(handedOut.getMajorId());

        if (found >= 0){
            handedOutList.remove(found);
            handedOutList.add(found, handedOut);
            maintainQueueSize(found);
        } else {
            int insertTo = findInsertIndex(handedOut.getMajorId());
            handedOutList.add(insertTo, handedOut);
            maintainQueueSize(insertTo);
        }

    }

    private void maintainQueueSize(int touchedIndex) {
        if (touchedIndex < qLimit /2) {
            if (handedOutList.size() > qLimit) {
                handedOutList.remove(handedOutList.size() - 1);
            }
        }
        else {
            qLimit += qLimit/10;
        }

    }

    private int findInsertIndex(Long majorId) {
        if (handedOutList.size() == 0){
            return 0;
        }

        int i = 0;
        while (i < handedOutList.size() &&  handedOutList.get(i).getMajorId() > majorId) {
            i++;
        }

        if (i == handedOutList.size()) {
            logger.error("DANGER, inserting {} at the end of handedOutList: ",majorId);
            logger.error(handedOutList.toString());
        }

        return i;
    }

    private int findExistingIndex(Long majorId){
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy