Author: desruisseaux
Date: Sat Aug  5 16:09:04 2017
New Revision: 1804194

URL: http://svn.apache.org/viewvc?rev=1804194&view=rev
Log:
Replace the findResource(String) implementation based on stream by a 
implementation based on classical loops.
The intend is to make the merge with JDK7 branch easier, and to avoid the hack 
needed for checked exceptions.

Modified:
    
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources.java
    
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources.properties
    
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources_fr.properties
    
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/Aggregate.java
    
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java

Modified: 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources.java?rev=1804194&r1=1804193&r2=1804194&view=diff
==============================================================================
--- 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources.java
 [UTF-8] Sat Aug  5 16:09:04 2017
@@ -149,6 +149,16 @@ public final class Resources extends Ind
         public static final short ProcessingExecutedOn_1 = 12;
 
         /**
+         * More than one resource have the “{1}” identifier in the “{0}” data 
store.
+         */
+        public static final short ResourceIdentifierCollision_2 = 23;
+
+        /**
+         * No resource found for the “{1}” identifier in the “{0}” data store.
+         */
+        public static final short ResourceNotFound_2 = 24;
+
+        /**
          * The “{1}” element must be declared before “{0}”.
          */
         public static final short ShallBeDeclaredBefore_2 = 22;

Modified: 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources.properties
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources.properties?rev=1804194&r1=1804193&r2=1804194&view=diff
==============================================================================
--- 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources.properties
 [ISO-8859-1] (original)
+++ 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources.properties
 [ISO-8859-1] Sat Aug  5 16:09:04 2017
@@ -36,6 +36,8 @@ IllegalOutputTypeForWriter_2      = The
 InconsistentNameComponents_2      = Components of the \u201c{1}\u201d name are 
inconsistent with those of the name previously binded in \u201c{0}\u201d data 
store.
 MissingSchemeInURI_1              = Missing scheme in \u201c{0}\u201d URI.
 ProcessingExecutedOn_1            = Processing executed on {0}.
+ResourceIdentifierCollision_2     = More than one resource have the 
\u201c{1}\u201d identifier in the \u201c{0}\u201d data store.
+ResourceNotFound_2                = No resource found for the \u201c{1}\u201d 
identifier in the \u201c{0}\u201d data store.
 ShallBeDeclaredBefore_2           = The \u201c{1}\u201d element must be 
declared before \u201c{0}\u201d.
 StreamIsForwardOnly_1             = Can not move backward in the 
\u201c{0}\u201d stream.
 StreamIsReadOnce_1                = The \u201c{0}\u201d data store can be read 
only once.

Modified: 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources_fr.properties
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources_fr.properties?rev=1804194&r1=1804193&r2=1804194&view=diff
==============================================================================
--- 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources_fr.properties
 [ISO-8859-1] (original)
+++ 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/Resources_fr.properties
 [ISO-8859-1] Sat Aug  5 16:09:04 2017
@@ -41,6 +41,8 @@ IllegalOutputTypeForWriter_2      = Le l
 InconsistentNameComponents_2      = Les \u00e9l\u00e9ments qui composent le 
nom \u00ab\u202f{1}\u202f\u00bb ne sont pas coh\u00e9rents avec ceux du nom qui 
avait \u00e9t\u00e9 pr\u00e9c\u00e9demment li\u00e9 dans les donn\u00e9es de 
\u00ab\u202f{0}\u202f\u00bb.
 MissingSchemeInURI_1              = Il manque le sch\u00e9ma dans l\u2019URI 
\u00ab\u202f{0}\u202f\u00bb.
 ProcessingExecutedOn_1            = Traitement ex\u00e9cut\u00e9 sur {0}.
+ResourceIdentifierCollision_2     = Plusieurs ressources utilisent 
l\u2019identifiant \u00ab\u202f{1}\u202f\u00bb dans les donn\u00e9es de 
\u00ab\u202f{0}\u202f\u00bb.
+ResourceNotFound_2                = Aucune ressource n\u2019a \u00e9t\u00e9 
trouv\u00e9e pour l\u2019identifiant \u00ab\u202f{1}\u202f\u00bb dans les 
donn\u00e9es de \u00ab\u202f{0}\u202f\u00bb.
 ShallBeDeclaredBefore_2           = L\u2019\u00e9l\u00e9ment 
