open-coap / java-coap

CoAP Java library
Apache License 2.0
18 stars 5 forks source link

GH-82 TransportContext: merge and key iterator #85

Open szysas opened 2 months ago

sbernard31 commented 2 months ago

It's take me few minutes to get how "merge with override" works. :thinking:
Actually, when you merge if there is 2 value with same key both are kept in the chained list but only the first one will be returned by the get(), correct ?

That's surprising me but if it works why not. I guess one drawback is that merged context takes probably a bit more memory than necessary.

I didn't test it but reading the code I guess that :

TransportContext ctx1 = TransportContext.of(DUMMY_KEY, "111").with(DUMMY_KEY2, "222");
TransportContext ctx2 = TransportContext.of(DUMMY_KEY, "aaa");
TransportContext ctx1Ctx2 = ctx1.with(ctx2);

TransportContext ctx3 = TransportContext.of(DUMMY_KEY, "aaa").with(DUMMY_KEY2, "222");   
// OR TransportContext ctx3 = TransportContext.of(DUMMY_KEY2, "222").with(DUMMY_KEY, "aaa");  
// not sure about the merge order : this makes me think 
// that maybe a better name would be addFirst() or addLast() (instead of with())
// Just because order seems relevant for equals()

assertEquals(ctx1Ctx2, ctx3); 
// From user point of view, I guess this should be equal, but is it ? 
// reading the code I'm not sure. 

and also if I iterate over ctx1Ctx2 and print all key, I will get DUMMY_KEY twice correct ?

(I maybe totally missed something, sorry I didn't take time to execute code)

szysas commented 2 months ago

and also if I iterate over ctx1Ctx2 and print all key, I will get DUMMY_KEY twice correct ?

Yes, that is correct.