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/03/05 09:19:39 UTC

[isis] 02/02: ISIS-1897 implement ObjectContracts

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

commit 5d736584e24c14006cc9882112317e818c887cd9
Author: Andi Huber <ah...@apache.org>
AuthorDate: Mon Mar 5 10:18:19 2018 +0100

    ISIS-1897 implement ObjectContracts
---
 .../isis/applib/util/ObjectContract_Impl.java      | 68 ++++++++++++++++++++++
 .../isis/applib/util/ObjectContract_Parser.java    | 44 ++++++++++++++
 .../apache/isis/applib/util/ObjectContracts.java   | 54 +++++++++++------
 3 files changed, 149 insertions(+), 17 deletions(-)

diff --git a/core/applib/src/main/java/org/apache/isis/applib/util/ObjectContract_Impl.java b/core/applib/src/main/java/org/apache/isis/applib/util/ObjectContract_Impl.java
new file mode 100644
index 0000000..db6391f
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/util/ObjectContract_Impl.java
@@ -0,0 +1,68 @@
+/*
+ *  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 org.apache.isis.applib.internal.exceptions._Exceptions;
+import org.apache.isis.applib.util.ObjectContracts.ObjectContract;
+import org.apache.isis.applib.util.ObjectContracts.ToStringEvaluator;
+
+/**
+ * Package private default implementation for ObjectContract.
+ *  
+ * @since 2.0.0
+ */
+class ObjectContract_Impl<T> implements ObjectContract<T> {
+
+	@Override
+	public int compare(Object obj, Object other) {
+		// TODO Auto-generated method stub
+		_Exceptions.throwNotImplemented();
+		return 0;
+	}
+
+	@Override
+	public boolean equals(Object obj, Object other) {
+		// TODO Auto-generated method stub
+		_Exceptions.throwNotImplemented();
+		return false;
+	}
+
+	@Override
+	public int hashCode(Object obj) {
+		// TODO Auto-generated method stub
+		_Exceptions.throwNotImplemented();
+		return 0;
+	}
+
+	@Override
+	public String toString(Object obj) {
+		// TODO Auto-generated method stub
+		_Exceptions.throwNotImplemented();
+		return null;
+	}
+
+	@Override
+	public ObjectContract<T> withToStringEvaluators(ToStringEvaluator... evaluators) {
+		// TODO Auto-generated method stub
+		_Exceptions.throwNotImplemented();
+		return null;
+	}
+
+}
diff --git a/core/applib/src/main/java/org/apache/isis/applib/util/ObjectContract_Parser.java b/core/applib/src/main/java/org/apache/isis/applib/util/ObjectContract_Parser.java
new file mode 100644
index 0000000..d9555c4
--- /dev/null
+++ b/core/applib/src/main/java/org/apache/isis/applib/util/ObjectContract_Parser.java
@@ -0,0 +1,44 @@
+/*
+ *  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 org.apache.isis.applib.internal.exceptions._Exceptions;
+import org.apache.isis.applib.util.ObjectContracts.ObjectContract;
+
+/**
+ * Package private parser for ObjectContract.<br/><br/>
+ *  
+ * Not public API! Use ObjectContracts.parse(String) instead.
+ * 
+ * 
+ * @since 2.0.0
+ */
+class ObjectContract_Parser<T> {
+
+	public static <T> ObjectContract<T> parse(Class<T> target, String propertyNames) {
+		final ObjectContract_Impl<T> contract = new ObjectContract_Impl<>();
+		
+		//TODO
+		_Exceptions.throwNotImplemented();
+		
+		return contract;
+	}
+
+}
diff --git a/core/applib/src/main/java/org/apache/isis/applib/util/ObjectContracts.java b/core/applib/src/main/java/org/apache/isis/applib/util/ObjectContracts.java
index 2f95aa0..aa588f3 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/util/ObjectContracts.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/util/ObjectContracts.java
@@ -18,9 +18,10 @@
  */
 package org.apache.isis.applib.util;
 
+import java.util.Objects;
 import java.util.function.Function;
 
-import org.apache.isis.applib.internal.exceptions._Exceptions;
+import org.apache.isis.applib.internal.base._Casts;
 
 /**
  * Provides fluent composition for Objects' equals, hashCode and toString.
@@ -86,6 +87,13 @@ public final class ObjectContracts {
         String evaluate(Object o);
     }
 	
+    /**
+     * WARNING Possible misuse because of forgetting respectively the last method 
+     * argument with {@code equals}, [@code hashCode} and {@code toString}!
+     *  
+     * @since 2.0.0
+     * @param <T>
+     */
 	public static interface ObjectContract<T> {
 
 		public int compare(T obj, T other);
@@ -98,40 +106,52 @@ public final class ObjectContracts {
 		
 	    // -- TO STRING EVALUATION
 
+		/**
+		 * True 'wither' (each call returns a new instance of ObjectContract)!
+		 * @param evaluators
+		 * @return contract with ToStringEvaluator(s) to apply to properties when 
+		 * processing the toString algorithm.
+		 */
 		public ObjectContract<T> withToStringEvaluators(ToStringEvaluator ... evaluators);
 		
 	}
 	
 	public static <T> ObjectContract<T> parse(Class<T> target, String propertyNames) {
-		// TODO Auto-generated method stub
-		throw _Exceptions.notImplemented();
+		return ObjectContract_Parser.parse(target, propertyNames);
 	}
 
 	// -- BACKWARDS COMPATIBILITY
 	
-	@Deprecated
-	public static String toString(Object obj, String propertyNames) {
+	@Deprecated // uses reflection on each call
+	public static <T> String toString(T obj, String propertyNames) {
+		Objects.requireNonNull(obj, "obj required, otherwise undecidable");
 		
-		// TODO Auto-generated method stub
-		throw _Exceptions.notImplemented();
+		return parse(_Casts.uncheckedCast(obj.getClass()), propertyNames)
+				.toString(obj);
 	}
 	
-	@Deprecated
-	public static boolean equals(Object obj, Object other, String propertyNames) {
-		// TODO Auto-generated method stub
-		throw _Exceptions.notImplemented();
+	@Deprecated // uses reflection on each call
+	public static <T> boolean equals(T obj, Object other, String propertyNames) {
+		Objects.requireNonNull(obj, "obj required, otherwise undecidable"); 
+		
+		return parse(_Casts.uncheckedCast(obj.getClass()), propertyNames)
+				.equals(obj, other);
 	}
 	
-	@Deprecated
+	@Deprecated // uses reflection on each call
 	public static int hashCode(Object obj, String propertyNames) {
-		// TODO Auto-generated method stub
-		throw _Exceptions.notImplemented();
+		Objects.requireNonNull(obj, "obj required, otherwise undecidable");
+
+		return parse(_Casts.uncheckedCast(obj.getClass()), propertyNames)
+				.hashCode(obj);
 	}
 
-	@Deprecated
+	@Deprecated // uses reflection on each call
 	public static <T> int compare(T obj, T other, String propertyNames) {
-		// TODO Auto-generated method stub
-		throw _Exceptions.notImplemented();
+		Objects.requireNonNull(obj, "obj required, otherwise undecidable");
+
+		return parse(_Casts.uncheckedCast(obj.getClass()), propertyNames)
+				.compare(obj, other);
 	}
 
 	

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