00001 /* 00002 * SafeComparator.hpp 00003 * 00004 * Copyright (c) 2000, 2011, 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 00073 // ----- Comparator interface ------------------------------------------- 00074 00075 public: 00076 /** 00077 * Compares its two arguments for order. Returns a negative integer, 00078 * zero, or a positive integer as the first argument is less than, equal 00079 * to, or greater than the second. Null values are evaluated as "less 00080 * then" any non-null value. If the wrapped comparator is not specified, 00081 * all non-null values must implement the Comparable interface. 00082 * 00083 * @param vO1 the first object to be compared 00084 * @param vO2 the second object to be compared 00085 * 00086 * @return a negative integer, zero, or a positive integer as the first 00087 * argument is less than, equal to, or greater than the second 00088 * 00089 * @throws ClassCastException if the arguments' types prevent them from 00090 * being compared by this Comparator. 00091 */ 00092 virtual int32_t compare(Object::View vO1, Object::View vO2) const; 00093 00094 00095 // ----- QueryMap interface --------------------------------------------- 00096 00097 public: 00098 /** 00099 * Compare two entries using the underlying comparator. If the wrapped 00100 * comparator does not implement the QueryMapComparator interface, revert 00101 * to the entry values comparison. 00102 */ 00103 virtual int32_t compareEntries(QueryMap::Entry::View vEntry1, 00104 QueryMap::Entry::View vEntry2) const; 00105 00106 00107 // ----- EntryAwareComparator interface --------------------------------- 00108 00109 public: 00110 /** 00111 * {@inheritDoc} 00112 */ 00113 virtual bool isKeyComparator() const; 00114 00115 /** 00116 * Check whether the specified comparator expects to compare keys 00117 * or values. 00118 * 00119 * @param vComparator a Comparator to check 00120 * 00121 * @return true if the comparator expects keys; false otherwise 00122 */ 00123 static bool isKeyComparator(Comparator::View vComparator); 00124 00125 00126 // ----- PortableObject interface --------------------------------------- 00127 00128 public: 00129 /** 00130 * {@inheritDoc} 00131 */ 00132 virtual void readExternal(PofReader::Handle hIn); 00133 00134 /** 00135 * {@inheritDoc} 00136 */ 00137 virtual void writeExternal(PofWriter::Handle hOut) const; 00138 00139 00140 // ----- Object interface ----------------------------------------------- 00141 00142 public: 00143 /** 00144 * {@inheritDoc} 00145 */ 00146 virtual bool equals(Object::View v) const; 00147 00148 /** 00149 * {@inheritDoc} 00150 */ 00151 virtual size32_t hashCode() const; 00152 00153 /** 00154 * {@inheritDoc} 00155 */ 00156 virtual void toStream(std::ostream& out) const; 00157 00158 00159 // ----- data member accessors ------------------------------------------ 00160 00161 public: 00162 /** 00163 * Obtain the wrapped Comparator. 00164 * 00165 * @return the wrapped Comparator 00166 */ 00167 virtual Comparator::View getComparator() const; 00168 00169 00170 // ----- Helpers -------------------------------------------------------- 00171 00172 public: 00173 /** 00174 * Compares its two arguments for order. Returns a negative integer, 00175 * zero, or a positive integer as the first argument is less than, equal 00176 * to, or greater than the second. Null values are evaluated as 00177 * "less then" any non-null value. Non-null values must implement the 00178 * coherence::util::Comparable interface. 00179 * 00180 * @param vComparator a comparator to use for the comparison (optional) 00181 * @param vO1 the first object to be compared 00182 * @param vO2 the second object to be compared 00183 * 00184 * @return a negative integer, zero, or a positive integer as the first 00185 * argument is less than, equal to, or greater than the second 00186 * 00187 * @throws ClassCastException if the arguments are not Comparable 00188 */ 00189 static int32_t compareSafe(Comparator::View vComparator, 00190 Object::View vO1, Object::View vO2); 00191 00192 00193 // ----- constants ------------------------------------------------------ 00194 00195 public: 00196 /** 00197 * An instance of the SafeComparator. 00198 */ 00199 static SafeComparator::Handle getInstance(); 00200 00201 00202 // ----- data members --------------------------------------------------- 00203 00204 protected: 00205 /** 00206 * The wrapped Comparator. Could be null. 00207 */ 00208 MemberView<Comparator> m_vComparator; 00209 }; 00210 00211 COH_CLOSE_NAMESPACE3 00212 00213 #endif // COH_SAFE_COMPARATOR_HPP