Skip to content
Snippets Groups Projects
Commit 2c18b1b3 authored by gmantele's avatar gmantele
Browse files

ADQL: 2 BIG BUGS fixed in TextualSearchList:

(1) Missing contains(Object) function. Without this function, any ArrayList function like retainAll(...) did not work properly: the research in the collection was made with a bad key. KeyExtractor was not used.
(2) Result of a research (function get(...)) must return a copy of the result, since the result is an ArrayList, value of the HashMap used for research. If a copy is not returned, any modification (and particularly remove(...)) can be made on the value of the index (HashMap) used for the research.
parent 628848f1
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,8 @@ package cds.utils; ...@@ -16,7 +16,8 @@ package cds.utils;
* You should have received a copy of the GNU Lesser General Public License * You should have received a copy of the GNU Lesser General Public License
* along with ADQLLibrary. If not, see <http://www.gnu.org/licenses/>. * along with ADQLLibrary. If not, see <http://www.gnu.org/licenses/>.
* *
* Copyright 2012 - UDS/Centre de Données astronomiques de Strasbourg (CDS) * Copyright 2012-2014 - UDS/Centre de Données astronomiques de Strasbourg (CDS),
* Astronomisches Rechen Institute (ARI)
*/ */
import java.util.ArrayList; import java.util.ArrayList;
...@@ -37,8 +38,8 @@ import java.util.HashMap; ...@@ -37,8 +38,8 @@ import java.util.HashMap;
* <p><b><u>WARNING:</u> The extracted key MUST be CASE-SENSITIVE and UNIQUE !</b></p> * <p><b><u>WARNING:</u> The extracted key MUST be CASE-SENSITIVE and UNIQUE !</b></p>
* *
* @param <E> Type of object to manage in this list. * @param <E> Type of object to manage in this list.
* @author Gr&eacute;gory Mantelet (CDS) * @author Gr&eacute;gory Mantelet (CDS;ARI)
* @version 09/2011 * @version 1.1 (11/2013)
*/ */
public class TextualSearchList< E > extends ArrayList<E> { public class TextualSearchList< E > extends ArrayList<E> {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
...@@ -140,6 +141,31 @@ public class TextualSearchList< E > extends ArrayList<E> { ...@@ -140,6 +141,31 @@ public class TextualSearchList< E > extends ArrayList<E> {
addAll(c); addAll(c);
} }
/**
* Returns true if this list contains the specified element.
* More formally, returns true if and only if this list contains at least one element
* e such that (keyExtractor.getKey(o).equals(keyExtractor.getKey(e))).
*
* @see java.util.ArrayList#contains(java.lang.Object)
* @see #getKey(Object)
*
* @since 1.1
*/
@SuppressWarnings("unchecked")
@Override
public boolean contains(Object o){
try{
if (o == null)
return false;
else{
E object = (E)o;
return !get(getKey(object)).isEmpty();
}
}catch(Exception e){
return false;
}
}
/** /**
* Searches (CASE-INSENSITIVE) the object which has the given key. * Searches (CASE-INSENSITIVE) the object which has the given key.
* *
...@@ -159,6 +185,7 @@ public class TextualSearchList< E > extends ArrayList<E> { ...@@ -159,6 +185,7 @@ public class TextualSearchList< E > extends ArrayList<E> {
* *
* @return All the objects whose the key is the same as the given one. * @return All the objects whose the key is the same as the given one.
*/ */
@SuppressWarnings("unchecked")
public ArrayList<E> get(final String key, final boolean caseSensitive){ public ArrayList<E> get(final String key, final boolean caseSensitive){
if (key == null) if (key == null)
return new ArrayList<E>(0); return new ArrayList<E>(0);
...@@ -167,7 +194,7 @@ public class TextualSearchList< E > extends ArrayList<E> { ...@@ -167,7 +194,7 @@ public class TextualSearchList< E > extends ArrayList<E> {
if (founds == null) if (founds == null)
return new ArrayList<E>(0); return new ArrayList<E>(0);
else else
return founds; return (ArrayList<E>)founds.clone();
} }
/** /**
...@@ -457,6 +484,7 @@ public class TextualSearchList< E > extends ArrayList<E> { ...@@ -457,6 +484,7 @@ public class TextualSearchList< E > extends ArrayList<E> {
* @param <E> Type of object from which a textual key must be extracted. * @param <E> Type of object from which a textual key must be extracted.
*/ */
protected static class DefaultKeyExtractor< E > implements KeyExtractor<E> { protected static class DefaultKeyExtractor< E > implements KeyExtractor<E> {
@Override
public String getKey(final E obj){ public String getKey(final E obj){
return obj.toString(); return obj.toString();
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment