You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by ah...@apache.org on 2018/02/28 22:20:46 UTC

[isis] branch master updated: ISIS-1841 moving deprecated classes+tests to applib-legacy

This is an automated email from the ASF dual-hosted git repository.

ahuber pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/isis.git


The following commit(s) were added to refs/heads/master by this push:
     new 82912e9  ISIS-1841 moving deprecated classes+tests to applib-legacy
82912e9 is described below

commit 82912e9a8986291389e0a81c6d4e97c10dc688c2
Author: Andi Huber <ah...@apache.org>
AuthorDate: Wed Feb 28 23:20:42 2018 +0100

    ISIS-1841 moving deprecated classes+tests to applib-legacy
---
 .../java/org/apache/isis/applib/util/Clause.java   |   0
 .../isis/applib/util/ObjectContractsLegacy.java    | 286 +++++++++++++++++++++
 .../java/org/apache/isis/applib/util/Invoice.java  |   4 +-
 .../org/apache/isis/applib/util/InvoiceItem.java   |   4 +-
 .../apache/isis/applib/util/NumberedEvaluator.java |   2 +-
 .../applib/util/ObjectContractsTest_compareTo.java |   0
 .../applib/util/ObjectContractsTest_equals.java    |  14 +-
 .../ObjectContractsTest_equals_and_hashCode.java   |   8 +-
 .../applib/util/ObjectContractsTest_toString.java  |   8 +-
 9 files changed, 306 insertions(+), 20 deletions(-)

