简体   繁体   中英

How to programme progress when max value is not known before a method is called?

On the remote server, there is a 5-digit numbers of particles. As they are being downloaded, the progress has to be updated. The number of particles is not known before the 3rd party downloader starts. So my methods look like this

//Method calling progress counter
@Override
public void onUpdated(Object fetcher, int particlesRemaining) {
    //particlesRemaining is int like 10240
    int progress = (int) countProgress(particlesRemaining);
    setProgress(progress);
}

//method counting progress
public static double countProgress(int particlesRemaining) {
    //???how to set maxValue???
    double temp = 100 / maxValue * (maxValue - particlesRemaining);
    return temp;
}

A parameter particlesRemaining is not known before I first time call countProgress . The first-time value is always the Max value and all subsequent values are smaller than the first one.

So how shall I set maxValue once when I don't know it's value before countProgress has been called for the first time ? After setting is first time as the max value, I have to lock it from changing.

Wouldn't the first 'particlesRemaining' be the max value?

then you could do:

//in class variables
int maxValue=0;

//method counting progress
public static double countProgress(int particlesRemaining) {

    //???how to set maxValue???
    if(maxValue<particlesRemaining) {
        maxValue=particlesRemaining;}
        double temp = 100 / maxValue * (maxValue - particlesRemaining);
        return temp;
    }
}

Don't forget if you use it several times you need reset maxValue before use

Add field.

int maxValue;

Change your onUpdated

public void onUpdated(Object fetcher, int particlesRemaining) {
    if(maxValue =n null) {
          maxValue = particlesRemaining;
          setProgress(0);
    } else{
         setProgress(100 / maxValue * (maxValue - particlesRemaining));
    }
}

maxValue = INTEGER.MAX_VALUE将为您提供Java可以处理的最大数量

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM