I need to store a TDSContent into a hashmap specifying a String key. I need to do something like this.
public class Target {
public Target() {
final int a = 0x64c896;
final int b = 0xc050be;
final int c = 0xffc896;
TDS = new HashMap<String, TDSContent>();
TDSContent contentMA = new TDSContent(a, b, c, 10);
TDSContent contentMB = new TDSContent(a, c, b, 10);
TDSContent contentMC = new TDSContent(b, a, c, 10);
TDSContent contentMD = new TDSContent(c, b, a, 10);
TDSContent contentME = new TDSContent(c, a, b, 10);
TDSContent contentMF = new TDSContent(b, c, a, 10);
... // and so on...
TDS.put("Marker A", contentMA);
TDS.put("Marker B", contentMB);
TDS.put("Marker C", contentMC);
TDS.put("Marker D", contentMD);
TDS.put("Marker E", contentME);
TDS.put("Marker F", contentMF);
... // and so on...
}
public int getCL(String key) {
TDSContent tdc = TDS.get(key);
if(tdc.equals(contentMA)) {
return contentMA.getValue();
} else if(tdc.equals(contentMB)) {
return contentMB.getValue();
} else if(tdc.equals(contentMC)) {
return contentMC.getValue();
} else if(tdc.equals(contentMD)) {
return contentMD.getValue();
} else if(tdc.equals(contentME)) {
return contentME.getValue();
} else if(tdc.equals(contentMF)) {
return contentMF.getValue();
} ...// and so on...
else {
return contentMD.getValue();
}
}
}
The problem is, it will take so much hard work to manually create an object of class TDSContent.
Can I do something like this …:
public class Target {
public Target() {
final int a = 0x64c896;
final int b = 0xc050be;
final int c = 0xffc896;
TDS = new HashMap<String, TDSContent>();
// form: new TDSContent(CL, CM, CR, D);
TDS.put("Marker A", new TDSContent(a, b, c, 10));
TDS.put("Marker B", new TDSContent(a, c, b, 10));
... // and so on..
}
public int getCL(String key) {
// this method gets the first parameter of the TDSContent
// constructor (see comment above).
return TDS.get(key).getValue();
}
public int getCM(String key) {
... // similar to getCL but returns the second parameter of the TDSContent
// constructor (see comment above)
… on getCL() and get and the instantiated[?] value of TDSContent?
Specifically, if I have a call on something like:
Target target = new Target();
int x1 = target.getCL("Marker A"); // I should get 0x64c896 here
int x2 = target.getCM("Marker A"); // I should get 0xc050be here
Is that possible?
Certainly, just match the
getXXX()method you call on theTDSContentobject in the map to the accessor. This assumes you have accessors onTDSContentfor each constructor argument.