[ 
https://issues.apache.org/jira/browse/ISIS-1897?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16388013#comment-16388013
 ] 

Dan Haywood commented on ISIS-1897:
-----------------------------------

At a quick glance, looks good.  This info needs to go into 
[http://isis.apache.org/guides/rgcms/rgcms.html#_rgcms_classes_utility_ObjectContracts]

 

> applib: the new ObjectContracts needs backward compatibility
> ------------------------------------------------------------
>
>                 Key: ISIS-1897
>                 URL: https://issues.apache.org/jira/browse/ISIS-1897
>             Project: Isis
>          Issue Type: Improvement
>          Components: Core
>            Reporter: Andi Huber
>            Assignee: Andi Huber
>            Priority: Major
>             Fix For: 2.0.0-M1
>
>
> 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 ...
> {code:java}
> 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);
>     }
>       
>       ...
>       
> }
> {code}
> ----
> Isis since 2.0.0 provides an optimized variant now: reflection is used only 
> once at class initialization ...
> {code:java}
> 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);
>     }
>       
>       ...
>       
> }
> {code}
> ----
> 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)
> {code:java}
> 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);
> {code}
> If different then natural order comparators are needed then resort to Java's 
> Comparator composition ...
> {code:java}
> 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()));      
> {code}



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to