Class ALcyDomainObjectConverter

java.lang.Object
com.luciad.lucy.model.ALcyDomainObjectConverter
Direct Known Subclasses:
TLcyCompositeDomainObjectConverter

public abstract class ALcyDomainObjectConverter extends Object

Abstract class which groups methods to create adapter objects for domain objects, and to convert domain objects to new domain objects for the same or another model.

The adapt method creates an adapter object for a domain object, which is linked to this domain object. Changes made to either the adapter object or the original domain object affects the other object. The convert method on the other hand creates a new domain object, which has no link with the original domain object. Changes made to the converted domain object will not alter the original domain object.

An example use of the adapt method is to change the radius of a circle, which is contained in a domain object. The domain object can be seen as a black box, and therefore there is no setRadius method available, although it contains a circle. The adapt method, which knows about the structure of the domain object, can return an instance of ILcdCircle which function as an adapter object. Calling the setRadius method on the adapter object results in a change of the radius of the circle, contained in the domain object.

The convert method creates a new object for a certain target model. This target model can be different from the model of the original domain object. It also allows to convert the shape of the domain model. Changes to the converted object do not alter the original object since the original object and the resulting converted object are two distinct objects.

Example usages of the convert method are the conversion from a polyline to a buffer, a circle to a sphere, ...

Note: currently there are only ALcyDomainObjectConverter instances available for the domain objects of drawing models. Those domain object converters can be obtained through the TLcyDrawingFormat.getDomainObjectConverter() method:


   ILcyLucyEnv lucy = ...;
   TLcyDrawingAddOn drawingAddOn = lucy.retrieveAddOnByClass(TLcyDrawingAddOn.class);
   ALcyDomainObjectConverter converter = drawingAddOn.getDrawingFormat().getDomainObjectConverter();
 

Example usages

Changing the geometry of a domain object

To change the geometry of the domain object, adapt the domain object to an ILcdShape extension, and interact with that shape adapter. This allows you to make changes to the geometry without actual knowledge of the domain object implementation details.

   //adapt a domain object to an ILcdShape to be able to modify the shape properties
   //a write lock is required as the shape will be modified
   try(TLcdLock.Lock lock = TLcdLockUtil.writeLock(aModel)){
     if (converter.canAdapt(domainObject, aModel, ILcdPolyline.class)){
       ILcdPolyline adapter = converter.adapt(domainObject, aModel, ILcdPolyline.class);

       //change the polyline
       adapter.move2DPoint(0, 10, 10);
       aModel.elementChanged(domainObject, ILcdModel.FIRE_LATER);
     }
   } finally{
     aModel.fireCollectedModelChanges();
   }
 

Add an extruded version of a domain object to a model:


   //convert a domain object to an extruded version, suited for the same model
   //a write lock is required as the extruded version will be added to the model
   try(TLcdLockUtil.Lock lock = TLcdLockUtil.writeLock(aModel)){
     if (converter.canConvert(domainObject, aModel, aModel, ILcdExtrudedShape.class)) {
       //convert the domain object to an object which can be adapted to an ILcdExtrudedShape
       Object convertedObject = converter.convert(domainObject, aModel, aModel, ILcdExtrudedShape.class);

       //adapt the object to an ILcdExtrudedShape
       ILcdExtrudedShape extrudedShape = converter.adapt(convertedObject, aModel, ILcdExtrudedShape.class);

       //use the adapter to retrieve the extruded shape properties
       if (extrudedShape.getMinimumZ() > 0){
         //do something with the shape
       }

       //add the extruded version of the domain object to the model
       if (aModel.canAddElement(convertedObject)) {
         aModel.addElement(convertedObject, ILcdModel.FIRE_LATER);
       }
     }
   } finally {
     aModel.fireCollectedModelChanges();
   }
 

Add a copy of a domain object from one model into another model:

