bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / anycompare.cxx
blobf08a65f4dfbd665c9b972cda409f357df1abca51
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 class DatePredicateLess : public IKeyPredicateLess
62 public:
63 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
65 Date lhs, rhs;
66 if ( !( _lhs >>= lhs )
67 || !( _rhs >>= rhs )
69 throw ::com::sun::star::lang::IllegalArgumentException();
70 // FIXME Timezone?
72 if ( lhs.Year < rhs.Year )
73 return true;
74 if ( lhs.Year > rhs.Year )
75 return false;
77 if ( lhs.Month < rhs.Month )
78 return true;
79 if ( lhs.Month > rhs.Month )
80 return false;
82 if ( lhs.Day < rhs.Day )
83 return true;
84 return false;
88 class TimePredicateLess : public IKeyPredicateLess
90 public:
91 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
93 Time lhs, rhs;
94 if ( !( _lhs >>= lhs )
95 || !( _rhs >>= rhs )
97 throw ::com::sun::star::lang::IllegalArgumentException();
98 // FIXME Timezone?
100 if ( lhs.Hours < rhs.Hours )
101 return true;
102 if ( lhs.Hours > rhs.Hours )
103 return false;
105 if ( lhs.Minutes < rhs.Minutes )
106 return true;
107 if ( lhs.Minutes > rhs.Minutes )
108 return false;
110 if ( lhs.Seconds < rhs.Seconds )
111 return true;
112 if ( lhs.Seconds > rhs.Seconds )
113 return false;
115 if ( lhs.NanoSeconds < rhs.NanoSeconds )
116 return true;
117 return false;
121 class DateTimePredicateLess : public IKeyPredicateLess
123 public:
124 virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
126 DateTime lhs, rhs;
127 if ( !( _lhs >>= lhs )
128 || !( _rhs >>= rhs )
130 throw ::com::sun::star::lang::IllegalArgumentException();
131 // FIXME Timezone?
133 if ( lhs.Year < rhs.Year )
134 return true;
135 if ( lhs.Year > rhs.Year )
136 return false;
138 if ( lhs.Month < rhs.Month )
139 return true;
140 if ( lhs.Month > rhs.Month )
141 return false;
143 if ( lhs.Day < rhs.Day )
144 return true;
145 if ( lhs.Day > rhs.Day )
146 return false;
148 if ( lhs.Hours < rhs.Hours )
149 return true;
150 if ( lhs.Hours > rhs.Hours )
151 return false;
153 if ( lhs.Minutes < rhs.Minutes )
154 return true;
155 if ( lhs.Minutes > rhs.Minutes )
156 return false;
158 if ( lhs.Seconds < rhs.Seconds )
159 return true;
160 if ( lhs.Seconds > rhs.Seconds )
161 return false;
163 if ( lhs.NanoSeconds < rhs.NanoSeconds )
164 return true;
165 return false;
170 ::std::unique_ptr< IKeyPredicateLess > getStandardLessPredicate( Type const & i_type, Reference< XCollator > const & i_collator )
172 ::std::unique_ptr< IKeyPredicateLess > pComparator;
173 switch ( i_type.getTypeClass() )
175 case TypeClass_CHAR:
176 pComparator.reset( new ScalarPredicateLess< sal_Unicode >() );
177 break;
178 case TypeClass_BOOLEAN:
179 pComparator.reset( new ScalarPredicateLess< sal_Bool >() );
180 break;
181 case TypeClass_BYTE:
182 pComparator.reset( new ScalarPredicateLess< sal_Int8 >() );
183 break;
184 case TypeClass_SHORT:
185 pComparator.reset( new ScalarPredicateLess< sal_Int16 >() );
186 break;
187 case TypeClass_UNSIGNED_SHORT:
188 pComparator.reset( new ScalarPredicateLess< sal_uInt16 >() );
189 break;
190 case TypeClass_LONG:
191 pComparator.reset( new ScalarPredicateLess< sal_Int32 >() );
192 break;
193 case TypeClass_UNSIGNED_LONG:
194 pComparator.reset( new ScalarPredicateLess< sal_uInt32 >() );
195 break;
196 case TypeClass_HYPER:
197 pComparator.reset( new ScalarPredicateLess< sal_Int64 >() );
198 break;
199 case TypeClass_UNSIGNED_HYPER:
200 pComparator.reset( new ScalarPredicateLess< sal_uInt64 >() );
201 break;
202 case TypeClass_FLOAT:
203 pComparator.reset( new ScalarPredicateLess< float >() );
204 break;
205 case TypeClass_DOUBLE:
206 pComparator.reset( new ScalarPredicateLess< double >() );
207 break;
208 case TypeClass_STRING:
209 if ( i_collator.is() )
210 pComparator.reset( new StringCollationPredicateLess( i_collator ) );
211 else
212 pComparator.reset( new StringPredicateLess() );
213 break;
214 case TypeClass_TYPE:
215 pComparator.reset( new TypePredicateLess() );
216 break;
217 case TypeClass_ENUM:
218 pComparator.reset( new EnumPredicateLess( i_type ) );
219 break;
220 case TypeClass_INTERFACE:
221 pComparator.reset( new InterfacePredicateLess() );
222 break;
223 case TypeClass_STRUCT:
224 if ( i_type.equals( ::cppu::UnoType< Date >::get() ) )
225 pComparator.reset( new DatePredicateLess() );
226 else if ( i_type.equals( ::cppu::UnoType< Time >::get() ) )
227 pComparator.reset( new TimePredicateLess() );
228 else if ( i_type.equals( ::cppu::UnoType< DateTime >::get() ) )
229 pComparator.reset( new DateTimePredicateLess() );
230 break;
231 default:
232 break;
234 return pComparator;
238 } // namespace comphelper
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */