Bug3199:Bugfix with a generic boatClassSearch.

This commit is contained in:
Lukas Furmanek committed 2015-11-05 12:55:06 +01:00
1 parent 88e8466fca
commit af2cb8fdcc
3 files changed
+95 -16

No files matched your search

@@ -1,11 +1,11 @@
package com.sap.sailing.gwt.ui.adminconsole;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.i18n.client.LocaleInfo;
@@ -37,6 +37,7 @@ import com.sap.sailing.gwt.ui.shared.EventDTO;
import com.sap.sailing.gwt.ui.shared.RaceLogSetStartTimeAndProcedureDTO;
import com.sap.sailing.gwt.ui.shared.RegattaDTO;
import com.sap.sailing.gwt.ui.shared.StrippedLeaderboardDTO;
import com.sap.sse.common.Util;
import com.sap.sse.common.Util.Pair;
import com.sap.sse.common.Util.Triple;
import com.sap.sse.gwt.client.ErrorReporter;
@@ -595,8 +596,7 @@ public class SmartphoneTrackingEventManagementPanel extends AbstractLeaderboardC
new AsyncCallback<Iterable<CompetitorDTO>>() {
@Override
public void onFailure(Throwable caught) {
// FIXME logging
errorReporter.reportError("Error while searching BoatClass: " + caught.getMessage());
GWT.log("Error while searching BoatClass.");
showWithBoatClass.showWithBoatClass(null);
}
@@ -604,20 +604,12 @@ public class SmartphoneTrackingEventManagementPanel extends AbstractLeaderboardC
public void onSuccess(Iterable<CompetitorDTO> result) {
String boatClass = null;
if (result != null) {
HashMap<String, Integer> countBoatsPerType = new HashMap<String, Integer>();
int highestCount = 0;
ArrayList<String> boatClassen = new ArrayList<String>();
for (CompetitorDTO comp : result) {
String boatClassName = comp.getBoatClass().getName();
Integer boatClassCount = countBoatsPerType.get(boatClassName);
if (boatClassCount == null)
boatClassCount = 0;
boatClassCount++;
countBoatsPerType.put(boatClassName, boatClassCount);
if (boatClassCount > highestCount) {
highestCount = boatClassCount;
boatClass = boatClassName;
}
boatClassen.add(comp.getBoatClass().getName());
}
boatClass = Util.getDominantObjekt(boatClassen);
}
showWithBoatClass.showWithBoatClass(boatClass);
}
@@ -625,5 +617,5 @@ public class SmartphoneTrackingEventManagementPanel extends AbstractLeaderboardC
}
}
}
@@ -0,0 +1,53 @@
package com.sap.sse.common.test;
import java.util.ArrayList;
import org.junit.Assert;
import org.junit.Test;
import com.sap.sse.common.Util;
public class GetDominantObjektTest {
@Test
public void testNullCollection() {
Assert.assertArrayEquals(null, Util.getDominantObjekt(null));
}
@Test
public void testEmptyCollection() {
Iterable<String> objects = new ArrayList<String>();
Assert.assertEquals(null, Util.getDominantObjekt(objects));
}
@Test
public void testCollectionWithOneObject() {
ArrayList<String> objectCollection = new ArrayList<String>();
String dominant = "Test";
objectCollection.add(dominant);
Assert.assertEquals(dominant, Util.getDominantObjekt(objectCollection));
}
@Test
public void testCollectionWithTheSameCount() {
ArrayList<String> objectCollection = new ArrayList<String>();
String dominant = "Test";
String dominantTwo = "Test2";
objectCollection.add(dominant);
objectCollection.add(dominantTwo);
String result = Util.getDominantObjekt(objectCollection);
if(!(result == dominant || result == dominantTwo)) {
Assert.assertEquals(dominant, result);
}
}
@Test
public void testCollectionWithMoreThenOneObject() {
ArrayList<String> objectCollection = new ArrayList<String>();
String dominant = "Test";
objectCollection.add("not Dominant");
objectCollection.add(dominant);
objectCollection.add(dominant);
Assert.assertEquals(dominant, Util.getDominantObjekt(objectCollection));
}
}
@@ -3,6 +3,7 @@ package com.sap.sse.common;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
@@ -404,4 +405,37 @@ public class Util {
}
return result;
}
/**
* Searches the dominant object in a <code> Iterable&LTT&GT </code> collection.
*
* @param objects
* The <code> Iterable&LTT&GT </code> collection which should be analyzed.
* @return <code> T </code> Returns the dominant object. If the collection have two objects with the highest count,
* you will get one of them returned. If the collection is <code> null </code> or empty, the method will
* return <code> null </code>.
*/
public static <T> T getDominantObjekt(Iterable<T> objects) {
T result = null;
if (objects != null) {
if (objects.iterator().hasNext()) {
HashMap<T, Integer> countPerObject = new HashMap<T, Integer>();
int highestCount = 0;
for (T it : objects) {
Integer objectCount = countPerObject.get(it);
if (objectCount == null) {
objectCount = 0;
}
objectCount++;
countPerObject.put(it, objectCount);
if (objectCount > highestCount) {
highestCount = objectCount;
result = it;
}
}
}
}
return result;
}
}