If the model contains ILcdDataObject
instances, you can use the ILcdModel.query
method in combination with a TLcdOGCSortBy
to find the domain object with the minimum or maximum value for a certain property.
This example decodes country data of the whole world, and finds the countries with the smallest and the largest populations
based on the "POP_1994"
property.
String countriesFile = "Data/Shp/NaturalEarth/50m_cultural/ne_50m_admin_0_countries_lakes.shp";
TLcdCompositeModelDecoder modelDecoder =
new TLcdCompositeModelDecoder(TLcdServiceLoader.getInstance(ILcdModelDecoder.class));
ILcdModel model = modelDecoder.decode(countriesFile);
try (TLcdLockUtil.Lock lock = TLcdLockUtil.readLock(model)) {
String propertyName = "POP_EST";
//Create a TLcdOGCSortBy instance which specifies that we want
//to order the objects based on the value of a property
TLcdOGCSortBy ascendingSortOrder =
TLcdOGCSortBy.comparing(TLcdOGCFilterFactory.property(propertyName), ASC);
TLcdOGCSortBy descendingSortOrder =
TLcdOGCSortBy.comparing(TLcdOGCFilterFactory.property(propertyName), DESC);
//Use the ILcdModel.query method to perform the query with the TLcdOGCSortBy
//Since we are only interested in the min/max value,
//we can limit the result of the query method to a single element
Object domainObjectWithMinimumValue =
model.query(ILcdModel.all().sorted(ascendingSortOrder).limit(1))
.findFirst()
.get();
Object domainObjectWithMaximumValue =
model.query(ILcdModel.all().sorted(descendingSortOrder).limit(1))
.findFirst()
.get();
System.out.println("Minimum value is: " +
((ILcdDataObject) domainObjectWithMinimumValue).getValue(propertyName));
System.out.println("Maximum value is: " +
((ILcdDataObject) domainObjectWithMaximumValue).getValue(propertyName));
}
The performance of this method depends on the implementation of the For example, in-memory models will most likely loop over all elements. Database models can convert the query to an SQL statement and let the database do the sorting. For more information, see Performance guidelines. |