• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef LIBTEXTCLASSIFIER_UTILS_CALENDAR_CALENDAR_JAVAICU_H_
18 #define LIBTEXTCLASSIFIER_UTILS_CALENDAR_CALENDAR_JAVAICU_H_
19 
20 #include <jni.h>
21 
22 #include <memory>
23 #include <string>
24 
25 #include "annotator/types.h"
26 #include "utils/base/integral_types.h"
27 #include "utils/calendar/calendar-common.h"
28 #include "utils/java/jni-base.h"
29 #include "utils/java/jni-cache.h"
30 
31 namespace libtextclassifier3 {
32 
33 class Calendar {
34  public:
35   explicit Calendar(JniCache* jni_cache);
36   bool Initialize(const std::string& time_zone, const std::string& locale,
37                   int64 time_ms_utc);
38   bool AddSecond(int value) const;
39   bool AddMinute(int value) const;
40   bool AddHourOfDay(int value) const;
41   bool AddDayOfMonth(int value) const;
42   bool AddYear(int value) const;
43   bool AddMonth(int value) const;
44   bool GetDayOfWeek(int* value) const;
45   bool GetFirstDayOfWeek(int* value) const;
46   bool GetTimeInMillis(int64* value) const;
47   bool SetZoneOffset(int value) const;
48   bool SetDstOffset(int value) const;
49   bool SetYear(int value) const;
50   bool SetMonth(int value) const;
51   bool SetDayOfYear(int value) const;
52   bool SetDayOfMonth(int value) const;
53   bool SetDayOfWeek(int value) const;
54   bool SetHourOfDay(int value) const;
55   bool SetMinute(int value) const;
56   bool SetSecond(int value) const;
57   bool SetMillisecond(int value) const;
58 
59  private:
60   JniCache* jni_cache_;
61   JNIEnv* jenv_;
62   ScopedLocalRef<jobject> calendar_;
63 };
64 
65 class CalendarLib {
66  public:
67   CalendarLib();
68   explicit CalendarLib(const std::shared_ptr<JniCache>& jni_cache);
69 
70   // Returns false (dummy version).
InterpretParseData(const DatetimeParsedData & parse_data,int64 reference_time_ms_utc,const std::string & reference_timezone,const std::string & reference_locale,bool prefer_future_for_unspecified_date,int64 * interpreted_time_ms_utc,DatetimeGranularity * granularity)71   bool InterpretParseData(const DatetimeParsedData& parse_data,
72                           int64 reference_time_ms_utc,
73                           const std::string& reference_timezone,
74                           const std::string& reference_locale,
75                           bool prefer_future_for_unspecified_date,
76                           int64* interpreted_time_ms_utc,
77                           DatetimeGranularity* granularity) const {
78     Calendar calendar(jni_cache_.get());
79     if (!impl_.InterpretParseData(parse_data, reference_time_ms_utc,
80                                   reference_timezone, reference_locale,
81                                   prefer_future_for_unspecified_date, &calendar,
82                                   granularity)) {
83       return false;
84     }
85     return calendar.GetTimeInMillis(interpreted_time_ms_utc);
86   }
87 
GetGranularity(const DatetimeParsedData & data)88   DatetimeGranularity GetGranularity(const DatetimeParsedData& data) const {
89     return impl_.GetGranularity(data);
90   }
91 
92  private:
93   std::shared_ptr<JniCache> jni_cache_;
94   calendar::CalendarLibTempl<Calendar> impl_;
95 };
96 
97 }  // namespace libtextclassifier3
98 
99 #endif  // LIBTEXTCLASSIFIER_UTILS_CALENDAR_CALENDAR_JAVAICU_H_
100