00001 /* 00002 * Copyright (c) 2000, 2020, Oracle and/or its affiliates. 00003 * 00004 * Licensed under the Universal Permissive License v 1.0 as shown at 00005 * http://oss.oracle.com/licenses/upl. 00006 */ 00007 #ifndef COH_CHAINED_COMPARATOR_HPP 00008 #define COH_CHAINED_COMPARATOR_HPP 00009 00010 #include "coherence/lang.ns" 00011 00012 #include "coherence/io/pof/PofReader.hpp" 00013 #include "coherence/io/pof/PofWriter.hpp" 00014 #include "coherence/io/pof/PortableObject.hpp" 00015 #include "coherence/util/Comparator.hpp" 00016 #include "coherence/util/QueryMap.hpp" 00017 #include "coherence/util/comparator/EntryAwareComparator.hpp" 00018 #include "coherence/util/comparator/QueryMapComparator.hpp" 00019 00020 COH_OPEN_NAMESPACE3(coherence,util,comparator) 00021 00022 using coherence::io::pof::PofReader; 00023 using coherence::io::pof::PofWriter; 00024 using coherence::io::pof::PortableObject; 00025 00026 /** 00027 * Composite comparator implementation based on a collection of comparators. 00028 * The comparators in the array assumed to be sorted according to their 00029 * priorities; only in a case when the n-th comparator cannot determine the 00030 * order of the passed objects: 00031 * <pre> 00032 * vaComparator[n]->compare(o1, o2) == 0 00033 * </pre> 00034 * the (n+1)-th comparator will be applied to calculate the value. 00035 * 00036 * @author gm 2008.08.04 00037 */ 00038 class COH_EXPORT ChainedComparator 00039 : public class_spec<ChainedComparator, 00040 extends<Object>, 00041 implements<Comparator, 00042 QueryMapComparator, 00043 EntryAwareComparator, 00044 PortableObject> > 00045 { 00046 friend class factory<ChainedComparator>; 00047 00048 // ----- constructors/destructor ---------------------------------------- 00049 00050 protected: 00051 /** 00052 * Default constructor (necessary for the PortableObject interface). 00053 */ 00054 ChainedComparator(); 00055 00056 /** 00057 * Construct a ChainedComparator. 00058 * 00059 * @param vaComparator the comparator array 00060 */ 00061 ChainedComparator(ObjectArray::View vaComparator); 00062 00063 00064 // ----- Comparator interface ------------------------------------------- 00065 00066 public: 00067 /** 00068 * Compares its two arguments for order. Returns a negative integer, 00069 * zero, or a positive integer as the first argument is less than, equal 00070 * to, or greater than the second. 00071 * 00072 * @param vO1 the first object to be compared 00073 * @param vO2 the second object to be compared 00074 * 00075 * @return a negative integer, zero, or a positive integer as the first 00076 * argument is less than, equal to, or greater than the second 00077 * 00078 * @throws ClassCastException if the arguments' types prevent them from 00079 * being compared by this Comparator. 00080 */ 00081 virtual int32_t compare(Object::View vO1, Object::View vO2) const; 00082 00083 00084 // ----- QueryMap interface --------------------------------------------- 00085 00086 public: 00087 /** 00088 * Compare two entries using the underlying comparator.This implementation 00089 * simply passes on this invocation to the wrapped 00090 * Comparator objects if they too implement this interface, or invokes 00091 * their default compare method passing the values extracted from the 00092 * passed entries. 00093 */ 00094 virtual int32_t compareEntries(QueryMap::Entry::View vEntry1, 00095 QueryMap::Entry::View vEntry2) const; 00096 00097 00098 // ----- EntryAwareComparator interface --------------------------------- 00099 00100 public: 00101 /** 00102 * {@inheritDoc} 00103 */ 00104 virtual bool isKeyComparator() const; 00105 00106 /** 00107 * Check whether the specified comparator expects to compare keys 00108 * or values. 00109 * 00110 * @param vComparator a Comparator to check 00111 * 00112 * @return true if the comparator expects keys; false otherwise 00113 */ 00114 static bool isKeyComparator(Comparator::View vComparator); 00115 00116 00117 // ----- PortableObject interface --------------------------------------- 00118 00119 public: 00120 /** 00121 * {@inheritDoc} 00122 */ 00123 virtual void readExternal(PofReader::Handle hIn); 00124 00125 /** 00126 * {@inheritDoc} 00127 */ 00128 virtual void writeExternal(PofWriter::Handle hOut) const; 00129 00130 // ----- Object interface ----------------------------------------------- 00131 00132 public: 00133 /** 00134 * {@inheritDoc} 00135 */ 00136 virtual bool equals(Object::View v) const; 00137 00138 // ----- data member accessors ------------------------------------------ 00139 00140 public: 00141 /** 00142 * Obtain the underlying Comparator array. 00143 * 00144 * @return the Comparator array 00145 */ 00146 virtual ObjectArray::View getComparators() const; 00147 00148 // ----- constants ------------------------------------------------------ 00149 00150 public: 00151 /** 00152 * An instance of the ChainedComparator. 00153 */ 00154 static ChainedComparator::Handle getInstance(); 00155 00156 00157 // ----- data members --------------------------------------------------- 00158 00159 protected: 00160 /** 00161 * The wrapped Comparator array. Could be null. 00162 */ 00163 FinalView<ObjectArray> f_vaComparator; 00164 }; 00165 00166 COH_CLOSE_NAMESPACE3 00167 00168 #endif // COH_CHAINED_COMPARATOR_HPP