The converter can be used to copy domain objects from one model to another. Those models could be models from the same format, but might as well models from different formats. It can for example be used to import GeoJSON domain objects into a Lucy drawing model.

   //copy a domain object from one model with a geodetic reference to another model with a grid reference
   // domainObject is an element of aGeodeticReferenceModel in this example
   Object convertedObject = null;
   try(TLcdLockUtil.Lock readLock = TLcdLockUtil.readLock(aGeodeticReferenceModel)){
     if (converter.canConvert(domainObject, aGeodeticReferenceModel, aGridReferenceModel)){
       convertedObject = converter.convert(domainObject, aGeodeticReferenceModel, aGridReferenceModel);
     }
   }
   if (convertedObject != null){
     //add the converted object to the target model
     try(TLcdLockUtil.Lock lock = TLcdLockUtil.writeLock(aGridReferenceModel)){
       if (aGridReferenceModel.canAddElement(convertedObject)){
         aGridReferenceModel.addElement(convertedObject, ILcdModel.FIRE_LATER);
       }
     } finally{
       aGridReferenceModel.fireCollectedModelChanges();
     }
   }
 

Converting a circle domain object to a sphere domain object:


   //convert a domain object containing a circle to a domain object containing a sphere
   try(TLcdLockUtil.Lock lock = TLcdLockUtil.writeLock(aModel)){
     if (converter.canConvert(circleDomainObject, aModel, aModel, TLcdLonLatHeightSphere.class)){
       Object sphereDomainObject = converter.convert(circleDomainObject, aModel, aModel, TLcdLonLatHeightSphere.class);

       //add the domain object to the model
       if (aModel.canAddElement(sphereDomainObject)){
         aModel.addElement(sphereDomainObject, ILcdModel.FIRE_LATER);
       }
     }
   } finally{
     aModel.fireCollectedModelChanges();
   }
 
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    Creates a new ALcyDomainObjectConverter.
  • Method Summary

    Modifier and Type
    Method
    Description
    abstract <T> T
    adapt(Object aDomainObject, ILcdModel aSourceModel, Class<T> aDesiredClass)
    Creates an adapter object for aDomainObject contained in aSourceModel, which will be an instance of aDesiredClass.
    abstract boolean
    canAdapt(Object aDomainObject, ILcdModel aSourceModel, Class aDesiredClass)
    Indicates whether this converter can create an adapter object for aDomainObject, which will be an instance of aDesiredClass.
    boolean
    canConvert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel)
    The default implementation of this method calls the canConvert method where the parameter aCanAdaptToClass is set to Object.class.
    abstract boolean
    canConvert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel, Class aCanAdaptToClass)
    Indicates whether this converter is capable of converting a domain object aDomainObject of aSourceModel into a domain object for the model aTargetModel.
    convert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel)
    The default implementation of this method calls the convert method where the parameter aCanAdaptToClass is set to Object.class.
    abstract Object
    convert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel, Class aCanAdaptToClass)
    Converts a domain object aDomainObject of aSourceModel into a domain object for the model aTargetModel.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ALcyDomainObjectConverter

      protected ALcyDomainObjectConverter()
      Creates a new ALcyDomainObjectConverter.
  • Method Details

    • canAdapt

      public abstract boolean canAdapt(Object aDomainObject, ILcdModel aSourceModel, Class aDesiredClass)

      Indicates whether this converter can create an adapter object for aDomainObject, which will be an instance of aDesiredClass. The object aDomainObject is contained in aSourceModel.

      The adapter object and the domain object are linked, which means changes made to the adapter object will be passed on to the original object and vice versa.

      Since this method indicates whether an adapter object for aDomainObject can be created, it must return false when the adapter object adds information to the aDomainObject. The adapter object may only contain less information than the original object. For example a sphere can be adapted to a circle, since this simply removes the height information. Adapting a circle to a sphere should not be possible, since the adapter object (the sphere) contains information that is not available in the original object (the height). For such cases the convert method should be used instead.

      Parameters:
      aDomainObject - the original object
      aSourceModel - the model containing aDomainObject
      aDesiredClass - the class of the adapter object
      Returns:
      true when this converter can perform the adapt operation
    • adapt

      public abstract <T> T adapt(Object aDomainObject, ILcdModel aSourceModel, Class<T> aDesiredClass) throws IllegalArgumentException

      Creates an adapter object for aDomainObject contained in aSourceModel, which will be an instance of aDesiredClass.

      The adapter object and the domain object are linked, which means changes made to the adapter object will be passed on to the original object and vice versa.

      An IllegalArgumentException must be thrown when the converter cannot handle the domain object, or could not adapt the domain object to an instance of aDesiredClass. It is possible to check in advance if this converter is capable of creating an adapter object for the domain object by calling the canAdapt method first.

      Parameters:
      aDomainObject - the original object
      aSourceModel - the model containing aDomainObject
      aDesiredClass - the class of the adapter object
      Returns:
      the adapter object for aDomainObject, which is an instance of aDesiredClass
      Throws:
      IllegalArgumentException - when the converter could not create an adapter object of aDesiredClass for aDomainObject
    • canConvert

      public abstract boolean canConvert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel, Class aCanAdaptToClass)

      Indicates whether this converter is capable of converting a domain object aDomainObject of aSourceModel into a domain object for the model aTargetModel. The parameter aCanAdaptToClass is passed to this method to check if the creation of a new domain object is possible in such a way that the adapt method called with that new domain object will return an object of instance aCanAdaptToClass.

      Notice that the source and target model may be the same. When the aCanAdaptToClass is not important, Object.class should be passed as parameter or the canConvert(Object, com.luciad.model.ILcdModel, com.luciad.model.ILcdModel) method can be used instead.

      Parameters:
      aDomainObject - the original domain object
      aSourceModel - the model containing aDomainObject
      aTargetModel - the model for which a new domain object should be created.
      aCanAdaptToClass - adapting the resulting new domain object to this class should be possible
      Returns:
      true when the conversion can be done by this converter
    • canConvert

      public boolean canConvert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel)

      The default implementation of this method calls the canConvert method where the parameter aCanAdaptToClass is set to Object.class. This method should therefore be used when the resulting domain object does not need to be adapted to a specific class afterwards.

      Parameters:
      aDomainObject - the original domain object
      aSourceModel - the model containing aDomainObject
      aTargetModel - the model for which a new domain object should be created.
      Returns:
      true when the conversion can be done by this converter
    • convert

      public abstract Object convert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel, Class aCanAdaptToClass) throws IllegalArgumentException, TLcdOutOfBoundsException

      Converts a domain object aDomainObject of aSourceModel into a domain object for the model aTargetModel. The parameter aCanAdaptToClass will influence the creation of the new domain object in such a way that the adapt method called with that new domain object will return an object of instance aCanAdaptToClass.

      Note that the source and target model may be the same. When the aCanAdaptToClass is not important, Object.class should be passed as parameter or the convert(Object, com.luciad.model.ILcdModel, com.luciad.model.ILcdModel) method can be used instead.

      An IllegalArgumentException must be thrown when the converter cannot handle the domain object, or could not convert the domain object in such a way that the resulting domain object is adaptable to an instance of aCanAdaptToClass afterwards. It is possible to check in advance if this converter is capable of converting the domain object by calling the canConvert method first.

      Parameters:
      aDomainObject - the original domain object
      aSourceModel - the model containing aDomainObject
      aTargetModel - the model for which a new domain object should be created.
      aCanAdaptToClass - adapting the resulting new domain object to this class should be possible
      Returns:
      a new domain object for the model aTargetModel, which can be adapted to an instance of aCanAdaptToClass
      Throws:
      IllegalArgumentException - when the converter could not handle the domain object, or could not convert the domain object in such a way that the resulting domain object is adaptable to an instance of aCanAdaptToClass afterwards.
      TLcdOutOfBoundsException - when restoring the state for the target model failed because the coordinates could not be properly converted from the source ILcdModelReference to the target ILcdModelReference.
    • convert

      public Object convert(Object aDomainObject, ILcdModel aSourceModel, ILcdModel aTargetModel) throws IllegalArgumentException, TLcdOutOfBoundsException

      The default implementation of this method calls the convert method where the parameter aCanAdaptToClass is set to Object.class. This method should therefore be used when the resulting domain object does not need to be adapted to a specific class afterwards.

      Parameters:
      aDomainObject - the original domain object
      aSourceModel - the model containing aDomainObject
      aTargetModel - the model for which a new domain object should be created.
      Returns:
      a new domain object for the model aTargetModel, which can be adapted to an instance of aCanAdaptToClass
      Throws:
      IllegalArgumentException - when the converter could not handle the domain object, or could not convert the domain object in such a way that the resulting domain object is adaptable to an instance of aCanAdaptToClass afterwards.
      TLcdOutOfBoundsException - when restoring the state for the target model failed because the coordinates could not be properly converted from the source ILcdModelReference to the target ILcdModelReference.