diff --git a/core/applib/src/main/java/org/apache/isis/applib/util/Clause.java b/core/applib-legacy/src/main/java/org/apache/isis/applib/util/Clause.java
similarity index 100%
rename from core/applib/src/main/java/org/apache/isis/applib/util/Clause.java
rename to core/applib-legacy/src/main/java/org/apache/isis/applib/util/Clause.java
diff --git a/core/applib-legacy/src/main/java/org/apache/isis/applib/util/ObjectContractsLegacy.java b/core/applib-legacy/src/main/java/org/apache/isis/applib/util/ObjectContractsLegacy.java
new file mode 100644
index 0000000..f07f2d9
--- /dev/null
+++ b/core/applib-legacy/src/main/java/org/apache/isis/applib/util/ObjectContractsLegacy.java
@@ -0,0 +1,286 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one or more
+ *  contributor license agreements.  See the NOTICE file distributed with
+ *  this work for additional information regarding copyright ownership.
+ *  The ASF licenses this file to You under the Apache License, Version 2.0
+ *  (the "License"); you may not use this file except in compliance with
+ *  the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.isis.applib.util;
+
+import java.util.Arrays;
+import java.util.Comparator;
+import java.util.List;
+import java.util.stream.Collectors;
+
+import org.apache.isis.applib.internal.base._Casts;
+import org.apache.isis.applib.internal.base._NullSafe;
+import org.apache.isis.applib.internal.base._Strings;
+import org.apache.isis.applib.internal.collections._Lists;
+
+import com.google.common.base.Function;
+import com.google.common.base.Objects;
+import com.google.common.base.Objects.ToStringHelper;
+import com.google.common.collect.ComparisonChain;
+import com.google.common.collect.Ordering;
+
+public class ObjectContractsLegacy {
+
+    //region > compare
+
+    /**
+     * Evaluates which of p and q is first.
+     *
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     * @param propertyNames - the property name or names, CSV format.  If multiple properties, use the {@link #compare(Object, Object, String...) varargs} overloaded version of this method.
+     */
+    @Deprecated
+    public static <T> int compare(final T p, final T q, final String propertyNames) {
+        final Iterable<String> propertyNamesIter = csvToIterable(propertyNames);
+        return compare(p, q, propertyNamesIter);
+    }
+
+    /**
+     * Evaluates which of p and q is first.
+     *
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     */
+    @Deprecated
+    public static <T> int compare(final T p, final T q, final String... propertyNames) {
+        final Iterable<String> propertyNamesIter = varargsToIterable(propertyNames);
+        return compare(p, q, propertyNamesIter);
+    }
+
+    private static <T> int compare(final T p, final T q, final Iterable<String> propertyNamesIter) {
+        if(p == null) { return -1;}
+        if(q == null) { return +1;}
+        if(p.getClass() != q.getClass()) {
+            // just sort on the class type
+            return Ordering.natural().onResultOf((Function<Object, String>) o -> o.getClass().getSimpleName()).compare(p, q);
+        }
+
+        final Iterable<Clause> clauses = clausesFor(propertyNamesIter);
+        ComparisonChain chain = ComparisonChain.start();
+        for (final Clause clause : clauses) {
+            final Comparable<T> propertyValueOfP = _Casts.uncheckedCast(clause.getValueOf(p));
+            final Comparable<T> propertyValueOfQ = _Casts.uncheckedCast(clause.getValueOf(q));
+            chain = chain.compare(propertyValueOfP, propertyValueOfQ, clause.getDirection().getOrdering());
+        }
+        return chain.result();
+    }
+    //endregion
+
+    //region > compareBy
+    /**
+     * Returns a {@link Comparator} to evaluate objects by their property name(s).
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     * @param propertyNames - the property name or names, CSV format.  If multiple properties, use the {@link #compareBy(String...)} varargs} overloaded version of this method.
+     */
+    @Deprecated
+    public static <T> Comparator<T> compareBy(final String propertyNames){
+        return (p, q) -> compare(p, q, propertyNames);
+    }
+    /**
+     * Returns a {@link Comparator} to evaluate objects by their property name(s).
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     */
+    @Deprecated
+    public static <T> Comparator<T> compareBy(final String... propertyNames){
+        return (p, q) -> compare(p, q, propertyNames);
+    }
+    //endregion
+
+    //region > toString
+
+    /**
+     * Returns a string representation of the object consisting of the specified property name(s).
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     * @param propertyNames - the property name or names, CSV format.  If multiple properties, use the {@link #toString(Object, String...)} varargs} overloaded version of this method.
+     */
+    @Deprecated
+    public static String toString(Object p, String propertyNames) {
+        return new ObjectContractsLegacy().toStringOf(p, propertyNames);
+    }
+    /**
+     * Returns a string representation of the object consisting of the specified property name(s).
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     */
+    @Deprecated
+    public static String toString(Object p, String... propertyNames) {
+        return new ObjectContractsLegacy().toStringOf(p, propertyNames);
+    }
+    //endregion
+
+    //region > hashCode
+    /**
+     * Returns the hashCode for the object using the specified property name(s).
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     * @param propertyNames - the property name or names, CSV format.  If multiple properties, use the {@link #hashCode(Object, String...)} varargs} overloaded version of this method.
+     */
+    @Deprecated
+    public static int hashCode(Object obj, String propertyNames) {
+        final Iterable<String> propertyNamesIter = csvToIterable(propertyNames);
+        return hashCode(obj, propertyNamesIter);
+    }
+
+    /**
+     * Returns the hashCode for the object using the specified property name(s).
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     */
+    @Deprecated
+    public static int hashCode(Object obj, String... propertyNames) {
+        final Iterable<String> propertyNamesIter = varargsToIterable(propertyNames);
+        return hashCode(obj, propertyNamesIter);
+    }
+
+    private static int hashCode(final Object obj, final Iterable<String> propertyNamesIter) {
+        final List<Object> propertyValues = _Lists.newArrayList();
+        for (final Clause clause : clausesFor(propertyNamesIter)) {
+            final Object propertyValue = clause.getValueOf(obj);
+            if(propertyValue != null) {
+                propertyValues.add(propertyValue);
+            }
+        }
+        return Objects.hashCode(propertyValues.toArray());
+    }
+    //endregion
+
+    //region > equals
+
+    /**
+     * Returns whether two objects are equal, considering just the specified property name(s).
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     * @param propertyNames - the property name or names, CSV format.  If multiple properties, use the {@link #equals(Object, Object, String...)} varargs} overloaded version of this method.
+     */
+    @Deprecated
+    public static boolean equals(Object p, Object q, String propertyNames) {
+        if(p==null && q==null) {
+            return true;
+        }
+        if(p==null || q==null) {
+            return false;
+        }
+        if(p.getClass() != q.getClass()) {
+            return false;
+        }
+        final Iterable<String> propertyNamesIter = csvToIterable(propertyNames);
+        return equals(p, q, propertyNamesIter);
+    }
+
+    /**
+     * Returns whether two objects are equal, considering just the specified property name(s).
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     */
+    @Deprecated
+    public static boolean equals(Object p, Object q, String... propertyNames) {
+        if(p==null && q==null) {
+            return true;
+        }
+        if(p==null || q==null) {
+            return false;
+        }
+        if(p.getClass() != q.getClass()) {
+            return false;
+        }
+        final Iterable<String> propertyNamesIter = varargsToIterable(propertyNames);
+        return equals(p, q, propertyNamesIter);
+    }
+
+    private static boolean equals(final Object p, final Object q, final Iterable<String> propertyNamesIter) {
+        final Iterable<Clause> clauses = clausesFor(propertyNamesIter);
+        for (final Clause clause : clauses) {
+            final Object pValue = clause.getValueOf(p);
+            final Object qValue = clause.getValueOf(q);
+            if(!Objects.equal(pValue, qValue)) {
+                return false;
+            }
+        }
+        return true;
+    }
+    //endregion
+
+    //region > helpers
+    private static Iterable<Clause> clausesFor(final Iterable<String> iterable) {
+        return _NullSafe.stream(iterable)
+        		.map(Clause::parse)
+        		.collect(Collectors.toList());
+    }
+
+    private static Iterable<String> csvToIterable(final String propertyNames) {
+        return _Strings.splitThenStream(propertyNames, ",")
+        		.collect(Collectors.toList());
+    }
+
+    private static List<String> varargsToIterable(final String[] iterable) {
+        return Arrays.asList(iterable);
+    }
+    //endregion
+
+    //region > toStringOf
+
+    public interface ToStringEvaluator {
+        boolean canEvaluate(Object o);
+        String evaluate(Object o);
+    }
+    
+    private final List<ToStringEvaluator> evaluators = _Lists.newArrayList();
+
+    public ObjectContractsLegacy with(ToStringEvaluator evaluator) {
+        evaluators.add(evaluator);
+        return this;
+    }
+
+    /**
+     * Returns a string representation of two objects, considering just the specified property name(s).
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     * @param propertyNames - the property name or names, CSV format.  If multiple properties, use the {@link #toString(Object, String...)} varargs} overloaded version of this method.
+     */
+    @Deprecated
+    public String toStringOf(Object p, String propertyNames) {
+        final Iterable<String> propertyNamesIter = csvToIterable(propertyNames);
+        return toStringOf(p, propertyNamesIter);
+    }
+
+    /**
+     * Returns a string representation of two objects, considering just the specified property name(s).
+     * @deprecated - please be aware that this utility heavily uses reflection.  We don't actually intend to deprecate this method (it's useful while prototyping), but we wanted to bring this to your attention!
+     */
+    @Deprecated
+    public String toStringOf(Object p, String... propertyNames) {
+        final Iterable<String> propertyNamesIter = varargsToIterable(propertyNames);
+        return toStringOf(p, propertyNamesIter);
+    }
+
+    private String toStringOf(final Object p, final Iterable<String> propertyNamesIter) {
+        final ToStringHelper stringHelper = Objects.toStringHelper(p);
+        for (final Clause clause : clausesFor(propertyNamesIter)) {
+            stringHelper.add(clause.getPropertyName(), asString(clause, p));
+        }
+        return stringHelper.toString();
+    }
+
+    private String asString(final Clause clause, Object p) {
+        final Object value = clause.getValueOf(p);
+        if(value == null) {
+            return null;
+        }
+        for (ToStringEvaluator evaluator : evaluators) {
+            if(evaluator.canEvaluate(value)) {
+                return evaluator.evaluate(value);
+            }
+        }
+        return value.toString();
+    }
+
+    //endregion
+
+
+}
diff --git a/core/applib/src/test/java/org/apache/isis/applib/util/Invoice.java b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/Invoice.java
similarity index 89%
rename from core/applib/src/test/java/org/apache/isis/applib/util/Invoice.java
rename to core/applib-legacy/src/test/java/org/apache/isis/applib/util/Invoice.java
index edcafc9..27ca497 100644
--- a/core/applib/src/test/java/org/apache/isis/applib/util/Invoice.java
+++ b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/Invoice.java
@@ -29,10 +29,10 @@ class Invoice implements Comparable<Invoice>{
     }
     @Override
     public String toString() {
-        return ObjectContracts.toString(this, KEY_PROPERTIES);
+        return ObjectContractsLegacy.toString(this, KEY_PROPERTIES);
     }
     @Override
     public int compareTo(Invoice o) {
-        return ObjectContracts.compare(this, o, KEY_PROPERTIES);
+        return ObjectContractsLegacy.compare(this, o, KEY_PROPERTIES);
     }
 }
