porting ColdFusion "structure" style to java

M

Marc E

Greetings All,
I have an ArrayList of beans. Two of the fields in the bean are
"SystemID" and "ProcessTypeID". In this ArrayList, if there are only X
number of ProcessTypeIDs allowed for a given SystemID, I have to remove that
bean from the list. For example, the AL has 10 beans in it. 5 are for System
A. 3 of those beans are ProcessTypeID 1. But I'm only allowed to have 2
beans with SystemA/ProcessTypeID 1 combination. So when I hit the 3rd and
subsquent ones, I have to remove them from the list.

I come from a coldfusion background, and the easiest/fastest/most
maintainable solution would be along the lines of this:
loop over array
cfparam a structure that looks like this:
s_tmp[bean.systemid][bean.processtypeid]. default value = 0
if s_tmp[bean.systemid][bean.processtypeid] > 2
remove from array
else
s_tmp[bean.systemid][bean.processtypeid] =
s_tmp[bean.systemid][bean.processtypeid] + 1

end loop over array

or something along those lines. basically, just hold a 2-key structure whose
value is the number of instances in the list of that systemid/processtypeid
combination.

Now, in java, I can do this, and I have done it, by simply making a
single-key hashmap whose key is a string like this: SystemID & "," &
ProcessTypeID. The full method appears below.

However, this just seems hacky/tacky/bad practice. Problem is, I'm not sure
the best way to solve it in a "good" way. I don't want to create 10 new
objects every time this loop runs when I can get away with a single hashmap,
particularly since this is inside of a program that runs on a timer every 5
seconds. Although I don't know that the extra object creation is even
something to be mindful of. I also want to make sure that the next guy who
comes along, assuming he's a reasonably good java programmer, wont' look at
it and be thoroughly confused, but also won't be like "man, what a n00b".

So...any advice on making this more java-worthy?

thanks.

Marc.

Here's the current method. It works, it's fast..it just feels wrong.

public ArrayList<RequestBean> removeExtraConcurrents(ArrayList<RequestBean>
requests){
HashMap<String,Integer> concurrentRequests = new
HashMap<String,Integer>();
String keyString = "";
for(Iterator <RequestBean>iter = requests.iterator();
iter.hasNext();){
RequestBean request = iter.next();
//log.debug("Looking at systemid " + request.getSystemID() + ";
ptid " + request.getProcessTypeID() + "; maxConcurrent = " +
request.getMaxConcurrent());
if(request.getMaxConcurrent() > 0){
keyString = "" + request.getSystemID() + "," +
request.getProcessTypeID();
if(concurrentRequests.containsKey(keyString)) {
//check the current number of requests for this
system/processtypeid combo; if we've hit the max already, remove this
request from the list
int count = (Integer)concurrentRequests.get(keyString);
if(count == request.getMaxConcurrent()){
log.info("Removing request " + request.getRequestID()
+ " because maxConcurrent is reached");
iter.remove();
}else{
concurrentRequests.put(keyString,count+1);
}
}else{
concurrentRequests.put(keyString,1);

}
}
}
return requests;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top