Monday, July 28, 2008

Using BETWEEN in an EJB3 query


Say if you have an EJB3 entity bean named Landmark which is mapped to a table called landmarks which has 2 columns longitude & latitude (amongst others). To query a given latitude & longitude, its a good idea to relax the query a bit by using BETWEEN.



An example is shown below where we allow a tolerance of 0.00003 for both latitude & longitude. The sample shown is an excerpt from an EJB3 entity bean, right before its class declaration.


@Entity
@Table(name = "landmarks")
@NamedQueries( {
@NamedQuery(
name = "Landmark.findByCoordinates",
query = "SELECT lgr FROM Landmark lgr " +
"WHERE lgr.latitude BETWEEN (?1 - 0.00003) AND (?1 + 0.00003) " +
"AND lgr.longitude BETWEEN (?2 - 0.00003) AND (?2 + 0.00003) ")
})
public class Landmark implements Serializable {

Convert NMEA latitude & longitude to decimal

Here's how you convert latitude & longitude obtained from NMEA's GPGGA to decimal:











 NMEADecimal
latitude0302.7846903 + (02.78469/60) = 3.046412
longitude10141.82531101 + 41.82531/60) = 101.6971


Notice that for latitude of 0302.78469,


03 ==> degress. counted as is


02.78469 ==> minutes. divide by 60 before adding to degrees above


Hence, the decimal equivalent is:


03 + (02.78469/60) = 3.046412

Friday, May 30, 2008

Comparing byte and int in Java

Yet another blog title of mine with "in Java" at the end :)

Anyway, one of the most confusing aspects of dealing with byte in Java was its actual value range. Based on some simple JUnit tests I made, I got the following output:

0 byte = 0 int
1 byte = 1 int
126 byte = 126 int
127 byte = 127 int
-128 byte = 128 int
-127 byte = 129 int
-2 byte = 254 int
-1 byte = 255 int

Interesting ....

From here we can deduce:
1) byte goes from 0 to 127 then -128 to -1
2) the range above actually corresponds to integer values of 0 to 127 then 128 to 255

Here is the JUnit test case which produced the above output. Note that Java does not allow byte values:
1) beyond or equal 128 and;
2) less or equal to -129
... which explains the remarked code portions below.


import static org.junit.Assert.*;
import org.junit.Test;

public class BaitTest {

@Test
public void printNegativeByte() {
byte baitValue;
int intValue;

baitValue = 0;
intValue = baitValue & 0xFF;
System.out.println(baitValue + " byte = " + intValue + " int");

baitValue = 1;
intValue = baitValue & 0xFF;
System.out.println(baitValue + " byte = " + intValue + " int");

baitValue = 126;
intValue = baitValue & 0xFF;
System.out.println(baitValue + " byte = " + intValue + " int");

baitValue = 127;
intValue = baitValue & 0xFF;
System.out.println(baitValue + " byte = " + intValue + " int");

// baitValue = 128;
// intValue = baitValue & 0xFF;
// System.out.println(baitValue + " byte = " + intValue + " int");

// baitValue = -129;
// intValue = baitValue & 0xFF;
// System.out.println(baitValue + " byte = " + intValue + " int");

baitValue = -128;
intValue = baitValue & 0xFF;
System.out.println(baitValue + " byte = " + intValue + " int");

baitValue = -127;
intValue = baitValue & 0xFF;
System.out.println(baitValue + " byte = " + intValue + " int");

baitValue = -2;
intValue = baitValue & 0xFF;
System.out.println(baitValue + " byte = " + intValue + " int");

baitValue = -1;
intValue = baitValue & 0xFF;
System.out.println(baitValue + " byte = " + intValue + " int");
}

}