\u00ab\u202f{1}\u202f\u00bb doit \u00eatre d\u00e9clar\u00e9 avant 
\u00ab\u202f{0}\u202f\u00bb.
 StreamIsForwardOnly_1             = Ne peut pas reculer dans le flux de 
donn\u00e9es \u00ab\u202f{0}\u202f\u00bb.
 StreamIsReadOnce_1                = Les donn\u00e9es de 
\u00ab\u202f{0}\u202f\u00bb ne peuvent \u00eatre lues qu\u2019une seule fois.

Modified: 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/Aggregate.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/Aggregate.java?rev=1804194&r1=1804193&r2=1804194&view=diff
==============================================================================
--- 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/Aggregate.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/Aggregate.java
 [UTF-8] Sat Aug  5 16:09:04 2017
@@ -81,6 +81,7 @@ public interface Aggregate extends Resou
      * {@link org.apache.sis.metadata.iso.citation.DefaultCitation#getTitle() 
title}</blockquote>
      *
      * @return all children resources that are components of this aggregate. 
Never {@code null}.
+     * @throws DataStoreException if an error occurred while fetching the 
components.
      */
-    Collection<Resource> components();
+    Collection<Resource> components() throws DataStoreException;
 }

Modified: 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java?rev=1804194&r1=1804193&r2=1804194&view=diff
==============================================================================
--- 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java
 [UTF-8] (original)
+++ 
sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStore.java
 [UTF-8] Sat Aug  5 16:09:04 2017
@@ -16,20 +16,19 @@
  */
 package org.apache.sis.storage;
 
-import java.util.Collection;
 import java.util.Locale;
+import java.util.Map;
+import java.util.IdentityHashMap;
 import java.util.NoSuchElementException;
-import java.util.Objects;
-import java.util.function.Function;
-import org.apache.sis.internal.metadata.NameToIdentifier;
 import org.opengis.metadata.Metadata;
+import org.opengis.metadata.citation.Citation;
+import org.opengis.metadata.identification.Identification;
 import org.apache.sis.util.Localized;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.logging.WarningListener;
 import org.apache.sis.util.logging.WarningListeners;
-import org.opengis.metadata.Identifier;
-import org.opengis.metadata.citation.Citation;
-import org.opengis.metadata.identification.Identification;
+import org.apache.sis.internal.metadata.NameToIdentifier;
+import org.apache.sis.internal.storage.Resources;
 
 
 /**
@@ -191,7 +190,7 @@ public abstract class DataStore implemen
      * an {@link Aggregate} from which other resources can be accessed.
      *
      * @return the starting point of all resources in this data store,
-     *         or {@code null} if this data store does not contains any 
resources.
+     *         or {@code null} if this data store does not contain any 
resources.
      * @throws DataStoreException if an error occurred while reading the data.
      */
     public abstract Resource getRootResource() throws DataStoreException;
@@ -205,63 +204,67 @@ public abstract class DataStore implemen
      * {@link 
org.apache.sis.metadata.iso.identification.AbstractIdentification#getCitation() 
citation} /
      * {@link 
org.apache.sis.metadata.iso.citation.DefaultCitation#getIdentifiers() 
identifier}</blockquote>
      *
-     * The default implementation verifies the {@linkplain #getRootResource() 
root resource}, then iterates over all
-     * components of all {@link Aggregate}s. If exactly one match is found, 
the associated resource is returned.
+     * The default implementation verifies the {@linkplain #getRootResource() 
root resource}, then iterates over
+     * components of {@link Aggregate}s. If a match is found without 
ambiguity, the associated resource is returned.
      * Otherwise an exception is thrown. Subclasses are encouraged to override 
this method with a more efficient
      * implementation.
      *
-     * @param  name  identifier of the data to acquire. Must be non-null.
+     * @param  identifier  identifier of the resource to fetch. Must be 
non-null.
      * @return resource associated to the given identifier (never {@code 
null}).
      * @throws IllegalNameException if no resource is found for the given 
