1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 3*cdf0e10cSrcweir * 4*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 5*cdf0e10cSrcweir * 6*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 7*cdf0e10cSrcweir * 8*cdf0e10cSrcweir * This file is part of OpenOffice.org. 9*cdf0e10cSrcweir * 10*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 11*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 12*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 13*cdf0e10cSrcweir * 14*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 15*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 16*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 18*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 19*cdf0e10cSrcweir * 20*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 21*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 22*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 23*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 24*cdf0e10cSrcweir * 25*cdf0e10cSrcweir ************************************************************************/ 26*cdf0e10cSrcweir 27*cdf0e10cSrcweir #include "precompiled_comphelper.hxx" 28*cdf0e10cSrcweir 29*cdf0e10cSrcweir #include "comphelper/anycompare.hxx" 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir /** === begin UNO includes === **/ 32*cdf0e10cSrcweir #include <com/sun/star/util/Date.hpp> 33*cdf0e10cSrcweir #include <com/sun/star/util/Time.hpp> 34*cdf0e10cSrcweir #include <com/sun/star/util/DateTime.hpp> 35*cdf0e10cSrcweir /** === end UNO includes === **/ 36*cdf0e10cSrcweir 37*cdf0e10cSrcweir //...................................................................................................................... 38*cdf0e10cSrcweir namespace comphelper 39*cdf0e10cSrcweir { 40*cdf0e10cSrcweir //...................................................................................................................... 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir /** === begin UNO using === **/ 43*cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 44*cdf0e10cSrcweir using ::com::sun::star::uno::XInterface; 45*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY; 46*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 47*cdf0e10cSrcweir using ::com::sun::star::uno::UNO_SET_THROW; 48*cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 49*cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 50*cdf0e10cSrcweir using ::com::sun::star::uno::Any; 51*cdf0e10cSrcweir using ::com::sun::star::uno::makeAny; 52*cdf0e10cSrcweir using ::com::sun::star::uno::Sequence; 53*cdf0e10cSrcweir using ::com::sun::star::uno::Type; 54*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_CHAR; 55*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_BOOLEAN; 56*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_BYTE; 57*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_SHORT; 58*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT; 59*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_LONG; 60*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_UNSIGNED_LONG; 61*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_HYPER; 62*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER; 63*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_FLOAT; 64*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_DOUBLE; 65*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_STRING; 66*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_TYPE; 67*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_ENUM; 68*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_INTERFACE; 69*cdf0e10cSrcweir using ::com::sun::star::uno::TypeClass_STRUCT; 70*cdf0e10cSrcweir using ::com::sun::star::i18n::XCollator; 71*cdf0e10cSrcweir using ::com::sun::star::util::Date; 72*cdf0e10cSrcweir using ::com::sun::star::util::Time; 73*cdf0e10cSrcweir using ::com::sun::star::util::DateTime; 74*cdf0e10cSrcweir /** === end UNO using === **/ 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir //================================================================================================================== 77*cdf0e10cSrcweir //= DatePredicateLess 78*cdf0e10cSrcweir //================================================================================================================== 79*cdf0e10cSrcweir class DatePredicateLess : public IKeyPredicateLess 80*cdf0e10cSrcweir { 81*cdf0e10cSrcweir public: 82*cdf0e10cSrcweir virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const 83*cdf0e10cSrcweir { 84*cdf0e10cSrcweir Date lhs, rhs; 85*cdf0e10cSrcweir if ( !( _lhs >>= lhs ) 86*cdf0e10cSrcweir || !( _rhs >>= rhs ) 87*cdf0e10cSrcweir ) 88*cdf0e10cSrcweir throw ::com::sun::star::lang::IllegalArgumentException(); 89*cdf0e10cSrcweir 90*cdf0e10cSrcweir if ( lhs.Year < rhs.Year ) 91*cdf0e10cSrcweir return true; 92*cdf0e10cSrcweir if ( lhs.Year > rhs.Year ) 93*cdf0e10cSrcweir return false; 94*cdf0e10cSrcweir 95*cdf0e10cSrcweir if ( lhs.Month < rhs.Month ) 96*cdf0e10cSrcweir return true; 97*cdf0e10cSrcweir if ( lhs.Month > rhs.Month ) 98*cdf0e10cSrcweir return false; 99*cdf0e10cSrcweir 100*cdf0e10cSrcweir if ( lhs.Day < rhs.Day ) 101*cdf0e10cSrcweir return true; 102*cdf0e10cSrcweir return false; 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir }; 105*cdf0e10cSrcweir 106*cdf0e10cSrcweir //================================================================================================================== 107*cdf0e10cSrcweir //= TimePredicateLess 108*cdf0e10cSrcweir //================================================================================================================== 109*cdf0e10cSrcweir class TimePredicateLess : public IKeyPredicateLess 110*cdf0e10cSrcweir { 111*cdf0e10cSrcweir public: 112*cdf0e10cSrcweir virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const 113*cdf0e10cSrcweir { 114*cdf0e10cSrcweir Time lhs, rhs; 115*cdf0e10cSrcweir if ( !( _lhs >>= lhs ) 116*cdf0e10cSrcweir || !( _rhs >>= rhs ) 117*cdf0e10cSrcweir ) 118*cdf0e10cSrcweir throw ::com::sun::star::lang::IllegalArgumentException(); 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir if ( lhs.Hours < rhs.Hours ) 121*cdf0e10cSrcweir return true; 122*cdf0e10cSrcweir if ( lhs.Hours > rhs.Hours ) 123*cdf0e10cSrcweir return false; 124*cdf0e10cSrcweir 125*cdf0e10cSrcweir if ( lhs.Minutes < rhs.Minutes ) 126*cdf0e10cSrcweir return true; 127*cdf0e10cSrcweir if ( lhs.Minutes > rhs.Minutes ) 128*cdf0e10cSrcweir return false; 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir if ( lhs.Seconds < rhs.Seconds ) 131*cdf0e10cSrcweir return true; 132*cdf0e10cSrcweir if ( lhs.Seconds > rhs.Seconds ) 133*cdf0e10cSrcweir return false; 134*cdf0e10cSrcweir 135*cdf0e10cSrcweir if ( lhs.HundredthSeconds < rhs.HundredthSeconds ) 136*cdf0e10cSrcweir return true; 137*cdf0e10cSrcweir return false; 138*cdf0e10cSrcweir } 139*cdf0e10cSrcweir }; 140*cdf0e10cSrcweir 141*cdf0e10cSrcweir //================================================================================================================== 142*cdf0e10cSrcweir //= DateTimePredicateLess 143*cdf0e10cSrcweir //================================================================================================================== 144*cdf0e10cSrcweir class DateTimePredicateLess : public IKeyPredicateLess 145*cdf0e10cSrcweir { 146*cdf0e10cSrcweir public: 147*cdf0e10cSrcweir virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const 148*cdf0e10cSrcweir { 149*cdf0e10cSrcweir DateTime lhs, rhs; 150*cdf0e10cSrcweir if ( !( _lhs >>= lhs ) 151*cdf0e10cSrcweir || !( _rhs >>= rhs ) 152*cdf0e10cSrcweir ) 153*cdf0e10cSrcweir throw ::com::sun::star::lang::IllegalArgumentException(); 154*cdf0e10cSrcweir 155*cdf0e10cSrcweir if ( lhs.Year < rhs.Year ) 156*cdf0e10cSrcweir return true; 157*cdf0e10cSrcweir if ( lhs.Year > rhs.Year ) 158*cdf0e10cSrcweir return false; 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir if ( lhs.Month < rhs.Month ) 161*cdf0e10cSrcweir return true; 162*cdf0e10cSrcweir if ( lhs.Month > rhs.Month ) 163*cdf0e10cSrcweir return false; 164*cdf0e10cSrcweir 165*cdf0e10cSrcweir if ( lhs.Day < rhs.Day ) 166*cdf0e10cSrcweir return true; 167*cdf0e10cSrcweir if ( lhs.Day > rhs.Day ) 168*cdf0e10cSrcweir return false; 169*cdf0e10cSrcweir 170*cdf0e10cSrcweir if ( lhs.Hours < rhs.Hours ) 171*cdf0e10cSrcweir return true; 172*cdf0e10cSrcweir if ( lhs.Hours > rhs.Hours ) 173*cdf0e10cSrcweir return false; 174*cdf0e10cSrcweir 175*cdf0e10cSrcweir if ( lhs.Minutes < rhs.Minutes ) 176*cdf0e10cSrcweir return true; 177*cdf0e10cSrcweir if ( lhs.Minutes > rhs.Minutes ) 178*cdf0e10cSrcweir return false; 179*cdf0e10cSrcweir 180*cdf0e10cSrcweir if ( lhs.Seconds < rhs.Seconds ) 181*cdf0e10cSrcweir return true; 182*cdf0e10cSrcweir if ( lhs.Seconds > rhs.Seconds ) 183*cdf0e10cSrcweir return false; 184*cdf0e10cSrcweir 185*cdf0e10cSrcweir if ( lhs.HundredthSeconds < rhs.HundredthSeconds ) 186*cdf0e10cSrcweir return true; 187*cdf0e10cSrcweir return false; 188*cdf0e10cSrcweir } 189*cdf0e10cSrcweir }; 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir //------------------------------------------------------------------------------------------------------------------ 192*cdf0e10cSrcweir ::std::auto_ptr< IKeyPredicateLess > getStandardLessPredicate( Type const & i_type, Reference< XCollator > const & i_collator ) 193*cdf0e10cSrcweir { 194*cdf0e10cSrcweir ::std::auto_ptr< IKeyPredicateLess > pComparator; 195*cdf0e10cSrcweir switch ( i_type.getTypeClass() ) 196*cdf0e10cSrcweir { 197*cdf0e10cSrcweir case TypeClass_CHAR: 198*cdf0e10cSrcweir pComparator.reset( new ScalarPredicateLess< sal_Unicode >() ); 199*cdf0e10cSrcweir break; 200*cdf0e10cSrcweir case TypeClass_BOOLEAN: 201*cdf0e10cSrcweir pComparator.reset( new ScalarPredicateLess< sal_Bool >() ); 202*cdf0e10cSrcweir break; 203*cdf0e10cSrcweir case TypeClass_BYTE: 204*cdf0e10cSrcweir pComparator.reset( new ScalarPredicateLess< sal_Int8 >() ); 205*cdf0e10cSrcweir break; 206*cdf0e10cSrcweir case TypeClass_SHORT: 207*cdf0e10cSrcweir pComparator.reset( new ScalarPredicateLess< sal_Int16 >() ); 208*cdf0e10cSrcweir break; 209*cdf0e10cSrcweir case TypeClass_UNSIGNED_SHORT: 210*cdf0e10cSrcweir pComparator.reset( new ScalarPredicateLess< sal_uInt16 >() ); 211*cdf0e10cSrcweir break; 212*cdf0e10cSrcweir case TypeClass_LONG: 213*cdf0e10cSrcweir pComparator.reset( new ScalarPredicateLess< sal_Int32 >() ); 214*cdf0e10cSrcweir break; 215*cdf0e10cSrcweir case TypeClass_UNSIGNED_LONG: 216*cdf0e10cSrcweir pComparator.reset( new ScalarPredicateLess< sal_uInt32 >() ); 217*cdf0e10cSrcweir break; 218*cdf0e10cSrcweir case TypeClass_HYPER: 219*cdf0e10cSrcweir pComparator.reset( new ScalarPredicateLess< sal_Int64 >() ); 220*cdf0e10cSrcweir break; 221*cdf0e10cSrcweir case TypeClass_UNSIGNED_HYPER: 222*cdf0e10cSrcweir pComparator.reset( new ScalarPredicateLess< sal_uInt64 >() ); 223*cdf0e10cSrcweir break; 224*cdf0e10cSrcweir case TypeClass_FLOAT: 225*cdf0e10cSrcweir pComparator.reset( new ScalarPredicateLess< float >() ); 226*cdf0e10cSrcweir break; 227*cdf0e10cSrcweir case TypeClass_DOUBLE: 228*cdf0e10cSrcweir pComparator.reset( new ScalarPredicateLess< double >() ); 229*cdf0e10cSrcweir break; 230*cdf0e10cSrcweir case TypeClass_STRING: 231*cdf0e10cSrcweir if ( i_collator.is() ) 232*cdf0e10cSrcweir pComparator.reset( new StringCollationPredicateLess( i_collator ) ); 233*cdf0e10cSrcweir else 234*cdf0e10cSrcweir pComparator.reset( new StringPredicateLess() ); 235*cdf0e10cSrcweir break; 236*cdf0e10cSrcweir case TypeClass_TYPE: 237*cdf0e10cSrcweir pComparator.reset( new TypePredicateLess() ); 238*cdf0e10cSrcweir break; 239*cdf0e10cSrcweir case TypeClass_ENUM: 240*cdf0e10cSrcweir pComparator.reset( new EnumPredicateLess( i_type ) ); 241*cdf0e10cSrcweir break; 242*cdf0e10cSrcweir case TypeClass_INTERFACE: 243*cdf0e10cSrcweir pComparator.reset( new InterfacePredicateLess() ); 244*cdf0e10cSrcweir break; 245*cdf0e10cSrcweir case TypeClass_STRUCT: 246*cdf0e10cSrcweir if ( i_type.equals( ::cppu::UnoType< Date >::get() ) ) 247*cdf0e10cSrcweir pComparator.reset( new DatePredicateLess() ); 248*cdf0e10cSrcweir else if ( i_type.equals( ::cppu::UnoType< Time >::get() ) ) 249*cdf0e10cSrcweir pComparator.reset( new TimePredicateLess() ); 250*cdf0e10cSrcweir else if ( i_type.equals( ::cppu::UnoType< DateTime >::get() ) ) 251*cdf0e10cSrcweir pComparator.reset( new DateTimePredicateLess() ); 252*cdf0e10cSrcweir break; 253*cdf0e10cSrcweir default: 254*cdf0e10cSrcweir break; 255*cdf0e10cSrcweir } 256*cdf0e10cSrcweir return pComparator; 257*cdf0e10cSrcweir } 258*cdf0e10cSrcweir 259*cdf0e10cSrcweir //...................................................................................................................... 260*cdf0e10cSrcweir } // namespace comphelper 261*cdf0e10cSrcweir //...................................................................................................................... 262