Uploaded image for project: 'Causeway'
  1. Causeway
  2. CAUSEWAY-1897

applib: the new ObjectContracts needs backward compatibility

    XMLWordPrintableJSON

Details

    • Improvement
    • Status: Closed
    • Major
    • Resolution: Resolved
    • None
    • 2.0.0-M1
    • Core
    • None

    Description

      ObjectContracts seems to be heavily used with the incode platform.

      To ease migration, I've provided functional backward compatibility.

      However the old methods are now marked for deprecation!

       

       


      Isis until 1.16. uses reflection on every of these 4 calls ...

      public class ApplicationPermission implements Comparable<ApplicationPermission> {
      
      	...
      
          private final static String propertyNames = "role, featureType, featureFqn, mode";
      
          @Override
          public int compareTo(final ApplicationPermission other) {
              return ObjectContracts.compare(this, other, propertyNames);
          }
      
          @Override
          public boolean equals(final Object obj) {
              return ObjectContracts.equals(this, obj, propertyNames);
          }
      
          @Override
          public int hashCode() {
              return ObjectContracts.hashCode(this, propertyNames);
          }
      
          @Override
          public String toString() {
              return ObjectContracts.toString(this, propertyNames);
          }
      	
      	...
      	
      }
      

      Isis since 2.0.0 provides an optimized variant now: reflection is used only once at class initialization ...

      public class ApplicationPermission implements Comparable<ApplicationPermission> {
      
      	...
      
          private final static String propertyNames = "role, featureType, featureFqn, mode";
          
          private final static ObjectContract<ApplicationPermission> contract 
          	= ObjectContracts.parse(ApplicationPermission.class, propertyNames);
      
          @Override
          public int compareTo(final ApplicationPermission other) {
              return contract.compare(this, other);
          }
      
          @Override
          public boolean equals(final Object other) {
              return contract.equals(this, other);
          }
      
          @Override
          public int hashCode() {
          	return contract.hashCode(this);
          }
      
          @Override
          public String toString() {
              return contract.toString(this);
          }
      	
      	...
      	
      }
      

      However the recommended way to use 'object contracts' now is to no longer resort to reflection.

      Parsing as shown above can be replaced by static method handles to enforce that properties actually exist (at compile-time)

      private final static ObjectContract<ApplicationPermission> contract	= 
      	ObjectContracts.contract(ApplicationPermission.class)
      	.thenUse("role", ApplicationPermission::getRole)
      	.thenUse("featureType", ApplicationPermission::getFeatureType)
      	.thenUse("featureFqn", ApplicationPermission::getFeatureFqn)
      	.thenUse("mode", ApplicationPermission::getMode);
      

      If different then natural order comparators are needed then resort to Java's Comparator composition ...

      private final static ObjectContract<ApplicationPermission> contract	= 
      	ObjectContracts.contract(ApplicationPermission.class)
      	.thenUse("role", ApplicationPermission::getRole, Comparator.naturalOrder())
      	.thenUse("featureType", ApplicationPermission::getFeatureType, Comparator.reverseOrder())
      	.thenUse("featureFqn", ApplicationPermission::getFeatureFqn, Comparator.nullsFirst(Comparator.naturalOrder()))
      	.thenUse("mode", ApplicationPermission::getMode, Comparator.nullsLast(Comparator.naturalOrder()));	
      

      Attachments

        Activity

          People

            hobrom Andi Huber
            hobrom Andi Huber
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: