/*
 *    GeoTools - The Open Source Java GIS Toolkit
 *    http://geotools.org
 * 
 *    (C) 2002-2008, Open Source Geospatial Foundation (OSGeo)
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation;
 *    version 2.1 of the License.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.filter.identity;

import java.util.Date;

import org.geotools.util.Utilities;
import org.opengis.filter.identity.ResourceId;
import org.opengis.filter.identity.Version;

/**
 * Implementation of {@link ResourceId} used for Query.
 * <p>
 * This class is mutable under one condition only; during a commit a datastore can update the
 * internal fid to reflect the real identify assigned by the database or wfs.
 * <p>
 * 
 * @author Justin Deoliveira, The Open Planning Project
 * @since 8.0
 * @source $URL$
 */
public class ResourceIdImpl extends FeatureIdVersionedImpl implements ResourceId {

    private Date startTime;
    private Date endTime;

    private long version;

    /**
     * Obtain a ResourceId based on version lookup.
     * 
     * @param fid feature being queried
     * @param featureVersion version used as a reference point 
     * @param version scope of version based query (first, last, index, all, date, etc...)
     */
    public ResourceIdImpl(String fid, String featureVersion, Version version) {
        super(fid, featureVersion, null );
        if( version == null ){
            // consider use of FeatureIdVersionedImpl
            throw new NullPointerException("Version is required for rid lookup");
        }
        this.version = version.union();
    }
    
    /**
     * Obtain a ResourceId that assumes the default, i.e. last, version.
     * 
     * @param fid
     * @param featureVersion
     */
    public ResourceIdImpl(String fid, String featureVersion) {
        this(fid, featureVersion, new Version(Version.Action.LAST));
    }
    
    /**
     * 
     * @param rid
     */
    public ResourceIdImpl(String fid, Date start, Date end ) {
        super(fid, null, null );
        if( start == null || end == null ){
            throw new NullPointerException("Start and end time are required for a lookup based on a date range");
        }
        this.startTime = start;
        this.endTime = end;
    }
    
    public void setRid(String rid) {
        setID(rid);
    }

    public void setPreviousRid(final String previousRid) {
        this.previousRid = previousRid;
    }

    public void setVersion(final Version version) {
        this.version = version.union();
    }

    @Override
    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(final Date startTime) {
        this.startTime = startTime;
    }

    @Override
    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(final Date endTime) {
        this.endTime = endTime;
    }

    @Override
    public Version getVersion() {
        return new Version( version );
    }
    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof ResourceId)) {
            return false;
        }
        if (!super.equals(obj)) {
            return false;
        }
        final ResourceId o = (ResourceId) obj;
        return Utilities.equals(featureVersion, o.getFeatureVersion())
                && Utilities.equals(previousRid, o.getPreviousRid())
                && Utilities.equals(version, o.getVersion())
                && Utilities.equals(startTime, o.getStartTime())
                && Utilities.equals(endTime, o.getEndTime());
    }

    @Override
    public int hashCode() {
        int hash = super.hashCode();
        hash = Utilities.hash(previousRid, hash);
        hash = Utilities.hash(version, hash);
        hash = Utilities.hash(startTime, hash);
        hash = Utilities.hash(endTime, hash);
        return hash;
    }
}

@@ -1775,16 +1775,35 @@
         
+ this.version = 0l;
  // cons
@@ -1849,92 +1849,29 @@
     
+%7D%0A
     
-throw new NullPointerException(%22Version is required for rid lookup%22);%0A        %7D%0A
+    else %7B%0A    
     
@@ -1902,24 +1902,34 @@
 on.union();%0A
+        %7D%0A
     %7D%0A    %0A 
@@ -3244,24 +3244,87 @@
 Version() %7B%0A
+        if( version == 0 )%7B%0A            return null;%0A        %7D%0A
         retu

allow ResourceIdImpl to use 0 to represent null Version