I am trying to assign properties to each object. I cannot modify the FileSystemUsage class of Sigar library, so i had to make another class to manage the propoerties.
The function where i am generating the objects is the following
private String getDiskUsage() {
try {
List<FileSystem> result = new ArrayList<FileSystem>();
org.hyperic.sigar.FileSystem[] fss = null;
fss = sig.getFileSystemList();
List<Integer> diskUsage = new ArrayList<Integer>();
String ioStat = "";
DiskMembers diskm = new DiskMembers();
for (int i = 0; i<fss.length; i++) {
FileSystem fs = fss[i];
if (fs.getType()==2) {
FileSystemUsage usage = sig.getFileSystemUsage(fs.getDirName());
if (diskm.getlastDiskCheck(i - 1) != 0) {
long diff = (nanoTime() - diskm.getlastDiskCheck(i - 1)) / 1000000;
long diffRead = usage.getDiskReadBytes() - diskm.getLastDiskRead(i - 1);
long diffWrite = usage.getDiskWriteBytes() - diskm.getlastDiskWrite(i - 1);
ioStat = String.format("(%d/%d kB/s)", diffRead / diff, diffWrite / diff);
}
diskm.setLastDiskRead(i, usage.getDiskReadBytes());
diskm.setlastDiskWrite(i, usage.getDiskWriteBytes());
diskm.setlastDiskCheck(i, nanoTime());
diskUsage.add((int) (usage.getUsePercent() * 100));
}
}
return String.format("%d%% %s", (Collections.max(diskUsage)), ioStat);
} catch (SigarException e) {
e.printStackTrace();
}
return "n/a";
}
The class that i made for managing the properties for each object is the following
class DiskMembers{
private long[] lastDiskCheck = new long[5];
private long[] lastDiskRead = new long[5];
private long[] lastDiskWrite = new long[5];
public DiskMembers(){
this.lastDiskCheck[0] = 0;
this.lastDiskRead[0] = 0;
this.lastDiskWrite[0] = 0;
}
public boolean setLastDiskRead(int i, long value){
this.lastDiskRead[i] = value;
return true;
}
public boolean setlastDiskCheck(int i,long value){
this.lastDiskCheck[i] = value;
return true;
}
public boolean setlastDiskWrite(int i,long value){
this.lastDiskWrite[i] = value;
return true;
}
public long getLastDiskRead(int i){
if (i==-1)
return this.lastDiskRead[0];
return this.lastDiskRead[i];
}
public long getlastDiskCheck(int i){
if (i==-1)
return this.lastDiskCheck[0];
return this.lastDiskCheck[i];
}
public long getlastDiskWrite(int i){
if (i==-1)
return this.lastDiskWrite[0];
return this.lastDiskWrite[i];
}
}
The problem is i can't manage to keep properties separate for each object. I mean when object usage is generated i check the lastDickCheck variable by calling diskm.getlastDiskCheck(i-1). The first time it won't execute this section of code because the return value is zero(0). But after that i will execute it every time even for the different object as long as they array is. I want to keep record of "lastdiskcheck" for each object "usage", for each (disk).
If i update lastDiskCheck variable for disk C: It picks it up for the disk D as well, which i don't what.
How can i do that?
See Question&Answers more detail:os