\ No newline at end of file
diff --git a/core/applib/src/test/java/org/apache/isis/applib/util/InvoiceItem.java b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/InvoiceItem.java
similarity index 94%
rename from core/applib/src/test/java/org/apache/isis/applib/util/InvoiceItem.java
rename to core/applib-legacy/src/test/java/org/apache/isis/applib/util/InvoiceItem.java
index ce981d1..258ae61 100644
--- a/core/applib/src/test/java/org/apache/isis/applib/util/InvoiceItem.java
+++ b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/InvoiceItem.java
@@ -64,10 +64,10 @@ class InvoiceItem implements Comparable<InvoiceItem> {
     
     @Override
     public String toString() {
-        return ObjectContracts.toString(this, KEY_PROPERTIES);
+        return ObjectContractsLegacy.toString(this, KEY_PROPERTIES);
     }
     @Override
     public int compareTo(InvoiceItem o) {
-        return ObjectContracts.compare(this, o, KEY_PROPERTIES);
+        return ObjectContractsLegacy.compare(this, o, KEY_PROPERTIES);
     }
 }
\ No newline at end of file
diff --git a/core/applib/src/test/java/org/apache/isis/applib/util/NumberedEvaluator.java b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/NumberedEvaluator.java
similarity index 93%
rename from core/applib/src/test/java/org/apache/isis/applib/util/NumberedEvaluator.java
rename to core/applib-legacy/src/test/java/org/apache/isis/applib/util/NumberedEvaluator.java
index aa2e6cd..fcb02e7 100644
--- a/core/applib/src/test/java/org/apache/isis/applib/util/NumberedEvaluator.java
+++ b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/NumberedEvaluator.java
@@ -16,7 +16,7 @@
  */
 package org.apache.isis.applib.util;
 
