00001 /* 00002 * Objects.hpp 00003 * 00004 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved. 00005 * 00006 * Oracle is a registered trademarks of Oracle Corporation and/or its 00007 * affiliates. 00008 * 00009 * This software is the confidential and proprietary information of Oracle 00010 * Corporation. You shall not disclose such confidential and proprietary 00011 * information and shall use it only in accordance with the terms of the 00012 * license agreement you entered into with Oracle. 00013 * 00014 * This notice may not be removed or altered. 00015 */ 00016 #ifndef COH_OBJECTS_HPP 00017 #define COH_OBJECTS_HPP 00018 00019 #include "coherence/lang.ns" 00020 00021 #include "coherence/util/Comparator.hpp" 00022 00023 COH_OPEN_NAMESPACE2(coherence,util) 00024 00025 /** 00026 * This class consists of static NULL-safe or NULL-tolerant 00027 * utility methods for operating on Objects. 00028 * 00029 * @author phf 2015.07.10 00030 * 00031 * @since 12.2.1 00032 */ 00033 class COH_EXPORT Objects 00034 : public abstract_spec<Objects> 00035 { 00036 // ----- Objects interface ---------------------------------------------- 00037 00038 public: 00039 using Object::toString; 00040 00041 /** 00042 * Returns the result of calling toString on the first 00043 * argument if the first argument is not NULL and returns 00044 * the second argument otherwise. 00045 * 00046 * @param v an Object 00047 * @param vsNullDefault string to return if the first argument is 00048 * NULL 00049 * 00050 * @return the result of calling toString on the first 00051 * argument if it is not NULL and the second argument 00052 * otherwise. 00053 * 00054 * @see Object#toString(Object) 00055 */ 00056 static String::View toString(Object::View v, String::View vsNullDefault); 00057 00058 /** 00059 * Returns 0 if the arguments are identical and 00060 * vC->compare(vA, vB) otherwise. 00061 * Consequently, if both arguments are NULL, 0 00062 * is returned. 00063 * 00064 * Note that if one of the arguments is NULL, a 00065 * NullPointerException may or may not be thrown depending on 00066 * what ordering policy, if any, the Comparator 00067 * chooses to have for NULL values. 00068 * 00069 * @param vA an Object 00070 * @param vB an Object to be compared with vA 00071 * @param vC the Comparator to compare the first two arguments 00072 * 00073 * @return 0 if the arguments are identical and 00074 * c->compare(vA, vB) otherwise. 00075 * 00076 * @see Comparable 00077 * @see Comparator 00078 */ 00079 static int32_t compare(Object::View vA, Object::View vB, Comparator::View vC); 00080 00081 /** 00082 * Checks that the specified object reference is not NULL. This 00083 * method is designed primarily for doing parameter validation in methods 00084 * and constructors, as demonstrated below: 00085 * <blockquote><pre> 00086 * protected Foo(Bar::View vBar) { 00087 * this.vBar = Objects::requireNonNull(vBar); 00088 * } 00089 * </pre></blockquote> 00090 * 00091 * @param oh the Object reference to check for nullity 00092 * 00093 * @return oh if not NULL 00094 * 00095 * @throws NullPointerException if oh is NULL 00096 */ 00097 static Object::Holder requireNonNull(Object::Holder oh); 00098 00099 /** 00100 * Checks that the specified Object reference is not NULL and 00101 * throws a customized NullPointerException if it is. This method 00102 * is designed primarily for doing parameter validation in methods and 00103 * constructors with multiple parameters, as demonstrated below: 00104 * <blockquote><pre> 00105 * protected Foo(Bar::View vBar, Baz::View vBaz) { 00106 * this.vBar = Objects::requireNonNull(vBar, "vBar must not be null"); 00107 * this.vBaz = Objects::requireNonNull(vBaz, "vBaz must not be null"); 00108 * } 00109 * </pre></blockquote> 00110 * 00111 * @param oh the Object reference to check for nullity 00112 * @param vsMessage detail message to be used in the event that a 00113 * NullPointerException is thrown 00114 * 00115 * @return oh if not NULL 00116 * 00117 * @throws NullPointerException if oh is NULL 00118 */ 00119 static Object::Holder requireNonNull(Object::Holder oh, String::View vsMessage); 00120 00121 /** 00122 * Returns true if the provided reference is NULL otherwise 00123 * returns false. 00124 * 00125 * @param v an Object to be checked against NULL 00126 * 00127 * @return true if the provided reference is NULL otherwise false 00128 */ 00129 static bool isNull(Object::View v); 00130 00131 /** 00132 * Returns true if the provided reference is non-NULL 00133 * otherwise returns false. 00134 * 00135 * @param v an Object to be checked against NULL 00136 * 00137 * @return true if the provided reference is non-NULL otherwise false 00138 */ 00139 static bool nonNull(Object::View v); 00140 }; 00141 00142 COH_CLOSE_NAMESPACE2 00143 00144 #endif // COH_OBJECTS_HPP