Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "comphelper/anycompare.hxx"
21 :
22 : #include <com/sun/star/util/Date.hpp>
23 : #include <com/sun/star/util/Time.hpp>
24 : #include <com/sun/star/util/DateTime.hpp>
25 :
26 : namespace comphelper
27 : {
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;
59 :
60 :
61 : //= DatePredicateLess
62 :
63 0 : class DatePredicateLess : public IKeyPredicateLess
64 : {
65 : public:
66 0 : virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
67 : {
68 0 : Date lhs, rhs;
69 0 : if ( !( _lhs >>= lhs )
70 0 : || !( _rhs >>= rhs )
71 : )
72 0 : throw ::com::sun::star::lang::IllegalArgumentException();
73 : // FIXME Timezone?
74 :
75 0 : if ( lhs.Year < rhs.Year )
76 0 : return true;
77 0 : if ( lhs.Year > rhs.Year )
78 0 : return false;
79 :
80 0 : if ( lhs.Month < rhs.Month )
81 0 : return true;
82 0 : if ( lhs.Month > rhs.Month )
83 0 : return false;
84 :
85 0 : if ( lhs.Day < rhs.Day )
86 0 : return true;
87 0 : return false;
88 : }
89 : };
90 :
91 :
92 : //= TimePredicateLess
93 :
94 0 : class TimePredicateLess : public IKeyPredicateLess
95 : {
96 : public:
97 0 : virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
98 : {
99 0 : Time lhs, rhs;
100 0 : if ( !( _lhs >>= lhs )
101 0 : || !( _rhs >>= rhs )
102 : )
103 0 : throw ::com::sun::star::lang::IllegalArgumentException();
104 : // FIXME Timezone?
105 :
106 0 : if ( lhs.Hours < rhs.Hours )
107 0 : return true;
108 0 : if ( lhs.Hours > rhs.Hours )
109 0 : return false;
110 :
111 0 : if ( lhs.Minutes < rhs.Minutes )
112 0 : return true;
113 0 : if ( lhs.Minutes > rhs.Minutes )
114 0 : return false;
115 :
116 0 : if ( lhs.Seconds < rhs.Seconds )
117 0 : return true;
118 0 : if ( lhs.Seconds > rhs.Seconds )
119 0 : return false;
120 :
121 0 : if ( lhs.NanoSeconds < rhs.NanoSeconds )
122 0 : return true;
123 0 : return false;
124 : }
125 : };
126 :
127 :
128 : //= DateTimePredicateLess
129 :
130 0 : class DateTimePredicateLess : public IKeyPredicateLess
131 : {
132 : public:
133 0 : virtual bool isLess( ::com::sun::star::uno::Any const & _lhs, ::com::sun::star::uno::Any const & _rhs ) const SAL_OVERRIDE
134 : {
135 0 : DateTime lhs, rhs;
136 0 : if ( !( _lhs >>= lhs )
137 0 : || !( _rhs >>= rhs )
138 : )
139 0 : throw ::com::sun::star::lang::IllegalArgumentException();
140 : // FIXME Timezone?
141 :
142 0 : if ( lhs.Year < rhs.Year )
143 0 : return true;
144 0 : if ( lhs.Year > rhs.Year )
145 0 : return false;
146 :
147 0 : if ( lhs.Month < rhs.Month )
148 0 : return true;
149 0 : if ( lhs.Month > rhs.Month )
150 0 : return false;
151 :
152 0 : if ( lhs.Day < rhs.Day )
153 0 : return true;
154 0 : if ( lhs.Day > rhs.Day )
155 0 : return false;
156 :
157 0 : if ( lhs.Hours < rhs.Hours )
158 0 : return true;
159 0 : if ( lhs.Hours > rhs.Hours )
160 0 : return false;
161 :
162 0 : if ( lhs.Minutes < rhs.Minutes )
163 0 : return true;
164 0 : if ( lhs.Minutes > rhs.Minutes )
165 0 : return false;
166 :
167 0 : if ( lhs.Seconds < rhs.Seconds )
168 0 : return true;
169 0 : if ( lhs.Seconds > rhs.Seconds )
170 0 : return false;
171 :
172 0 : if ( lhs.NanoSeconds < rhs.NanoSeconds )
173 0 : return true;
174 0 : return false;
175 : }
176 : };
177 :
178 :
179 0 : ::std::auto_ptr< IKeyPredicateLess > getStandardLessPredicate( Type const & i_type, Reference< XCollator > const & i_collator )
180 : {
181 : SAL_WNODEPRECATED_DECLARATIONS_PUSH
182 0 : ::std::auto_ptr< IKeyPredicateLess > pComparator;
183 : SAL_WNODEPRECATED_DECLARATIONS_POP
184 0 : switch ( i_type.getTypeClass() )
185 : {
186 : case TypeClass_CHAR:
187 0 : pComparator.reset( new ScalarPredicateLess< sal_Unicode >() );
188 0 : break;
189 : case TypeClass_BOOLEAN:
190 0 : pComparator.reset( new ScalarPredicateLess< sal_Bool >() );
191 0 : break;
192 : case TypeClass_BYTE:
193 0 : pComparator.reset( new ScalarPredicateLess< sal_Int8 >() );
194 0 : break;
195 : case TypeClass_SHORT:
196 0 : pComparator.reset( new ScalarPredicateLess< sal_Int16 >() );
197 0 : break;
198 : case TypeClass_UNSIGNED_SHORT:
199 0 : pComparator.reset( new ScalarPredicateLess< sal_uInt16 >() );
200 0 : break;
201 : case TypeClass_LONG:
202 0 : pComparator.reset( new ScalarPredicateLess< sal_Int32 >() );
203 0 : break;
204 : case TypeClass_UNSIGNED_LONG:
205 0 : pComparator.reset( new ScalarPredicateLess< sal_uInt32 >() );
206 0 : break;
207 : case TypeClass_HYPER:
208 0 : pComparator.reset( new ScalarPredicateLess< sal_Int64 >() );
209 0 : break;
210 : case TypeClass_UNSIGNED_HYPER:
211 0 : pComparator.reset( new ScalarPredicateLess< sal_uInt64 >() );
212 0 : break;
213 : case TypeClass_FLOAT:
214 0 : pComparator.reset( new ScalarPredicateLess< float >() );
215 0 : break;
216 : case TypeClass_DOUBLE:
217 0 : pComparator.reset( new ScalarPredicateLess< double >() );
218 0 : break;
219 : case TypeClass_STRING:
220 0 : if ( i_collator.is() )
221 0 : pComparator.reset( new StringCollationPredicateLess( i_collator ) );
222 : else
223 0 : pComparator.reset( new StringPredicateLess() );
224 0 : break;
225 : case TypeClass_TYPE:
226 0 : pComparator.reset( new TypePredicateLess() );
227 0 : break;
228 : case TypeClass_ENUM:
229 0 : pComparator.reset( new EnumPredicateLess( i_type ) );
230 0 : break;
231 : case TypeClass_INTERFACE:
232 0 : pComparator.reset( new InterfacePredicateLess() );
233 0 : break;
234 : case TypeClass_STRUCT:
235 0 : if ( i_type.equals( ::cppu::UnoType< Date >::get() ) )
236 0 : pComparator.reset( new DatePredicateLess() );
237 0 : else if ( i_type.equals( ::cppu::UnoType< Time >::get() ) )
238 0 : pComparator.reset( new TimePredicateLess() );
239 0 : else if ( i_type.equals( ::cppu::UnoType< DateTime >::get() ) )
240 0 : pComparator.reset( new DateTimePredicateLess() );
241 0 : break;
242 : default:
243 0 : break;
244 : }
245 0 : return pComparator;
246 : }
247 :
248 :
249 : } // namespace comphelper
250 :
251 :
252 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|