-import org.apache.isis.applib.util.ObjectContracts.ToStringEvaluator;
+import org.apache.isis.applib.util.ObjectContractsLegacy.ToStringEvaluator;
 
 public class NumberedEvaluator implements ToStringEvaluator {
 
diff --git a/core/applib/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_compareTo.java b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_compareTo.java
similarity index 100%
rename from core/applib/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_compareTo.java
rename to core/applib-legacy/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_compareTo.java
diff --git a/core/applib/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals.java b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals.java
similarity index 81%
rename from core/applib/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals.java
rename to core/applib-legacy/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals.java
index 5526e4f..da37a84 100644
--- a/core/applib/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals.java
+++ b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals.java
@@ -36,11 +36,11 @@ public class ObjectContractsTest_equals {
         }
         @Override
         public int hashCode() {
-            return ObjectContracts.hashCode(this, KEY_PROPERTIES);
+            return ObjectContractsLegacy.hashCode(this, KEY_PROPERTIES);
         }
         @Override
         public boolean equals(Object obj) {
-            return ObjectContracts.equals(this, obj, KEY_PROPERTIES);
+            return ObjectContractsLegacy.equals(this, obj, KEY_PROPERTIES);
         }
 
     }
@@ -64,27 +64,27 @@ public class ObjectContractsTest_equals {
 
     @Test
     public void happyCase() throws Exception {
-        assertTrue(ObjectContracts.equals(p, q, "number"));
+        assertTrue(ObjectContractsLegacy.equals(p, q, "number"));
     }
 
     @Test
     public void nullsAreEqual() throws Exception {
-        assertTrue(ObjectContracts.equals(null, null, "number"));
+        assertTrue(ObjectContractsLegacy.equals(null, null, "number"));
     }
 
     @Test
     public void notEqualDifferentValues() throws Exception {
-        assertFalse(ObjectContracts.equals(p, r, "number"));
+        assertFalse(ObjectContractsLegacy.equals(p, r, "number"));
     }
 
     @Test
     public void notEqualDifferentTypes() throws Exception {
-        assertFalse(ObjectContracts.equals(p, x, "number"));
+        assertFalse(ObjectContractsLegacy.equals(p, x, "number"));
     }
 
     @Test
     public void notEqualNull() throws Exception {
-        assertFalse(ObjectContracts.equals(p, null, "number"));
+        assertFalse(ObjectContractsLegacy.equals(p, null, "number"));
     }
 
 
diff --git a/core/applib/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals_and_hashCode.java b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals_and_hashCode.java
similarity index 93%
rename from core/applib/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals_and_hashCode.java
rename to core/applib-legacy/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals_and_hashCode.java
index 89dee00..d93092a 100644
--- a/core/applib/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals_and_hashCode.java
+++ b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_equals_and_hashCode.java
@@ -68,11 +68,11 @@ class Invoice3 {
     }
     @Override
     public int hashCode() {
-        return ObjectContracts.hashCode(this, KEY_PROPERTIES);
+        return ObjectContractsLegacy.hashCode(this, KEY_PROPERTIES);
     }
     @Override
     public boolean equals(Object obj) {
-        return ObjectContracts.equals(this, obj, KEY_PROPERTIES);
+        return ObjectContractsLegacy.equals(this, obj, KEY_PROPERTIES);
     }
     
 }
