/*
Copyright (c) 2013, Colorado State University
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
   list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.

This software is provided by the copyright holders and contributors "as is" and
any express or implied warranties, including, but not limited to, the implied
warranties of merchantability and fitness for a particular purpose are
disclaimed. In no event shall the copyright holder or contributors be liable for
any direct, indirect, incidental, special, exemplary, or consequential damages
(including, but not limited to, procurement of substitute goods or services;
loss of use, data, or profits; or business interruption) however caused and on
any theory of liability, whether in contract, strict liability, or tort
(including negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage.
*/

package galileo.dataset;

import java.io.IOException;

import galileo.serialization.ByteSerializable;
import galileo.serialization.SerializationInputStream;
import galileo.serialization.SerializationOutputStream;

public class SpatialRange implements ByteSerializable {
    private float upperLat;
    private float lowerLat;
    private float upperLon;
    private float lowerLon;

    private boolean hasElevation;
    private float upperElevation;
    private float lowerElevation;

    public SpatialRange(float lowerLat, float upperLat,
            float lowerLon, float upperLon) {
        this.lowerLat = lowerLat;
        this.upperLat = upperLat;
        this.lowerLon = lowerLon;
        this.upperLon = upperLon;

        hasElevation = false;
    }

    public SpatialRange(float lowerLat, float upperLat,
            float lowerLon, float upperLon,
            float upperElevation, float lowerElevation) {
        this.lowerLat = lowerLat;
        this.upperLat = upperLat;
        this.lowerLon = lowerLon;
        this.upperLon = upperLon;

        hasElevation = true;
        this.upperElevation = upperElevation;
        this.lowerElevation = lowerElevation;
    }

    public float getUpperBoundForLatitude() {
        return upperLat;
    }

    public float getLowerBoundForLatitude() {
        return lowerLat;
    }

    public float getUpperBoundForLongitude() {
        return upperLon;
    }

    public float getLowerBoundForLongitude() {
        return lowerLon;
    }

    public Coordinates getCenterPoint() {
        float latDifference = upperLat - lowerLat;
        float latDistance = latDifference / 2;

        float lonDifference = upperLon - lowerLon;
        float lonDistance = lonDifference / 2;

        return new Coordinates(lowerLat + latDistance,
                               lowerLon + lonDistance);
    }

    public boolean hasElevationBounds() {
        return hasElevation;
    }

    public float getUpperBoundForElevation() {
        return upperElevation;
    }

    public float getLowerBoundForElevation() {
        return lowerElevation;
    }

    @Override
    public String toString() {
        return "(" + lowerLat + ", " + lowerLon
            + "), (" + upperLat + ", " + upperLon + ")";
    }

    @Deserialize
    public SpatialRange(SerializationInputStream in)
    throws IOException {
        lowerLat = in.readFloat();
        upperLat = in.readFloat();
        lowerLon = in.readFloat();
        upperLon = in.readFloat();

        hasElevation = in.readBoolean();
        if (hasElevation) {
            lowerElevation = in.readFloat();
            upperElevation = in.readFloat();
        }
    }

    @Override
    public void serialize(SerializationOutputStream out)
    throws IOException {
        out.writeFloat(lowerLat);
        out.writeFloat(upperLat);
        out.writeFloat(lowerLon);
        out.writeFloat(upperLon);

        out.writeBoolean(hasElevation);
        if (hasElevation) {
            out.writeFloat(lowerElevation);
            out.writeFloat(upperElevation);
        }
    }
}

@@ -2489,32 +2489,126 @@
 evation;%0A    %7D%0A%0A
+    /*%0A     * Retrieves the smallest latitude value of this spatial range going east.%0A     */%0A
     public float
@@ -2603,35 +2603,35 @@
 public float get
-Upp
+Low
 erBoundForLatitu
@@ -2652,19 +2652,19 @@
  return 
-upp
+low
 erLat;%0A 
@@ -2661,32 +2661,125 @@
 owerLat;%0A    %7D%0A%0A
+    /*%0A     * Retrieves the largest latitude value of this spatial range going east.%0A     */%0A
     public float
@@ -2774,35 +2774,35 @@
 public float get
-Low
+Upp
 erBoundForLatitu
@@ -2815,35 +2815,35 @@
 %0A        return 
-low
+upp
 erLat;%0A    %7D%0A%0A  
@@ -2832,32 +2832,128 @@
 pperLat;%0A    %7D%0A%0A
+    /*%0A     * Retrieves the smallest longitude value of this spatial range going south.%0A     */%0A
     public float
@@ -2948,35 +2948,35 @@
 public float get
-Upp
+Low
 erBoundForLongit
@@ -2998,19 +2998,19 @@
  return 
-upp
+low
 erLon;%0A 
@@ -3007,32 +3007,127 @@
 owerLon;%0A    %7D%0A%0A
+    /*%0A     * Retrieves the largest longitude value of this spatial range going south.%0A     */%0A
     public float
@@ -3122,35 +3122,35 @@
 public float get
-Low
+Upp
 erBoundForLongit
@@ -3164,35 +3164,35 @@
 %0A        return 
-low
+upp
 erLon;%0A    %7D%0A%0A  

Add javadoc explaining SpatialRange bounds