This section describes how to retrieve the value stored in a RecordAttribute object.
A RecordAttribute object can be retrieved from a Record with the find() method. It takes a String containing the name of the attribute to get as it's argument. The same goes for the find() method is RecordAttributeCollection.
RecordAttribute ra = rec.find("ATTRNAME");
To determine which kind of data the record attribute holds, all record
attributes have a data type. To find out the type for an attribute, use the getDataType()
method. It returns a DataType object. The object
returned can then be compared with any of the static fields of the DataType
class, for instance DataType.TEXT
.
The table below shows which type of object the RecordAttribute's getValue()
method returns for each of the data types.
Data type | Value's class |
---|---|
AGGREGATE | Record |
ALPHA | String |
ARRAY | RecordCollection |
BINARY | ByteData |
BOOLEAN | Boolean |
DATE | Date (only date information may be used) |
DECIMAL | Double |
ENUM | String |
FLOAT | Double |
INTEGER | Long |
TEXT | String |
TIME | Date (only time information may be used) |
TIMESTAMP | Date |
When retrieving the value there are two ways to go. Either by using the getValue() method, which can always be used and returns an Object, or by using one of the more specialized methods in the table below. The default value is what is returned if the value is of another type or not set. Example: trying to use getDouble() on an attribute containing the value "a" (a String) will return Double.NaN. For more detailed information on what's returned, see the API documentation for RecordAttribute.
Method name | Returns | Default value |
---|---|---|
getAggregate | A Record if the attribute is an aggregate. | null |
getArray | A RecordCollection if the attribute is an array. | null |
getBinary | A Bytedata object if the attribute contains binary data. | null |
getBoolean | The boolean value, if the attribute's of BOOLEAN data type. Note! The default value cannot be distinguished from the boolean value false. | false |
getDate | The Date value if it's a Date or, if it's a Long, a Date representing the long value. | null |
getDouble | The double value if the value is a Long or Double. | Double.NaN |
getLong | The long value if the value is a Long. If it's a Double, the value is typecast to a long and returned. If it's a Date, the date's long value (getTime) is returned. | Long.MIN_VALUE |
getString | The String representation of the value. All simple values can be converted to String. | null |
A RecordAttribute's value can either be simple or compound. AGGREGATE and ARRAY attributes are compound, the rest are simple. By simple it means that it only contains one value, as opposed to an aggregate which have a Record as value (and that record contains other values). Reference attributes are however something in between, see the next section.
Relevant methods here: isSimple(), isCompound().
A key attribute is one that have a domain associated with it's value.
Use the isKey()
method to find out if an attribute is a key. If so,
the getDomain()
method may be used to fetch the domain. Also, there
is a getIdentity()
method which returns both value & domain in
the same String. This method can only be used for key or reference
attributes. If used for other attributes, an exception will occur.
A reference attribute also have a domain associated with it's value.
Key method here: isReference()
. A reference attribute may also, not
necessarily though, have a Record associated with it. That's called the referent
record and is retrieved with the getReferent()
method. If there is
no referent record, the method returns null
. The value of a
reference attribute normally is the same as the key attribute of it's referent
record.
Only ALPHA, TEXT and INTEGER attributes can be a key or reference. A RecordAttribute cannot be both a key & a reference.
Imagine a record with the following structure (the @ sign is the divisor between the value and the domain):
<PERSON> FIRST_NAME:TK=Homer@simpsons LAST_NAME:T=Simpson AGE:I=38 SPOUSE:T=Marge==> <PERSON> FIRST_NAME:TK=Marge@simpsons CHILDREN:ARRAY= <PERSON> FIRST_NAME:T=Bart <PERSON> FIRST_NAME:T=Lisa <PERSON> FIRST_NAME:T=Maggie
Here are some examples on how to safely get the values from the record. Safely here means without risk of getting a NullPointerException, which could happen if you are expecting this record but not getting it (or only parts thereof). The example assumes the record is in the variable named rec.
String fname = null, domain = null; long age = 0; Record spouse; RecordCollection children; RecordAttribute ra; ra = rec.find("FIRST_NAME"); if(ra != null) { fname = ra.getString(); if(ra.isKey()) domain = ra.getDomain(); } ra = rec.find("AGE"); if(ra != null && ra.getDataType() == DataType.INTEGER) age = ra.getLong(); // int and long values are handled the same. ra = rec.find("SPOUSE"); if(ra != null && ra.isReference()) spouse = ra.getReferent(); ra = rec.find("CHILDREN"); if(ra != null && ra.getDataType() == DataType.ARRAY) children = ra.getArray()