From 5b038779db2436314b2f41a2a31cd342f0abc841 Mon Sep 17 00:00:00 2001 From: Axel Uhl Date: Mon, 2 Jun 2014 23:51:49 +0200 Subject: [PATCH] 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 --- .../sap/sailing/domain/common/impl/Util.java | 192 ------------------ .../sailing/server/RegattaSearchResult.java | 7 + .../impl/RegattaByKeywordSearchService.java | 28 +++ .../impl/RegattaSearchResultRanker.java | 13 ++ java/com.sap.sse.common/META-INF/MANIFEST.MF | 3 +- .../src/com/sap/sse/common/Util.java | 192 ++++++++++++++++++ .../src/com/sap/sse/common/search/Hit.java | 11 + .../sap/sse/common/search/KeywordQuery.java | 14 ++ .../src/com/sap/sse/common/search/Result.java | 2 +- .../com/sap/sse/common/search/ResultImpl.java | 28 +++ .../com/sap/sse/common/search/Searchable.java | 4 +- 11 files changed, 298 insertions(+), 196 deletions(-) create mode 100755 java/com.sap.sailing.server/src/com/sap/sailing/server/RegattaSearchResult.java create mode 100755 java/com.sap.sailing.server/src/com/sap/sailing/server/impl/RegattaByKeywordSearchService.java create mode 100755 java/com.sap.sailing.server/src/com/sap/sailing/server/impl/RegattaSearchResultRanker.java create mode 100755 java/com.sap.sse.common/src/com/sap/sse/common/search/Hit.java create mode 100755 java/com.sap.sse.common/src/com/sap/sse/common/search/KeywordQuery.java create mode 100755 java/com.sap.sse.common/src/com/sap/sse/common/search/ResultImpl.java diff --git a/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/impl/Util.java b/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/impl/Util.java index 98a7dde1874..77e172e5668 100755 --- a/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/impl/Util.java +++ b/java/com.sap.sailing.domain.common/src/com/sap/sailing/domain/common/impl/Util.java @@ -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 what to addTo and returns addTo for chained use. - * If what is null, this operation does nothing, not even fail with an exception, but - * return the unmodified addTo. - */ - public static Collection addAll(Iterable what, Collection addTo) { - if (what != null) { - for (T t : what) { - addTo.add(t); - } - } - return addTo; - } - - /** - * Removes all elements in what from removeFrom and returns removeFrom for chained use. - * If what is null, this operation does nothing, not even fail with an exception, but - * return the unmodified removeFrom. - */ - public static Collection removeAll(Iterable what, Collection removeFrom) { - if (what != null) { - for (T t : what) { - removeFrom.remove(t); - } - } - return removeFrom; - } - - public static T[] toArray(Iterable what, T[] arr) { - List list = new ArrayList(); - addAll(what, list); - return list.toArray(arr); - } - - public static int size(Iterable i) { - if (i instanceof Collection) { - return ((Collection) i).size(); - } else { - int result = 0; - Iterator iter = i.iterator(); - while (iter.hasNext()) { - result++; - iter.next(); - } - return result; - } - } - - public static int indexOf(Iterable 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 boolean equals(Iterable a, Iterable b) { - Iterator aIter = a.iterator(); - Iterator 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 get(Iterable iterable, int i) { - if (iterable instanceof List) { - List l = (List) iterable; - return l.get(i); - } else { - Iterator iter = iterable.iterator(); - T result = iter.next(); - for (int j=0; j boolean contains(Iterable 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 boolean isEmpty(Iterable 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 int compareToWithNull(Comparable 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 V get(Map map, K key, V defaultVal) { - if (map.containsKey(key)) { - return map.get(key); - } - return defaultVal; - } - - public static void addToValueSet(Map> map, K key, V value) { - if (! map.containsKey(key)) { - map.put(key, new HashSet()); - } - map.get(key).add(value); - } - public static String join(String separator, Iterable 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(); - } } diff --git a/java/com.sap.sailing.server/src/com/sap/sailing/server/RegattaSearchResult.java b/java/com.sap.sailing.server/src/com/sap/sailing/server/RegattaSearchResult.java new file mode 100755 index 00000000000..13196ee8abe --- /dev/null +++ b/java/com.sap.sailing.server/src/com/sap/sailing/server/RegattaSearchResult.java @@ -0,0 +1,7 @@ +package com.sap.sailing.server; + +import com.sap.sse.common.search.Hit; + +public interface RegattaSearchResult extends Hit { + +} diff --git a/java/com.sap.sailing.server/src/com/sap/sailing/server/impl/RegattaByKeywordSearchService.java b/java/com.sap.sailing.server/src/com/sap/sailing/server/impl/RegattaByKeywordSearchService.java new file mode 100755 index 00000000000..e5b92ddf8ab --- /dev/null +++ b/java/com.sap.sailing.server/src/com/sap/sailing/server/impl/RegattaByKeywordSearchService.java @@ -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 search(RacingEventService service, KeywordQuery query) { + ResultImpl result = new ResultImpl<>(query, new RegattaSearchResultRanker()); + for (Regatta regatta : service.getAllRegattas()) { + // TODO continue here... + } + return result; + } +} diff --git a/java/com.sap.sailing.server/src/com/sap/sailing/server/impl/RegattaSearchResultRanker.java b/java/com.sap.sailing.server/src/com/sap/sailing/server/impl/RegattaSearchResultRanker.java new file mode 100755 index 00000000000..84756dc1358 --- /dev/null +++ b/java/com.sap.sailing.server/src/com/sap/sailing/server/impl/RegattaSearchResultRanker.java @@ -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 { + @Override + public int compare(RegattaSearchResult o1, RegattaSearchResult o2) { + // TODO Auto-generated method stub + return 0; + } +} diff --git a/java/com.sap.sse.common/META-INF/MANIFEST.MF b/java/com.sap.sse.common/META-INF/MANIFEST.MF index 0e45d756c65..1f520d6ae51 100644 --- a/java/com.sap.sse.common/META-INF/MANIFEST.MF +++ b/java/com.sap.sse.common/META-INF/MANIFEST.MF @@ -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 diff --git a/java/com.sap.sse.common/src/com/sap/sse/common/Util.java b/java/com.sap.sse.common/src/com/sap/sse/common/Util.java index 40b95804fc9..a703b300e64 100755 --- a/java/com.sap.sse.common/src/com/sap/sse/common/Util.java +++ b/java/com.sap.sse.common/src/com/sap/sse/common/Util.java @@ -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 what to addTo and returns addTo for chained use. + * If what is null, this operation does nothing, not even fail with an exception, but + * return the unmodified addTo. + */ + public static Collection addAll(Iterable what, Collection addTo) { + if (what != null) { + for (T t : what) { + addTo.add(t); + } + } + return addTo; + } + + /** + * Removes all elements in what from removeFrom and returns removeFrom for chained use. + * If what is null, this operation does nothing, not even fail with an exception, but + * return the unmodified removeFrom. + */ + public static Collection removeAll(Iterable what, Collection removeFrom) { + if (what != null) { + for (T t : what) { + removeFrom.remove(t); + } + } + return removeFrom; + } + + public static T[] toArray(Iterable what, T[] arr) { + List list = new ArrayList(); + addAll(what, list); + return list.toArray(arr); + } + + public static int size(Iterable i) { + if (i instanceof Collection) { + return ((Collection) i).size(); + } else { + int result = 0; + Iterator iter = i.iterator(); + while (iter.hasNext()) { + result++; + iter.next(); + } + return result; + } + } + + public static int indexOf(Iterable 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 boolean equals(Iterable a, Iterable b) { + Iterator aIter = a.iterator(); + Iterator 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 get(Iterable iterable, int i) { + if (iterable instanceof List) { + List l = (List) iterable; + return l.get(i); + } else { + Iterator iter = iterable.iterator(); + T result = iter.next(); + for (int j=0; j boolean contains(Iterable 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 boolean isEmpty(Iterable 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 int compareToWithNull(Comparable 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 V get(Map map, K key, V defaultVal) { + if (map.containsKey(key)) { + return map.get(key); + } + return defaultVal; + } + + public static void addToValueSet(Map> map, K key, V value) { + if (! map.containsKey(key)) { + map.put(key, new HashSet()); + } + 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(); + } } diff --git a/java/com.sap.sse.common/src/com/sap/sse/common/search/Hit.java b/java/com.sap.sse.common/src/com/sap/sse/common/search/Hit.java new file mode 100755 index 00000000000..f8524ee1fb5 --- /dev/null +++ b/java/com.sap.sse.common/src/com/sap/sse/common/search/Hit.java @@ -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 { + +} diff --git a/java/com.sap.sse.common/src/com/sap/sse/common/search/KeywordQuery.java b/java/com.sap.sse.common/src/com/sap/sse/common/search/KeywordQuery.java new file mode 100755 index 00000000000..b83b9c4512c --- /dev/null +++ b/java/com.sap.sse.common/src/com/sap/sse/common/search/KeywordQuery.java @@ -0,0 +1,14 @@ +package com.sap.sse.common.search; + +public class KeywordQuery implements Query { + private final Iterable keywords; + + protected KeywordQuery(Iterable keywords) { + super(); + this.keywords = keywords; + } + + public Iterable getKeywords() { + return keywords; + } +} diff --git a/java/com.sap.sse.common/src/com/sap/sse/common/search/Result.java b/java/com.sap.sse.common/src/com/sap/sse/common/search/Result.java index 0378b74c998..0891c17ec04 100644 --- a/java/com.sap.sse.common/src/com/sap/sse/common/search/Result.java +++ b/java/com.sap.sse.common/src/com/sap/sse/common/search/Result.java @@ -6,7 +6,7 @@ package com.sap.sse.common.search; * @author Axel Uhl (D043530) * */ -public interface Result { +public interface Result { /** * The query for which this is the result. Useful when multiple queries have been fired concurrently. */ diff --git a/java/com.sap.sse.common/src/com/sap/sse/common/search/ResultImpl.java b/java/com.sap.sse.common/src/com/sap/sse/common/search/ResultImpl.java new file mode 100755 index 00000000000..fb31f58b004 --- /dev/null +++ b/java/com.sap.sse.common/src/com/sap/sse/common/search/ResultImpl.java @@ -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 implements Result { + private final TreeSet hits; + private final Query query; + + public ResultImpl(Query query, Comparator ranker) { + hits = new TreeSet(ranker); + this.query = query; + } + + public Iterable getHits() { + return Collections.unmodifiableCollection(hits); + } + + public void addHit(H hit) { + hits.add(hit); + } + + @Override + public Query getQuery() { + return query; + } +} diff --git a/java/com.sap.sse.common/src/com/sap/sse/common/search/Searchable.java b/java/com.sap.sse.common/src/com/sap/sse/common/search/Searchable.java index daa8374bdc0..1029f213342 100644 --- a/java/com.sap.sse.common/src/com/sap/sse/common/search/Searchable.java +++ b/java/com.sap.sse.common/src/com/sap/sse/common/search/Searchable.java @@ -6,6 +6,6 @@ package com.sap.sse.common.search; * @author Axel Uhl (D043530) * */ -public interface Searchable { - Result query(Query query); +public interface Searchable { + Result query(Query query); }