identifier, or if more than one resource is found.
      * @throws DataStoreException if another kind of error occurred while 
searching resources.
      */
-    public Resource findResource(final String name) throws DataStoreException {
-        ArgumentChecks.ensureNonEmpty("Name of the searched resource", name);
-
-        final Resource root = getRootResource();
-        if (root==null) throw new IllegalNameException("No resource found for 
name : "+name);
+    public Resource findResource(final String identifier) throws 
DataStoreException {
+        ArgumentChecks.ensureNonEmpty("identifier", identifier);
+        final Resource resource = findResource(identifier, getRootResource(), 
new IdentityHashMap<>());
+        if (resource != null) {
+            return resource;
+        }
+        throw new IllegalNameException(Resources.forLocale(getLocale())
+                .getString(Resources.Keys.ResourceNotFound_2, 
getDisplayName(), identifier));
+    }
 
-        //recursive search
-        Object res = new Function<Resource,Object>() {
-            @Override
-            public Object apply(final Resource candidate) {
-
-                final Metadata metadata;
-                try { metadata = candidate.getMetadata(); }
-                catch (DataStoreException ex) { return ex; }
-
-                final boolean match = metadata.getIdentificationInfo().stream()
-                   .map(Identification::getCitation)
-                   .filter(Objects::nonNull)
-                   .map(Citation::getIdentifiers)
-                   .anyMatch((Collection<? extends Identifier> t) -> 
NameToIdentifier.isHeuristicMatchForIdentifier(t, name));
-
-                Object result = match ? candidate : null;
-
-                if (candidate instanceof Aggregate) {
-                    final Aggregate agg = (Aggregate) candidate;
-                    for (Resource comp : agg.components()) {
-                        Object rr = apply(comp);
-                        if (rr instanceof DataStoreException) {
-                            return rr;
-                        } else if (rr instanceof Resource) {
-                            if (result!=null) {
-                                return new IllegalNameException("Multiple 
resources match the name : "+name);
-                            }
-                            result = rr;
+    /**
+     * Recursively searches for a resource identified by the given identifier.
+     * This is the implementation of {@link #findResource(String)}.
+     *
+     * @param  identifier  identifier of the resource to fetch.
+     * @param  candidate   a resource to compare against the identifier.
+     * @param  visited     resources visited so-far, for avoiding never-ending 
loops if cycles exist.
+     * @return resource associated to the given identifier, or {@code null} if 
not found.
+     */
+    private Resource findResource(final String identifier, final Resource 
candidate,
+            final Map<Resource,Boolean> visited) throws DataStoreException
+    {
+        if (candidate != null && visited.put(candidate, Boolean.TRUE) == null) 
{
+            final Metadata metadata = candidate.getMetadata();
+            if (metadata != null) {
+                for (final Identification identification : 
metadata.getIdentificationInfo()) {
+                    if (identification != null) {                              
                     // Paranoiac check.
+                        final Citation citation = identification.getCitation();
+                        if (citation != null && 
NameToIdentifier.isHeuristicMatchForIdentifier(citation.getIdentifiers(), 
identifier)) {
+                            return candidate;
+                        }
+                    }
+                }
+            }
+            if (candidate instanceof Aggregate) {
+                Resource result = null;
+                for (final Resource child : ((Aggregate) 
candidate).components()) {
+                    final Resource match = findResource(identifier, child, 
visited);
+                    if (match != null) {
+                        if (result == null) {
+                            result = match;
+                        } else {
+                            throw new 
IllegalNameException(Resources.forLocale(getLocale())
+                                    
.getString(Resources.Keys.ResourceIdentifierCollision_2, getDisplayName(), 
identifier));
                         }
                     }
                 }
                 return result;
             }
-        }.apply(root);
-
-        if (res==null) {
-            throw new IllegalNameException("No resource found for name : 
"+name);
-        } else if (res instanceof DataStoreException) {
-            throw (DataStoreException) res;
         }
-        return (Resource) res;
+        return null;
     }
 
     /**


Reply via email to