java - How to manage variables for multiple objects -


i trying assign properties each object. cannot modify filesystemusage class of sigar library, had make class manage propoerties.

the function generating objects 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();         (int = 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 made managing properties each object 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 can't manage keep properties separate each object. mean when object usage generated check lastdickcheck variable calling diskm.getlastdiskcheck(i-1). first time won't execute section of code because return value zero(0). after execute every time different object long array is. want keep record of "lastdiskcheck" each object "usage", each (disk).

if update lastdiskcheck variable disk c: picks disk d well, don't what.

how can that?

i try explain goal of setters , getters. in case, there different problems need explained.

in first function getdiskusage, there private method. can access method same class. don't know if objective.

moreover, if want access 3 different disks, instance 3 three different objects, example: diskmembers[] disks = new diskmembers[3]; //c d , e

in case, each object access getters/setters. attributes class diskmembers should be:

private boolean _diskcheck; private boolean _diskread; private boolean _diskwrite; 

each time modify disk, access of way: disks[0].set_diskcheck() = true;

in addition, in each instance have want. check last disk, can add attribute date, keep date of last modify.

lastly, recommend ensure setter method keep value, not return something. in other side, have getter, return value of private attribute. important thing if need create complex setter, better create more methods can access setter.

pd: try java object-oriented programming book , start reading, improve level in short period of time.


Comments

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -