1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "precompiled_comphelper.hxx"
25 
26 #include "comphelper/anycompare.hxx"
27 
28 /** === begin UNO includes === **/
29 #include <com/sun/star/util/Date.hpp>
30 #include <com/sun/star/util/Time.hpp>
31 #include <com/sun/star/util/DateTime.hpp>
32 /** === end UNO includes === **/
33 
34 //......................................................................................................................
35 namespace comphelper
36 {
37 //......................................................................................................................
38 
39 	/** === begin UNO using === **/
40 	using ::com::sun::star::uno::Reference;
41 	using ::com::sun::star::uno::XInterface;
42 	using ::com::sun::star::uno::UNO_QUERY;
43 	using ::com::sun::star::uno::UNO_QUERY_THROW;
44 	using ::com::sun::star::uno::UNO_SET_THROW;
45 	using ::com::sun::star::uno::Exception;
46 	using ::com::sun::star::uno::RuntimeException;
47 	using ::com::sun::star::uno::Any;
48 	using ::com::sun::star::uno::makeAny;
49 	using ::com::sun::star::uno::Sequence;
50 	using ::com::sun::star::uno::Type;
51     using ::com::sun::star::uno::TypeClass_CHAR;
52     using ::com::sun::star::uno::TypeClass_BOOLEAN;
53     using ::com::sun::star::uno::TypeClass_BYTE;
54     using ::com::sun::star::uno::TypeClass_SHORT;
55     using ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT;
56     using ::com::sun::star::uno::TypeClass_LONG;
57     using ::com::sun::star::uno::TypeClass_UNSIGNED_LONG;
58     using ::com::sun::star::uno::TypeClass_HYPER;
59     using ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER;
60     using ::com::sun::star::uno::TypeClass_FLOAT;
61     using ::com::sun::star::uno::TypeClass_DOUBLE;
62     using ::com::sun::star::uno::TypeClass_STRING;
63     using ::com::sun::star::uno::TypeClass_TYPE;
64     using ::com::sun::star::uno::TypeClass_ENUM;
65     using ::com::sun::star::uno::TypeClass_INTERFACE;
66     using ::com::sun::star::uno::TypeClass_STRUCT;
67     using ::com::sun::star::i18n::XCollator;
68     using ::com::sun::star::util::Date;
69     using ::com::sun::star::util::Time;
70     using ::com::sun::star::util::DateTime;
71 	/** === end UNO using === **/
72 
73 	//==================================================================================================================
74 	//= DatePredicateLess
75 	//==================================================================================================================
76     class DatePredicateLess : public IKeyPredicateLess
77     {
78     public:
isLess(::com::sun::star::uno::Any const & _lhs,::com::sun::star::uno::Any const & _rhs) const79         virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const
80         {
81             Date lhs, rhs;
82             if  (   !( _lhs >>= lhs )
83                 ||  !( _rhs >>= rhs )
84                 )
85                 throw ::com::sun::star::lang::IllegalArgumentException();
86 
87             if ( lhs.Year < rhs.Year )
88                 return true;
89             if ( lhs.Year > rhs.Year )
90                 return false;
91 
92             if ( lhs.Month < rhs.Month )
93                 return true;
94             if ( lhs.Month > rhs.Month )
95                 return false;
96 
97             if ( lhs.Day < rhs.Day )
98                 return true;
99             return false;
100         }
101     };
102 
103 	//==================================================================================================================
104 	//= TimePredicateLess
105 	//==================================================================================================================
106     class TimePredicateLess : public IKeyPredicateLess
107     {
108     public:
isLess(::com::sun::star::uno::Any const & _lhs,::com::sun::star::uno::Any const & _rhs) const109         virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const
110         {
111             Time lhs, rhs;
112             if  (   !( _lhs >>= lhs )
113                 ||  !( _rhs >>= rhs )
114                 )
115                 throw ::com::sun::star::lang::IllegalArgumentException();
116 
117             if ( lhs.Hours < rhs.Hours )
118                 return true;
119             if ( lhs.Hours > rhs.Hours )
120                 return false;
121 
122             if ( lhs.Minutes < rhs.Minutes )
123                 return true;
124             if ( lhs.Minutes > rhs.Minutes )
125                 return false;
126 
127             if ( lhs.Seconds < rhs.Seconds )
128                 return true;
129             if ( lhs.Seconds > rhs.Seconds )
130                 return false;
131 
132             if ( lhs.HundredthSeconds < rhs.HundredthSeconds )
133                 return true;
134             return false;
135         }
136     };
137 
138 	//==================================================================================================================
139 	//= DateTimePredicateLess
140 	//==================================================================================================================
141     class DateTimePredicateLess : public IKeyPredicateLess
142     {
143     public:
isLess(::com::sun::star::uno::Any const & _lhs,::com::sun::star::uno::Any const & _rhs) const144         virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const
145         {
146             DateTime lhs, rhs;
147             if  (   !( _lhs >>= lhs )
148                 ||  !( _rhs >>= rhs )
149                 )
150                 throw ::com::sun::star::lang::IllegalArgumentException();
151 
152             if ( lhs.Year < rhs.Year )
153                 return true;
154             if ( lhs.Year > rhs.Year )
155                 return false;
156 
157             if ( lhs.Month < rhs.Month )
158                 return true;
159             if ( lhs.Month > rhs.Month )
160                 return false;
161 
162             if ( lhs.Day < rhs.Day )
163                 return true;
164             if ( lhs.Day > rhs.Day )
165                 return false;
166 
167             if ( lhs.Hours < rhs.Hours )
168                 return true;
169             if ( lhs.Hours > rhs.Hours )
170                 return false;
171 
172             if ( lhs.Minutes < rhs.Minutes )
173                 return true;
174             if ( lhs.Minutes > rhs.Minutes )
175                 return false;
176 
177             if ( lhs.Seconds < rhs.Seconds )
178                 return true;
179             if ( lhs.Seconds > rhs.Seconds )
180                 return false;
181 
182             if ( lhs.HundredthSeconds < rhs.HundredthSeconds )
183                 return true;
184             return false;
185         }
186     };
187 
188 	//------------------------------------------------------------------------------------------------------------------
getStandardLessPredicate(Type const & i_type,Reference<XCollator> const & i_collator)189     ::std::auto_ptr< IKeyPredicateLess > getStandardLessPredicate( Type const & i_type, Reference< XCollator > const & i_collator )
190     {
191         ::std::auto_ptr< IKeyPredicateLess > pComparator;
192         switch ( i_type.getTypeClass() )
193         {
194         case TypeClass_CHAR:
195 			pComparator.reset( new ScalarPredicateLess< sal_Unicode >() );
196             break;
197         case TypeClass_BOOLEAN:
198 			pComparator.reset( new ScalarPredicateLess< sal_Bool >() );
199             break;
200         case TypeClass_BYTE:
201 			pComparator.reset( new ScalarPredicateLess< sal_Int8 >() );
202             break;
203         case TypeClass_SHORT:
204 			pComparator.reset( new ScalarPredicateLess< sal_Int16 >() );
205             break;
206         case TypeClass_UNSIGNED_SHORT:
207 			pComparator.reset( new ScalarPredicateLess< sal_uInt16 >() );
208             break;
209         case TypeClass_LONG:
210 			pComparator.reset( new ScalarPredicateLess< sal_Int32 >() );
211             break;
212         case TypeClass_UNSIGNED_LONG:
213 			pComparator.reset( new ScalarPredicateLess< sal_uInt32 >() );
214             break;
215         case TypeClass_HYPER:
216 			pComparator.reset( new ScalarPredicateLess< sal_Int64 >() );
217             break;
218         case TypeClass_UNSIGNED_HYPER:
219 			pComparator.reset( new ScalarPredicateLess< sal_uInt64 >() );
220             break;
221         case TypeClass_FLOAT:
222             pComparator.reset( new ScalarPredicateLess< float >() );
223             break;
224         case TypeClass_DOUBLE:
225             pComparator.reset( new ScalarPredicateLess< double >() );
226             break;
227         case TypeClass_STRING:
228             if ( i_collator.is() )
229                 pComparator.reset( new StringCollationPredicateLess( i_collator ) );
230             else
231                 pComparator.reset( new StringPredicateLess() );
232             break;
233         case TypeClass_TYPE:
234             pComparator.reset( new TypePredicateLess() );
235             break;
236         case TypeClass_ENUM:
237             pComparator.reset( new EnumPredicateLess( i_type ) );
238             break;
239         case TypeClass_INTERFACE:
240             pComparator.reset( new InterfacePredicateLess() );
241             break;
242         case TypeClass_STRUCT:
243             if ( i_type.equals( ::cppu::UnoType< Date >::get() ) )
244                 pComparator.reset( new DatePredicateLess() );
245             else if ( i_type.equals( ::cppu::UnoType< Time >::get() ) )
246                 pComparator.reset( new TimePredicateLess() );
247             else if ( i_type.equals( ::cppu::UnoType< DateTime >::get() ) )
248                 pComparator.reset( new DateTimePredicateLess() );
249             break;
250         default:
251             break;
252         }
253         return pComparator;
254     }
255 
256 //......................................................................................................................
257 } // namespace comphelper
258 //......................................................................................................................
259