update credits
[LibreOffice.git] / comphelper / source / misc / anycompare.cxx
blob30e805a791fc74d929bb32e27ad3723c510d6233
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include "comphelper/anycompare.hxx"
22 #include <com/sun/star/util/Date.hpp>
23 #include <com/sun/star/util/Time.hpp>
24 #include <com/sun/star/util/DateTime.hpp>
26 namespace comphelper
28 using ::com::sun::star::uno::Reference;
29 using ::com::sun::star::uno::XInterface;
30 using ::com::sun::star::uno::UNO_QUERY;
31 using ::com::sun::star::uno::UNO_QUERY_THROW;
32 using ::com::sun::star::uno::UNO_SET_THROW;
33 using ::com::sun::star::uno::Exception;
34 using ::com::sun::star::uno::RuntimeException;
35 using ::com::sun::star::uno::Any;
36 using ::com::sun::star::uno::makeAny;
37 using ::com::sun::star::uno::Sequence;
38 using ::com::sun::star::uno::Type;
39 using ::com::sun::star::uno::TypeClass_CHAR;
40 using ::com::sun::star::uno::TypeClass_BOOLEAN;
41 using ::com::sun::star::uno::TypeClass_BYTE;
42 using ::com::sun::star::uno::TypeClass_SHORT;
43 using ::com::sun::star::uno::TypeClass_UNSIGNED_SHORT;
44 using ::com::sun::star::uno::TypeClass_LONG;
45 using ::com::sun::star::uno::TypeClass_UNSIGNED_LONG;
46 using ::com::sun::star::uno::TypeClass_HYPER;
47 using ::com::sun::star::uno::TypeClass_UNSIGNED_HYPER;
48 using ::com::sun::star::uno::TypeClass_FLOAT;
49 using ::com::sun::star::uno::TypeClass_DOUBLE;
50 using ::com::sun::star::uno::TypeClass_STRING;
51 using ::com::sun::star::uno::TypeClass_TYPE;
52 using ::com::sun::star::uno::TypeClass_ENUM;
53 using ::com::sun::star::uno::TypeClass_INTERFACE;
54 using ::com::sun::star::uno::TypeClass_STRUCT;
55 using ::com::sun::star::i18n::XCollator;
56 using ::com::sun::star::util::Date;
57 using ::com::sun::star::util::Time;
58 using ::com::sun::star::util::DateTime;
60 //==================================================================================================================
61 //= DatePredicateLess
62 //==================================================================================================================
63 class DatePredicateLess : public IKeyPredicateLess
65 public:
66 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const
68 Date lhs, rhs;
69 if ( !( _lhs >>= lhs )
70 || !( _rhs >>= rhs )
72 throw ::com::sun::star::lang::IllegalArgumentException();
73 // FIXME Timezone?
75 if ( lhs.Year < rhs.Year )
76 return true;
77 if ( lhs.Year > rhs.Year )
78 return false;
80 if ( lhs.Month < rhs.Month )
81 return true;
82 if ( lhs.Month > rhs.Month )
83 return false;
85 if ( lhs.Day < rhs.Day )
86 return true;
87 return false;
91 //==================================================================================================================
92 //= TimePredicateLess
93 //==================================================================================================================
94 class TimePredicateLess : public IKeyPredicateLess
96 public:
97 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const
99 Time lhs, rhs;
100 if ( !( _lhs >>= lhs )
101 || !( _rhs >>= rhs )
103 throw ::com::sun::star::lang::IllegalArgumentException();
104 // FIXME Timezone?
106 if ( lhs.Hours < rhs.Hours )
107 return true;
108 if ( lhs.Hours > rhs.Hours )
109 return false;
111 if ( lhs.Minutes < rhs.Minutes )
112 return true;
113 if ( lhs.Minutes > rhs.Minutes )
114 return false;
116 if ( lhs.Seconds < rhs.Seconds )
117 return true;
118 if ( lhs.Seconds > rhs.Seconds )
119 return false;
121 if ( lhs.NanoSeconds < rhs.NanoSeconds )
122 return true;
123 return false;
127 //==================================================================================================================
128 //= DateTimePredicateLess
129 //==================================================================================================================
130 class DateTimePredicateLess : public IKeyPredicateLess
132 public:
133 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const
135 DateTime lhs, rhs;
136 if ( !( _lhs >>= lhs )
137 || !( _rhs >>= rhs )
139 throw ::com::sun::star::lang::IllegalArgumentException();
140 // FIXME Timezone?
142 if ( lhs.Year < rhs.Year )
143 return true;
144 if ( lhs.Year > rhs.Year )
145 return false;
147 if ( lhs.Month < rhs.Month )
148 return true;
149 if ( lhs.Month > rhs.Month )
150 return false;
152 if ( lhs.Day < rhs.Day )
153 return true;
154 if ( lhs.Day > rhs.Day )
155 return false;
157 if ( lhs.Hours < rhs.Hours )
158 return true;
159 if ( lhs.Hours > rhs.Hours )
160 return false;
162 if ( lhs.Minutes < rhs.Minutes )
163 return true;
164 if ( lhs.Minutes > rhs.Minutes )
165 return false;
167 if ( lhs.Seconds < rhs.Seconds )
168 return true;
169 if ( lhs.Seconds > rhs.Seconds )
170 return false;
172 if ( lhs.NanoSeconds < rhs.NanoSeconds )
173 return true;
174 return false;
178 //------------------------------------------------------------------------------------------------------------------
179 ::std::auto_ptr< IKeyPredicateLess > getStandardLessPredicate( Type const & i_type, Reference< XCollator > const & i_collator )
181 SAL_WNODEPRECATED_DECLARATIONS_PUSH
182 ::std::auto_ptr< IKeyPredicateLess > pComparator;
183 SAL_WNODEPRECATED_DECLARATIONS_POP
184 switch ( i_type.getTypeClass() )
186 case TypeClass_CHAR:
187 pComparator.reset( new ScalarPredicateLess< sal_Unicode >() );
188 break;
189 case TypeClass_BOOLEAN:
190 pComparator.reset( new ScalarPredicateLess< sal_Bool >() );
191 break;
192 case TypeClass_BYTE:
193 pComparator.reset( new ScalarPredicateLess< sal_Int8 >() );
194 break;
195 case TypeClass_SHORT:
196 pComparator.reset( new ScalarPredicateLess< sal_Int16 >() );
197 break;
198 case TypeClass_UNSIGNED_SHORT:
199 pComparator.reset( new ScalarPredicateLess< sal_uInt16 >() );
200 break;
201 case TypeClass_LONG:
202 pComparator.reset( new ScalarPredicateLess< sal_Int32 >() );
203 break;
204 case TypeClass_UNSIGNED_LONG:
205 pComparator.reset( new ScalarPredicateLess< sal_uInt32 >() );
206 break;
207 case TypeClass_HYPER:
208 pComparator.reset( new ScalarPredicateLess< sal_Int64 >() );
209 break;
210 case TypeClass_UNSIGNED_HYPER:
211 pComparator.reset( new ScalarPredicateLess< sal_uInt64 >() );
212 break;
213 case TypeClass_FLOAT:
214 pComparator.reset( new ScalarPredicateLess< float >() );
215 break;
216 case TypeClass_DOUBLE:
217 pComparator.reset( new ScalarPredicateLess< double >() );
218 break;
219 case TypeClass_STRING:
220 if ( i_collator.is() )
221 pComparator.reset( new StringCollationPredicateLess( i_collator ) );
222 else
223 pComparator.reset( new StringPredicateLess() );
224 break;
225 case TypeClass_TYPE:
226 pComparator.reset( new TypePredicateLess() );
227 break;
228 case TypeClass_ENUM:
229 pComparator.reset( new EnumPredicateLess( i_type ) );
230 break;
231 case TypeClass_INTERFACE:
232 pComparator.reset( new InterfacePredicateLess() );
233 break;
234 case TypeClass_STRUCT:
235 if ( i_type.equals( ::cppu::UnoType< Date >::get() ) )
236 pComparator.reset( new DatePredicateLess() );
237 else if ( i_type.equals( ::cppu::UnoType< Time >::get() ) )
238 pComparator.reset( new TimePredicateLess() );
239 else if ( i_type.equals( ::cppu::UnoType< DateTime >::get() ) )
240 pComparator.reset( new DateTimePredicateLess() );
241 break;
242 default:
243 break;
245 return pComparator;
248 //......................................................................................................................
249 } // namespace comphelper
250 //......................................................................................................................
252 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */