pulled up all but the Named-related methods from com.sap.sailing.common.Util to com.sap.sse.common.Util; further prepared for Regatta search on RacingEventService

This commit is contained in:
Axel Uhl committed 2014-06-02 23:51:49 +02:00
1 parent 0c47e86304
commit 5b038779db
11 files changed
+298 -196

No files matched your search

@@ -1,187 +1,8 @@
package com.sap.sailing.domain.common.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.sap.sailing.domain.common.Named;
public class Util extends com.sap.sse.common.Util {
/**
* Adds all elements from <code>what</code> to <code>addTo</code> and returns <code>addTo</code> for chained use.
* If <code>what</code> is <code>null</code>, this operation does nothing, not even fail with an exception, but
* return the unmodified <code>addTo</code>.
*/
public static <T> Collection<T> addAll(Iterable<? extends T> what, Collection<T> addTo) {
if (what != null) {
for (T t : what) {
addTo.add(t);
}
}
return addTo;
}
/**
* Removes all elements in <code>what</code> from <code>removeFrom</code> and returns <code>removeFrom</code> for chained use.
* If <code>what</code> is <code>null</code>, this operation does nothing, not even fail with an exception, but
* return the unmodified <code>removeFrom</code>.
*/
public static <T> Collection<T> removeAll(Iterable<T> what, Collection<T> removeFrom) {
if (what != null) {
for (T t : what) {
removeFrom.remove(t);
}
}
return removeFrom;
}
public static <T> T[] toArray(Iterable<? extends T> what, T[] arr) {
List<T> list = new ArrayList<T>();
addAll(what, list);
return list.toArray(arr);
}
public static <T> int size(Iterable<T> i) {
if (i instanceof Collection<?>) {
return ((Collection<?>) i).size();
} else {
int result = 0;
Iterator<T> iter = i.iterator();
while (iter.hasNext()) {
result++;
iter.next();
}
return result;
}
}
public static <T> int indexOf(Iterable<? extends T> i, T t) {
int result;
if (i instanceof List<?>) {
List<?> list = (List<?>) i;
result = list.indexOf(t);
} else {
boolean found = false;
int counter = 0;
for (T it : i) {
if (it == null && t == null
|| it != null && it.equals(t)) {
result = counter;
found = true;
break;
}
counter++;
}
if (found) {
result = counter;
} else {
result = -1;
}
}
return result;
}
public static <T> boolean equals(Iterable<? extends T> a, Iterable<? extends T> b) {
Iterator<? extends T> aIter = a.iterator();
Iterator<? extends T> bIter = b.iterator();
while (aIter.hasNext() && bIter.hasNext()) {
T ao = aIter.next();
T bo = bIter.next();
if (!equalsWithNull(ao, bo)) {
return false;
}
}
return !aIter.hasNext() && !bIter.hasNext();
}
public static <T> T get(Iterable<T> iterable, int i) {
if (iterable instanceof List<?>) {
List<T> l = (List<T>) iterable;
return l.get(i);
} else {
Iterator<T> iter = iterable.iterator();
T result = iter.next();
for (int j=0; j<i; j++) {
result = iter.next();
}
return result;
}
}
public static <T> boolean contains(Iterable<T> ts, T t) {
if (ts instanceof Collection<?>) {
return ((Collection<?>) ts).contains(t);
} else {
for (T t2 : ts) {
if (equalsWithNull(t2, t)) {
return true;
}
}
return false;
}
}
public static <T> boolean isEmpty(Iterable<T> ts) {
if (ts instanceof Collection<?>) {
return ((Collection<?>) ts).isEmpty();
} else {
return !ts.iterator().hasNext();
}
}
public static boolean equalsWithNull(Object o1, Object o2) {
final boolean result;
if (o1 == null) {
result = (o2 == null);
} else {
if (o2 == null) {
result = false;
} else {
result = o1.equals(o2);
}
}
return result;
}
public static <T> int compareToWithNull(Comparable<T> o1, T o2) {
final int result;
if (o1 == null) {
if (o2 == null) {
result = 0;
} else {
result = -1;
}
} else {
if (o2 == null) {
result = 1;
} else {
result = o1.compareTo(o2);
}
}
return result;
}
/**
* Return the default value instead of null, if the map does not contain the key.
*/
public static <K, V> V get(Map<K, V> map, K key, V defaultVal) {
if (map.containsKey(key)) {
return map.get(key);
}
return defaultVal;
}
public static <K, V> void addToValueSet(Map<K, Set<V>> map, K key, V value) {
if (! map.containsKey(key)) {
map.put(key, new HashSet<V>());
}
map.get(key).add(value);
}
public static String join(String separator, Iterable<? extends Named> nameds) {
return join(separator, toArray(nameds, new Named[size(nameds)]));
}
@@ -194,17 +15,4 @@ public class Util extends com.sap.sse.common.Util {
return join(separator, strings);
}
public static String join(String separator, String...strings) {
StringBuilder result = new StringBuilder();
boolean first = true;
for (String string : strings) {
if (first) {
first = false;
} else {
result.append(separator);
}
result.append(string);
}
return result.toString();
}
}
@@ -0,0 +1,7 @@
package com.sap.sailing.server;
import com.sap.sse.common.search.Hit;
public interface RegattaSearchResult extends Hit {
}
@@ -0,0 +1,28 @@
package com.sap.sailing.server.impl;
import com.sap.sailing.domain.base.Regatta;
import com.sap.sailing.server.RacingEventService;
import com.sap.sailing.server.RegattaSearchResult;
import com.sap.sse.common.search.KeywordQuery;
import com.sap.sse.common.search.Result;
import com.sap.sse.common.search.ResultImpl;
/**
* Searches a {@link RacingEventService} instance for regattas that somehow match with a
* {@link KeywordQuery}. Several attributes on the way are considered, in particular the event
* name (if a regatta is somehow linked to an event), the event's venue name, the leaderboard group
* name that contains a regatta leaderboard for the subject regatta, the regatta name itself,
* the regatta's boat class name, and the names of all competitors entered into the regatta.
*
* @author Axel Uhl (D043530)
*
*/
public class RegattaByKeywordSearchService {
Result<RegattaSearchResult> search(RacingEventService service, KeywordQuery query) {
ResultImpl<RegattaSearchResult> result = new ResultImpl<>(query, new RegattaSearchResultRanker());
for (Regatta regatta : service.getAllRegattas()) {
// TODO continue here...
}
return result;
}
}
@@ -0,0 +1,13 @@
package com.sap.sailing.server.impl;
import java.util.Comparator;
import com.sap.sailing.server.RegattaSearchResult;
public class RegattaSearchResultRanker implements Comparator<RegattaSearchResult> {
@Override
public int compare(RegattaSearchResult o1, RegattaSearchResult o2) {
// TODO Auto-generated method stub
return 0;
}
}
+2 -1
View File
@@ -6,4 +6,5 @@ Bundle-Version: 1.0.0.qualifier
Bundle-Vendor: SAP
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Export-Package: com.sap.sse.common,
com.sap.sse.common.filter
com.sap.sse.common.filter,
com.sap.sse.common.search
@@ -1,6 +1,13 @@
package com.sap.sse.common;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Util {
@@ -124,4 +131,189 @@ public class Util {
return "[" + a + ", " + b + ", " + c + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
}
}
/**
* Adds all elements from <code>what</code> to <code>addTo</code> and returns <code>addTo</code> for chained use.
* If <code>what</code> is <code>null</code>, this operation does nothing, not even fail with an exception, but
* return the unmodified <code>addTo</code>.
*/
public static <T> Collection<T> addAll(Iterable<? extends T> what, Collection<T> addTo) {
if (what != null) {
for (T t : what) {
addTo.add(t);
}
}
return addTo;
}
/**
* Removes all elements in <code>what</code> from <code>removeFrom</code> and returns <code>removeFrom</code> for chained use.
* If <code>what</code> is <code>null</code>, this operation does nothing, not even fail with an exception, but
* return the unmodified <code>removeFrom</code>.
*/
public static <T> Collection<T> removeAll(Iterable<T> what, Collection<T> removeFrom) {
if (what != null) {
for (T t : what) {
removeFrom.remove(t);
}
}
return removeFrom;
}
public static <T> T[] toArray(Iterable<? extends T> what, T[] arr) {
List<T> list = new ArrayList<T>();
addAll(what, list);
return list.toArray(arr);
}
public static <T> int size(Iterable<T> i) {
if (i instanceof Collection<?>) {
return ((Collection<?>) i).size();
} else {
int result = 0;
Iterator<T> iter = i.iterator();
while (iter.hasNext()) {
result++;
iter.next();
}
return result;
}
}
public static <T> int indexOf(Iterable<? extends T> i, T t) {
int result;
if (i instanceof List<?>) {
List<?> list = (List<?>) i;
result = list.indexOf(t);
} else {
boolean found = false;
int counter = 0;
for (T it : i) {
if (it == null && t == null
|| it != null && it.equals(t)) {
result = counter;
found = true;
break;
}
counter++;
}
if (found) {
result = counter;
} else {
result = -1;
}
}
return result;
}
public static <T> boolean equals(Iterable<? extends T> a, Iterable<? extends T> b) {
Iterator<? extends T> aIter = a.iterator();
Iterator<? extends T> bIter = b.iterator();
while (aIter.hasNext() && bIter.hasNext()) {
T ao = aIter.next();
T bo = bIter.next();
if (!equalsWithNull(ao, bo)) {
return false;
}
}
return !aIter.hasNext() && !bIter.hasNext();
}
public static <T> T get(Iterable<T> iterable, int i) {
if (iterable instanceof List<?>) {
List<T> l = (List<T>) iterable;
return l.get(i);
} else {
Iterator<T> iter = iterable.iterator();
T result = iter.next();
for (int j=0; j<i; j++) {
result = iter.next();
}
return result;
}
}
public static <T> boolean contains(Iterable<T> ts, T t) {
if (ts instanceof Collection<?>) {
return ((Collection<?>) ts).contains(t);
} else {
for (T t2 : ts) {
if (equalsWithNull(t2, t)) {
return true;
}
}
return false;
}
}
public static <T> boolean isEmpty(Iterable<T> ts) {
if (ts instanceof Collection<?>) {
return ((Collection<?>) ts).isEmpty();
} else {
return !ts.iterator().hasNext();
}
}
public static boolean equalsWithNull(Object o1, Object o2) {
final boolean result;
if (o1 == null) {
result = (o2 == null);
} else {
if (o2 == null) {
result = false;
} else {
result = o1.equals(o2);
}
}
return result;
}
public static <T> int compareToWithNull(Comparable<T> o1, T o2) {
final int result;
if (o1 == null) {
if (o2 == null) {
result = 0;
} else {
result = -1;
}
} else {
if (o2 == null) {
result = 1;
} else {
result = o1.compareTo(o2);
}
}
return result;
}
/**
* Return the default value instead of null, if the map does not contain the key.
*/
public static <K, V> V get(Map<K, V> map, K key, V defaultVal) {
if (map.containsKey(key)) {
return map.get(key);
}
return defaultVal;
}
public static <K, V> void addToValueSet(Map<K, Set<V>> map, K key, V value) {
if (! map.containsKey(key)) {
map.put(key, new HashSet<V>());
}
map.get(key).add(value);
}
public static String join(String separator, String...strings) {
StringBuilder result = new StringBuilder();
boolean first = true;
for (String string : strings) {
if (first) {
first = false;
} else {
result.append(separator);
}
result.append(string);
}
return result.toString();
}
}
@@ -0,0 +1,11 @@
package com.sap.sse.common.search;
/**
* A single search hit. Many hits constitute the search {@link Result}.
*
* @author Axel Uhl (D043530)
*
*/
public interface Hit {
}
@@ -0,0 +1,14 @@
package com.sap.sse.common.search;
public class KeywordQuery implements Query {
private final Iterable<String> keywords;
protected KeywordQuery(Iterable<String> keywords) {
super();
this.keywords = keywords;
}
public Iterable<String> getKeywords() {
return keywords;
}
}
@@ -6,7 +6,7 @@ package com.sap.sse.common.search;
* @author Axel Uhl (D043530)
*
*/
public interface Result {
public interface Result<H extends Hit> {
/**
* The query for which this is the result. Useful when multiple queries have been fired concurrently.
*/
@@ -0,0 +1,28 @@
package com.sap.sse.common.search;
import java.util.Collections;
import java.util.Comparator;
import java.util.TreeSet;
public class ResultImpl<H extends Hit> implements Result<H> {
private final TreeSet<H> hits;
private final Query query;
public ResultImpl(Query query, Comparator<H> ranker) {
hits = new TreeSet<H>(ranker);
this.query = query;
}
public Iterable<H> getHits() {
return Collections.unmodifiableCollection(hits);
}
public void addHit(H hit) {
hits.add(hit);
}
@Override
public Query getQuery() {
return query;
}
}
@@ -6,6 +6,6 @@ package com.sap.sse.common.search;
* @author Axel Uhl (D043530)
*
*/
public interface Searchable {
Result query(Query query);
public interface Searchable<H extends Hit> {
Result<H> query(Query query);
}