nl.knaw.dans.common.dbflib
Class Record

java.lang.Object
  extended by nl.knaw.dans.common.dbflib.Record

public class Record
extends Object

Represents a record in a table. A record basically maps a String key to a value object for a specified row in a table. The type of the value object depends on the field type. To find out which DBF types map to which Java types, see Type.

Values that are too large to fit in their designated fields will cause a ValueTooLargeException.

Author:
Jan van Mansum, Vesa Ã…kerman

Constructor Summary
Record(Map<String,Value> valueMap)
          Creates a new Record object.
 
Method Summary
 Boolean getBooleanValue(String fieldName)
          Returns the specified value as a Boolean object.
 Date getDateValue(String fieldName)
          Returns the specified value as a Date object.
 Number getNumberValue(String fieldName)
          Returns the value of the specified field as a Number.
 byte[] getRawValue(Field field)
          Returns the raw field value.
 String getStringValue(String fieldName)
          Returns the specified value as a java.lang.String object.
 Object getTypedValue(String fieldName)
          Returns the value as a Java object.
 boolean isMarkedDeleted()
          Returns whether the record is marked deleted in the database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Record

public Record(Map<String,Value> valueMap)
Creates a new Record object. aValueMap must specify the values for the fields in the record. The concrete Value subclasses must be compatible with the corresponding DBF field types, otherwise DataMismatchException is thrown when trying to add the record.

The following is a table of the Value subclasses, the DBF field types and the result of passing the one as a value for the other:

  CHARACTER LOGICAL NUMBER FLOAT DATE MEMO BINARY GENERAL
StringValue Accepted if within maximum length Accepted if one of "Y", "N", "T", "F" or a space, no leading/trailing spaces allowed Accepted if a valid number, that fits in the field and has exactly the number of decimals as the field's decimal count See NUMBER Accepted if in the format YYYYMMDD. No leading or trailing spaces. No check is done whether the date is itself valid. Accepted Accepted Accepted
BooleanValue Accepted, Y or N written as first character of the field Accepted, Y or N written DME)* DME DME Accepted Accepted Accepted
NumberValue Accepted, if the number fits in the field DME Accepted, if the digits before the decimal point and the minus sign (if any) together do not occupy more space than reserved for them by the field. If there are too many digits after the decimal point they are rounded See NUMBER DME Accepted Accepted Accepted
DateValue Accepted, if the CHARACTER field is at least 10 long (the size of a DATE field in DBF). DME DME DME Accepted Accepted, Date written as YYYYMMDD Accepted, Date written as YYYYMMDD Accepted, Date written as YYYYMMDD
ByteArrayValue DME DME DME DME DME Accepted, Date written as YYYYMMDD Accepted, Date written as YYYYMMDD Accepted, Date written as YYYYMMDD
)* DataMismatchException

Parameters:
valueMap - the mapping from field name to field value
Method Detail

getRawValue

public byte[] getRawValue(Field field)
                   throws DbfLibException
Returns the raw field value. The raw field value is the bytes as stored in the DBF file. If the value is empty null or a series of ASCII spaces may be returned.

Parameters:
field - the field for which to get the raw value
Returns:
a byte array
Throws:
DbfLibException - if the value was too large to be read

getTypedValue

public Object getTypedValue(String fieldName)
Returns the value as a Java object. The type of Java object returned depends on the field type in the xBase database. See Type for the mapping between the two.

Parameters:
fieldName - the field for which to get the value
Returns:
a Java object

getNumberValue

public Number getNumberValue(String fieldName)
Returns the value of the specified field as a Number. The exact subclass of Number used depends on the size of the corresponding Field and its decimalCount property. If decimalCount is zero an integral type is returned, otherwise a fractional type. Depending on the size java.lang.Integer, java.lang.Long or java.math.BigInteger is used as an integral type. For non-integral types the classes used are either java.lang.Double or java.math.BigDecimal.

It is not necessary to know the exact type used. You can use the conversion methods on the java.lang.Number class to convert the value before using it. (E.g., Number.intValue().) Of course you do need to know whether the value will fit in the chosen type. Note that comparisons may fail if you do not first convert the values. For instance if you compare the a java.math.BigInteger with a long using the equals method, false will be returned even if the values represent the same logical value.

Example:


 public Record searchSomeNum(final double val)
 {
      //
      //... SOME CODE HERE THAT RETRIEVES table1 ...
      //

      // Get a record iterator to loop over all the records.
      Iterator ri = table1.recordIterator();

      // Search for the record with SOMENUM = val
      while(ri.hasNext())
      {
         Record r = ri.next();
         Number n = r.getNumberValue("SOMENUM");

         // Convert n to a double before comparing it.
         if(n.doubleValue() == val)
         {
            return r;
         }
      }

      return null;
 }

 

Parameters:
fieldName - the name of the field with numerical data
Returns:
a Number object

getStringValue

public String getStringValue(String fieldName)
Returns the specified value as a java.lang.String object.

Parameters:
fieldName - the name of the field with character data
Returns:
a String object

getBooleanValue

public Boolean getBooleanValue(String fieldName)
Returns the specified value as a Boolean object.

Parameters:
fieldName - the name of the field with logical data
Returns:
a Boolean object

getDateValue

public Date getDateValue(String fieldName)
Returns the specified value as a Date object.

Parameters:
fieldName - the name of the field with date data
Returns:
a Date object

isMarkedDeleted

public boolean isMarkedDeleted()
Returns whether the record is marked deleted in the database. In the original dBase program this meant that the record was still visible but had a "deleted" flag.

Returns:
deleted status


Copyright © 2009-2012 DANS. All Rights Reserved.