00001 /* 00002 * SafeComparator.hpp 00003 * 00004 * Copyright (c) 2000, 2014, 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_SAFE_COMPARATOR_HPP 00017 #define COH_SAFE_COMPARATOR_HPP 00018 00019 #include "coherence/lang.ns" 00020 00021 #include "coherence/io/pof/PofReader.hpp" 00022 #include "coherence/io/pof/PofWriter.hpp" 00023 #include "coherence/io/pof/PortableObject.hpp" 00024 #include "coherence/util/Comparator.hpp" 00025 #include "coherence/util/QueryMap.hpp" 00026 #include "coherence/util/comparator/EntryAwareComparator.hpp" 00027 #include "coherence/util/comparator/QueryMapComparator.hpp" 00028 00029 COH_OPEN_NAMESPACE3(coherence,util,comparator) 00030 00031 using coherence::io::pof::PofReader; 00032 using coherence::io::pof::PofWriter; 00033 using coherence::io::pof::PortableObject; 00034 00035 00036 /** 00037 * NULL-safe delegating comparator. NULL values are evaluated as "less then" 00038 * any non-null value. If the wrapped comparator is not specified then all 00039 * non-null values must implement the coherence::util::Comparable interface. 00040 * Use SafeComparator::getInstance to obtain an instance of non-delegating 00041 * SafeComparator. 00042 * 00043 * @author djl 2008.05.05 00044 */ 00045 class COH_EXPORT SafeComparator 00046 : public class_spec<SafeComparator, 00047 extends<Object>, 00048 implements<Comparator, 00049 QueryMapComparator, 00050 EntryAwareComparator, 00051 PortableObject> > 00052 { 00053 friend class factory<SafeComparator>; 00054 00055 // ----- constructors --------------------------------------------------- 00056 00057 protected: 00058 /** 00059 * Default constructor (necessary for the PortableObject interface). 00060 */ 00061 SafeComparator(); 00062 00063 /** 00064 * Construct a SafeComparator delegating to the specified (wrapped) 00065 * comparator. 00066 * 00067 * @param vComparator Comparator object to delegate comparison of 00068 * non-null values (optional) 00069 */ 00070 SafeComparator(Comparator::View vComparator); 00071 00072 private: 00073 /** 00074 * Blocked copy constructor. 00075 */ 00076 SafeComparator(const SafeComparator&); 00077 00078 00079 // ----- Comparator interface ------------------------------------------- 00080 00081 public: 00082 /** 00083 * Compares its two arguments for order. Returns a negative integer, 00084 * zero, or a positive integer as the first argument is less than, equal 00085 * to, or greater than the second. Null values are evaluated as "less 00086 * then" any non-null value. If the wrapped comparator is not specified, 00087 * all non-null values must implement the Comparable interface. 00088 * 00089 * @param vO1 the first object to be compared 00090 * @param vO2 the second object to be compared 00091 * 00092 * @return a negative integer, zero, or a positive integer as the first 00093 * argument is less than, equal to, or greater than the second 00094 * 00095 * @throws ClassCastException if the arguments' types prevent them from 00096 * being compared by this Comparator. 00097 */ 00098 virtual int32_t compare(Object::View vO1, Object::View vO2) const; 00099 00100 00101 // ----- QueryMap interface --------------------------------------------- 00102 00103 public: 00104 /** 00105 * Compare two entries using the underlying comparator. If the wrapped 00106 * comparator does not implement the QueryMapComparator interface, revert 00107 * to the entry values comparison. 00108 */ 00109 virtual int32_t compareEntries(QueryMap::Entry::View vEntry1, 00110 QueryMap::Entry::View vEntry2) const; 00111 00112 00113 // ----- EntryAwareComparator interface --------------------------------- 00114 00115 public: 00116 /** 00117 * {@inheritDoc} 00118 */ 00119 virtual bool isKeyComparator() const; 00120 00121 /** 00122 * Check whether the specified comparator expects to compare keys 00123 * or values. 00124 * 00125 * @param vComparator a Comparator to check 00126 * 00127 * @return true if the comparator expects keys; false otherwise 00128 */ 00129 static bool isKeyComparator(Comparator::View vComparator); 00130 00131 00132 // ----- PortableObject interface --------------------------------------- 00133 00134 public: 00135 /** 00136 * {@inheritDoc} 00137 */ 00138 virtual void readExternal(PofReader::Handle hIn); 00139 00140 /** 00141 * {@inheritDoc} 00142 */ 00143 virtual void writeExternal(PofWriter::Handle hOut) const; 00144 00145 00146 // ----- Object interface ----------------------------------------------- 00147 00148 public: 00149 /** 00150 * {@inheritDoc} 00151 */ 00152 virtual bool equals(Object::View v) const; 00153 00154 /** 00155 * {@inheritDoc} 00156 */ 00157 virtual size32_t hashCode() const; 00158 00159 /** 00160 * {@inheritDoc} 00161 */ 00162 virtual void toStream(std::ostream& out) const; 00163 00164 00165 // ----- data member accessors ------------------------------------------ 00166 00167 public: 00168 /** 00169 * Obtain the wrapped Comparator. 00170 * 00171 * @return the wrapped Comparator 00172 */ 00173 virtual Comparator::View getComparator() const; 00174 00175 00176 // ----- Helpers -------------------------------------------------------- 00177 00178 public: 00179 /** 00180 * Compares its two arguments for order. Returns a negative integer, 00181 * zero, or a positive integer as the first argument is less than, equal 00182 * to, or greater than the second. Null values are evaluated as 00183 * "less then" any non-null value. Non-null values must implement the 00184 * coherence::util::Comparable interface. 00185 * 00186 * @param vComparator a comparator to use for the comparison (optional) 00187 * @param vO1 the first object to be compared 00188 * @param vO2 the second object to be compared 00189 * 00190 * @return a negative integer, zero, or a positive integer as the first 00191 * argument is less than, equal to, or greater than the second 00192 * 00193 * @throws ClassCastException if the arguments are not Comparable 00194 */ 00195 static int32_t compareSafe(Comparator::View vComparator, 00196 Object::View vO1, Object::View vO2); 00197 00198 00199 // ----- constants ------------------------------------------------------ 00200 00201 public: 00202 /** 00203 * An instance of the SafeComparator. 00204 */ 00205 static SafeComparator::Handle getInstance(); 00206 00207 00208 // ----- data members --------------------------------------------------- 00209 00210 protected: 00211 /** 00212 * The wrapped Comparator. Could be null. 00213 */ 00214 FinalView<Comparator> f_vComparator; 00215 }; 00216 00217 COH_CLOSE_NAMESPACE3 00218 00219 #endif // COH_SAFE_COMPARATOR_HPP