|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.oopitis.weather.WeatherProperty<T>
T - the type of the data this property representspublic abstract class WeatherProperty<T>
An object of this class represents a piece of weather information. Every
weather property is typed. The type of the data a property
represents is determined when the property is defined. Once a property is
defined, the data type cannot change. The strong typing
is enforced by the compiler via the use of Java generics. At runtime, the
type can be discovered by calling getValueClass.
A weather property is able to format the data it represents for a locale. The formatted data is fundamentally a string representation of the data object. In case of numeric types, formatting data often involves conversion to a different unit of measure. The string representation may look significantly different from the original value. For example,
Here is the output:Float t = report.get(WeatherService.TempCelsius); // Format the temperature for locale US. System.out.println(WeatherService.TempCelsius.format(t, Locale.US)); // Format the temperature according to the property definition. System.out.println(WeatherService.TempCelsius.format(t, null)); // Format the temperature for the system default locale (which is also US // in this example). System.out.println(WeatherService.TempCelsius.format(t)); Float w = report.get(WeatherService.WindDirectionDegree); System.out.println(WeatherService.WindDirectionDegree.format(w));
Notice how60.06 °F 15.59 °C 60.06 °F Southwest (227°)
null represents the default string representation.
A weather property itself also has a locale-sensitive string
representation which is the display name of the property.
In addition to the display name, a WeatherProperty
object, which is often assigned to a final static field, has a
constant name.
The constant name is the fully qualified name of the Java identifier the
object is assigned to. For example, the display name of
com.oopitis.weather.WeatherService.SunsetTime for
Locale.US is "Sunset Time" while the constant name is
"com.oopitis.weather.WeatherService.SunsetTime". The constant names are
useful when a list of weather properties is given without documentation.
Implementation note: While technically it is possible to define a
property without using generics, or to define a property as
WeatherProperty<Object>, such practice must be avoided. The
data type must be as specific as possible. As a rule, the data types are
primitive types or simple data structures. When defining new properties,
properties of the same data type should reuse the same class. It is neither
necessary nor optimal to create a distinct class for every new property.
Objects of this class are meant to be constants. They are recognized by
their object references as opposed to internal keys or numeric IDs. Having
multiple semantically identical weather properties is confusing
and must be avoided. If applicable, it is customary to include the unit of
measure in the constant name of a property. For example,
VisibilityKilometer is used instead of
Visibility in WeatherService.
| Field Summary | |
|---|---|
protected static java.util.Locale |
LOCALE_RAW
A locale with language code "" and country code "". |
protected java.lang.String |
resourceBundleName
The base resource bundle name from the constructor. |
protected java.lang.String |
resourceKey
The resource key from the constructor. |
| Constructor Summary | |
|---|---|
protected |
WeatherProperty(java.lang.String constantName)
Constructs a weather property with a constant name. |
protected |
WeatherProperty(java.lang.String constantName,
java.lang.String resourceBundleName,
java.lang.String resourceKey)
Constructs a weather property with a locale-sensitive name from resource bundles. |
| Method Summary | |
|---|---|
java.lang.String |
format(T value)
Formats the data this property represents for the default locale. |
abstract java.lang.String |
format(T value,
java.util.Locale locale)
Formats the data this property represents for a given locale. |
java.lang.String |
getConstantName()
Gets the fully qualified Java name of this constant. |
WeatherProperty[] |
getQueryableProperties()
Returns an array with this property as the only element. |
abstract java.lang.Class<T> |
getValueClass()
Gets the Class object of the data this property represents. |
java.lang.String |
toString()
Returns the string representation for the default locale. |
java.lang.String |
toString(java.util.Locale locale)
Returns a locale-specific string representation. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
| Field Detail |
|---|
protected static final java.util.Locale LOCALE_RAW
protected final java.lang.String resourceBundleName
protected final java.lang.String resourceKey
| Constructor Detail |
|---|
protected WeatherProperty(java.lang.String constantName)
constantName - the constant name
java.lang.NullPointerException - if the given constant name is null
protected WeatherProperty(java.lang.String constantName,
java.lang.String resourceBundleName,
java.lang.String resourceKey)
constantName - the constant nameresourceBundleName - the base resource bundle nameresourceKey - the resource key
java.lang.NullPointerException - if any of the arguments is null| Method Detail |
|---|
public abstract java.lang.Class<T> getValueClass()
Class object of the data this property represents.
Class object of the data this property
represents
public abstract java.lang.String format(T value,
java.util.Locale locale)
value - the data to be formattedlocale - the locale for which the data is to be formatted;
null if the data is to be formatted
according to the property definition
public java.lang.String format(T value)
this.format(value, Locale.getDefault()).
value - the data to be formatted
format(Object,Locale),
Locale.getDefault()public final WeatherProperty[] getQueryableProperties()
getQueryableProperties in interface Queryablepublic final java.lang.String getConstantName()
public java.lang.String toString(java.util.Locale locale)
Implementation note: Subclasses that override this method
without overriding toString() must never invoke
toString() directly or indirectly in the implementation.
Doing so will result in an infinite loop and eventually a
StackOverflowError.
locale - the locale for which the string is prepared; if
null, the system default locale is used.
java.util.MissingResourceException - if a resource key and
bundle name are specified but the key is not found in any
variations or the base bundletoString()public java.lang.String toString()
this.toString(Locale.getDefault()) if the value is not null;
otherwise the constant name is returned.
toString in class java.lang.ObjecttoString(Locale),
Locale.getDefault()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||