java - ArrayList of class, get specific value created in another class -
may title isn't specific one, don't know how call it. explain in detail
i have these classes:
public class channelcomponent { private string name; private string mode; //(1p1c / xpxc / 1pxc) private list<sourceprovidedport> publishers = new arraylist<sourceprovidedport>(); private list<sinkrequiredport> subscribers = new arraylist<sinkrequiredport>(); public channelcomponent(string name, string mode) { this.name = name; this.mode = mode; } public boolean canisubscribe(sinkrequiredport newport) { if ((mode.equals("1p1c") || mode.equals("1pxc")) && subscribers.size() < 1) { subscribers.add(newport); return true; } else if (mode.equals("xpxc")) { subscribers.add(newport); return true; } return false; } public string getname() { return name; } public string getmode() { return mode; } public void printchannel() { system.out.println("[" + name + "," + mode + "]" + "\n"); } }
testcentralregistry
public class testcentralregistry { private list<channelcomponent> channels = new arraylist<channelcomponent>(); public void addchannelcomponent(channelcomponent c) { channels.add(c); } public static void main(string... args) { testcentralregistry demo = new testcentralregistry(); demo.addchannelcomponent(new channelcomponent("channel1", "1p1c")); demo.addchannelcomponent(new channelcomponent("channel2", "xpxc")); } }
in testcentralregistry class created 2 channelcomponents, these channels compare mode value in method canisubscribe (located in channelcomponent class). how come, retrieve values created in testcentralregistry read them in channelcomponent class?
what missing?
because, class testchannel i'm going have channelcomponent reference, invoke method canisubscribe
public class testchannel { channelcomponent channelcomponent; public void callsubscribe(sinkrequiredport newport){ channelcomponent.canisubscribe(newport); } public static void main(string... args) { testchannel testchannel = new testchannel(); sinkrequiredport sinkport = new sinkrequiredport(); sinkport.setwantsuse("channel1"); testchannel.callsubscribe(sinkport); } }
and need compare values, created in testcentralregistry , testchannel see if there matching. know still need add lines getting value newport.getwantsuse(); , compare channelcomponent name ... still need value created in testcentralregistry
i hope question clear suggestions? thank in advance
try holding reference testcentralregistry in channelcomponent.
public class channelcomponent { private string name; private string mode; //(1p1c / xpxc / 1pxc) private list<sourceprovidedport> publishers = new arraylist<sourceprovidedport>(); private list<sinkrequiredport> subscribers = new arraylist<sinkrequiredport>(); private testcentralregistry testcentralregistry; public channelcomponent(string name, string mode) { this.name = name; this.mode = mode; } public void registertestcentralregistry( testcentralregistry testcentralregistry) { this.testcentralregistry = testcentralregistry; } }
register testcentralregistry shown below:
public class testcentralregistry { private list<channelcomponent> channels = new arraylist<channelcomponent>(); public void addchannelcomponent(channelcomponent c) { channels.add(c); } public static void main(string... args) { testcentralregistry demo = new testcentralregistry(); channelcomponent cc1 = new channelcomponent("channel1", "1p1c"); cc1.registertestcentralregistry( demo); channelcomponent cc2 = new channelcomponent("channel2", "xpxc"); cc2.registertestcentralregistry( demo); demo.addchannelcomponent( cc1); demo.addchannelcomponent( cc2); } }
then, can retrieve values created in testcentralregistry calling testcentralregistry.getx()
channelcomponent.
Comments
Post a Comment