@@ -115,10 +115,10 @@ class InvoiceItem3 {
     private static final String KEY_PROPERTIES = "invoice desc, productCode, quantity";
     @Override
     public int hashCode() {
-        return ObjectContracts.hashCode(this, KEY_PROPERTIES);
+        return ObjectContractsLegacy.hashCode(this, KEY_PROPERTIES);
     }
     @Override
     public boolean equals(Object obj) {
-        return ObjectContracts.equals(this, obj, KEY_PROPERTIES);
+        return ObjectContractsLegacy.equals(this, obj, KEY_PROPERTIES);
     }
 }
diff --git a/core/applib/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_toString.java b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_toString.java
similarity index 93%
rename from core/applib/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_toString.java
rename to core/applib-legacy/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_toString.java
index 89cef32..6b2216b 100644
--- a/core/applib/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_toString.java
+++ b/core/applib-legacy/src/test/java/org/apache/isis/applib/util/ObjectContractsTest_toString.java
@@ -68,11 +68,11 @@ class Invoice2 implements Comparable<Invoice2>, Numbered {
     }
     @Override
     public String toString() {
-        return ObjectContracts.toString(this, KEY_PROPERTIES);
+        return ObjectContractsLegacy.toString(this, KEY_PROPERTIES);
     }
     @Override
     public int compareTo(Invoice2 o) {
-        return ObjectContracts.compare(this, o, KEY_PROPERTIES);
+        return ObjectContractsLegacy.compare(this, o, KEY_PROPERTIES);
     }
 }
 class InvoiceItem2 implements Comparable<InvoiceItem2> {
@@ -123,10 +123,10 @@ class InvoiceItem2 implements Comparable<InvoiceItem2> {
     
     @Override
     public String toString() {
-        return new ObjectContracts().with(new NumberedEvaluator()).toStringOf(this, KEY_PROPERTIES);
+        return new ObjectContractsLegacy().with(new NumberedEvaluator()).toStringOf(this, KEY_PROPERTIES);
     }
     @Override
     public int compareTo(InvoiceItem2 o) {
-        return ObjectContracts.compare(this, o, KEY_PROPERTIES);
+        return ObjectContractsLegacy.compare(this, o, KEY_PROPERTIES);
     }
 }
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
ahuber@apache.org.