Chapter 13. Special Datatypes

Table of Contents

1. Boolean Values
2. String Patterns
2.1. Telephone numbers
2.2. Email addresses
2.3. URLs
3. Special Numeric Data Types
3.1. Positive and non-negative integers
3.2. The unit interval
3.3. Percentages
3.4. Arbitrary precision numbers
4. Calendar Dates and Times
4.1. Using JavaScript's Date class
4.2. Calendar dates and times in the user interface
4.3. Using Java's Date and Calendar classes
4.4. Using Date and Calendar with JPA
4.5. Rendering Date Attributes with JSF
5. Quantities
6. Complex Data Types
6.1. Records
6.2. Data collections

In an app, we have to deal with various types of attributes. While many of them have a standard datatype, like Integer or String, as their range, some of them may have special datatypes as their range. For instance, the Universal Business Language (UBL), which is an industry standard defining document formats for electronic commerce, defines a large set of business datatypes including the following.

UBL Data TypeDescription
DateOne calendar day according to the Gregorian calendar.
TimeAn instance of time that occurs every day.
DateTimeA particular point in the progression of time, together with relevant supplementary information.
PercentNumeric information that is assigned or is determined by calculation, counting, or sequencing and is expressed as a percentage.
AmountA number of monetary units specified using a given unit of currency.

The Semantic MediaWiki uses the following special datatypes.

Semantic MediaWiki Data TypeDescription
Geographic coordinateHolds coordinates describing geographic locations
Monolingual textTo associate a text value with a specific language code
QuantityHolds values that describe quantities, containing both a number and a unit
RecordAllows saving compound property values that consist of a short list of values with fixed type and order
EmailHolds e-mail addresses
URLHolds URIs, URNs and URLs
Telephone numberHolds international telephone numbers based on the RFC 3966 standard

We discuss several important cases of special datatypes and how to deal with them in the model code and in the HTML user interface code. The following class diagram illustrates examples of attributes having such a special datatype as their range.

Figure 13.1. An information design model with special datatypes.

An information design model with special datatypes.

In the C++ and C# programming communities, special datatypes like string patterns and quantities are also called "semantic types" (see, e.g., Gibson2016 and Perdeck2015).

1. Boolean Values

Boolean attributes, like isSmoker, can only have one of two possible values: true or false, which are sometimes more naturally expressed with yes or no. In the model code, Boolean attributes are treated like primitive datatype attributes, since all programming languages support Boolean as a primitive datatype.

For rendering a Boolean attribute in a user interface, there are several options. The simplest solution is using a checkbox field. Alternatively, a single-selection list with the two options true and false (or yes and no), or a corresponding radio button group with two radio buttons, could be used.

In an HTML form, a checkbox field is implemented with an input element of type checkbox. For assigning the value of the checkbox field to the Boolean attribute, the field's checked attribute has to be used, as shown in the following example where we have a checkbox field in an HTML form:

<form id="Person">
  <p><label>
   Is smoker: <input type="checkbox" name="isSmoker" />
  </label></p>
</form>

In JavaScript code, the checkbox field is accessed via its name, and the Boolean attribute is assigned by reading the field's checked attribute (instead of its value attribute), like so:

var isSmokerCheckboxField = document.forms["Person"].isSmoker;
var newPerson = new Person(...);
newPerson.isSmoker = isSmokerCheckboxField.checked;