@Override
public Collection<Agent> transform(Collection<Agent> list) {
if (list == null)
return null;
Agent agent = findAgentWithMostSites(list);
Set<String> bindings = new HashSet<String>();
for(Agent a : list){
if (a != null){
for (Site s : a.getSites()){
String x = s.getBindingMark();
if (x != null && Character.isDigit(x.charAt(0)))
bindings.add(x);
}
}
}
if (bindings.size() < 1)
return list;
HashMap<String,String> mapping = new HashMap<String,String>();
int i=1;
for(Site s : agent.getSites()){
String x = s.getBindingMark();
if (x != null && Character.isDigit(x.charAt(0))){
mapping.put(x, ""+i++);
bindings.remove(x);
}
}
for(String x : bindings){
mapping.put(x, ""+i++);
}
List<Agent> result = new ArrayList<Agent>();
for(Agent a : list){
List<Site> sites = new ArrayList<Site>();
for (Site s : a.getSites()){
String x = s.getBindingMark();
if (x != null && Character.isDigit(x.charAt(0))){
sites.add(s.clone().setBindingMark(mapping.get(x)));
} else {
sites.add(s);
}
}
result.add(new Agent(a.getName(),sites));
}
return result;
}