diff options
author | Victor Chang <vichang@google.com> | 2021-02-09 21:32:35 +0000 |
---|---|---|
committer | Victor Chang <vichang@google.com> | 2021-02-11 12:17:00 +0000 |
commit | 1d2e8f40c0e338e82bedcaea17763fdad9ca1ad7 (patch) | |
tree | 1fece9492d63d6d4189214aabdfcddf21c10cf07 /android_icu4j | |
parent | 75f20ebb72aabef5187259fe6929c1ab7073d227 (diff) | |
download | icu-1d2e8f40c0e338e82bedcaea17763fdad9ca1ad7.tar.gz |
Remove all @IntraCoreApi in android.icu
The APIs has been moved to com.android.icu package.
Bug: 139480281
Test: atest CtsIcuTestCases:com.android.icu.test.util
Change-Id: I7152a5f5381a5d41e06133a196b6a97667378cbe
Diffstat (limited to 'android_icu4j')
9 files changed, 190 insertions, 97 deletions
diff --git a/android_icu4j/api/intra/current.txt b/android_icu4j/api/intra/current.txt index ae66e3bdf..38520620b 100644 --- a/android_icu4j/api/intra/current.txt +++ b/android_icu4j/api/intra/current.txt @@ -1,65 +1,4 @@ // Signature format: 2.0 -package android.icu.text { - - public abstract class TimeZoneNames implements java.io.Serializable { - method public java.util.Collection<android.icu.text.TimeZoneNames.MatchInfo> find(CharSequence, int, java.util.EnumSet<android.icu.text.TimeZoneNames.NameType>); - } - - public static class TimeZoneNames.MatchInfo { - method public int matchLength(); - method public String mzID(); - method public android.icu.text.TimeZoneNames.NameType nameType(); - method public String tzID(); - } - -} - -package android.icu.util { - - public class AnnualTimeZoneRule extends android.icu.util.TimeZoneRule { - method public int getEndYear(); - method public android.icu.util.DateTimeRule getRule(); - field public static final int MAX_YEAR = 2147483647; // 0x7fffffff - } - - public abstract class BasicTimeZone extends android.icu.util.TimeZone { - method public abstract android.icu.util.TimeZoneTransition getNextTransition(long, boolean); - method public abstract android.icu.util.TimeZoneTransition getPreviousTransition(long, boolean); - method public abstract android.icu.util.TimeZoneRule[] getTimeZoneRules(); - } - - public class DateTimeRule implements java.io.Serializable { - method public int getDateRuleType(); - method public int getRuleDayOfMonth(); - method public int getRuleDayOfWeek(); - method public int getRuleMillisInDay(); - method public int getRuleMonth(); - method public int getTimeRuleType(); - field public static final int DOM = 0; // 0x0 - field public static final int DOW = 1; // 0x1 - field public static final int DOW_GEQ_DOM = 2; // 0x2 - field public static final int DOW_LEQ_DOM = 3; // 0x3 - field public static final int STANDARD_TIME = 1; // 0x1 - field public static final int UTC_TIME = 2; // 0x2 - field public static final int WALL_TIME = 0; // 0x0 - } - - public class InitialTimeZoneRule extends android.icu.util.TimeZoneRule { - } - - public abstract class TimeZoneRule implements java.io.Serializable { - method public int getDSTSavings(); - method public int getRawOffset(); - } - - public class TimeZoneTransition { - method public android.icu.util.TimeZoneRule getFrom(); - method public long getTime(); - method public android.icu.util.TimeZoneRule getTo(); - } - -} - package com.android.i18n.system { public class AppSpecializationHooks { diff --git a/android_icu4j/src/main/java/android/icu/text/TimeZoneNames.java b/android_icu4j/src/main/java/android/icu/text/TimeZoneNames.java index c53e32021..db832e983 100644 --- a/android_icu4j/src/main/java/android/icu/text/TimeZoneNames.java +++ b/android_icu4j/src/main/java/android/icu/text/TimeZoneNames.java @@ -313,7 +313,6 @@ public abstract class TimeZoneNames implements Serializable { * @see MatchInfo * @hide draft / provisional / internal are hidden on Android */ - @libcore.api.IntraCoreApi public Collection<MatchInfo> find(CharSequence text, int start, EnumSet<NameType> types) { throw new UnsupportedOperationException("The method is not implemented in TimeZoneNames base class."); } @@ -324,7 +323,6 @@ public abstract class TimeZoneNames implements Serializable { * @hide Only a subset of ICU is exposed in Android * @hide draft / provisional / internal are hidden on Android */ - @libcore.api.IntraCoreApi public static class MatchInfo { private NameType _nameType; private String _tzID; @@ -370,7 +368,6 @@ public abstract class TimeZoneNames implements Serializable { * @see #mzID() * @hide draft / provisional / internal are hidden on Android */ - @libcore.api.IntraCoreApi public String tzID() { return _tzID; } @@ -385,7 +382,6 @@ public abstract class TimeZoneNames implements Serializable { * @see #tzID() * @hide draft / provisional / internal are hidden on Android */ - @libcore.api.IntraCoreApi public String mzID() { return _mzID; } @@ -396,7 +392,6 @@ public abstract class TimeZoneNames implements Serializable { * @see NameType * @hide draft / provisional / internal are hidden on Android */ - @libcore.api.IntraCoreApi public NameType nameType() { return _nameType; } @@ -406,7 +401,6 @@ public abstract class TimeZoneNames implements Serializable { * @return the match length. * @hide draft / provisional / internal are hidden on Android */ - @libcore.api.IntraCoreApi public int matchLength() { return _matchLength; } diff --git a/android_icu4j/src/main/java/android/icu/util/AnnualTimeZoneRule.java b/android_icu4j/src/main/java/android/icu/util/AnnualTimeZoneRule.java index 65926dd12..b80896845 100644 --- a/android_icu4j/src/main/java/android/icu/util/AnnualTimeZoneRule.java +++ b/android_icu4j/src/main/java/android/icu/util/AnnualTimeZoneRule.java @@ -20,7 +20,6 @@ import android.icu.impl.Grego; * * @hide Only a subset of ICU is exposed in Android */ -@libcore.api.IntraCoreApi public class AnnualTimeZoneRule extends TimeZoneRule { private static final long serialVersionUID = -8870666707791230688L; @@ -28,7 +27,6 @@ public class AnnualTimeZoneRule extends TimeZoneRule { /** * The constant representing the maximum year used for designating a rule is permanent. */ - @libcore.api.IntraCoreApi public static final int MAX_YEAR = Integer.MAX_VALUE; private final DateTimeRule dateTimeRule; @@ -64,7 +62,6 @@ public class AnnualTimeZoneRule extends TimeZoneRule { * @return An <code>AnnualDateTimeRule</code> which represents the start date/time * rule used by this time zone rule. */ - @libcore.api.IntraCoreApi public DateTimeRule getRule() { return dateTimeRule; } @@ -85,7 +82,6 @@ public class AnnualTimeZoneRule extends TimeZoneRule { * @return The end year of this rule (inclusive). The year is in Gregorian calendar * with 0 == 1 BCE, -1 == 2 BCE, etc. */ - @libcore.api.IntraCoreApi public int getEndYear() { return endYear; } diff --git a/android_icu4j/src/main/java/android/icu/util/BasicTimeZone.java b/android_icu4j/src/main/java/android/icu/util/BasicTimeZone.java index b885c2506..216742003 100644 --- a/android_icu4j/src/main/java/android/icu/util/BasicTimeZone.java +++ b/android_icu4j/src/main/java/android/icu/util/BasicTimeZone.java @@ -28,7 +28,6 @@ import android.icu.impl.Grego; * * @hide Only a subset of ICU is exposed in Android */ -@libcore.api.IntraCoreApi @libcore.api.CorePlatformApi public abstract class BasicTimeZone extends TimeZone { @@ -47,7 +46,6 @@ public abstract class BasicTimeZone extends TimeZone { * after the given base time, or null if no time zone transitions * are available after the base time. */ - @libcore.api.IntraCoreApi @libcore.api.CorePlatformApi public abstract TimeZoneTransition getNextTransition(long base, boolean inclusive); @@ -62,7 +60,6 @@ public abstract class BasicTimeZone extends TimeZone { * before the given base time, or null if no time zone transitions * are available before the base time. */ - @libcore.api.IntraCoreApi public abstract TimeZoneTransition getPreviousTransition(long base, boolean inclusive); /** @@ -214,7 +211,6 @@ public abstract class BasicTimeZone extends TimeZone { * @return The array of <code>TimeZoneRule</code> which represents this * time zone. */ - @libcore.api.IntraCoreApi public abstract TimeZoneRule[] getTimeZoneRules(); /** diff --git a/android_icu4j/src/main/java/android/icu/util/DateTimeRule.java b/android_icu4j/src/main/java/android/icu/util/DateTimeRule.java index 3f26a3cd8..b1dcb9ddb 100644 --- a/android_icu4j/src/main/java/android/icu/util/DateTimeRule.java +++ b/android_icu4j/src/main/java/android/icu/util/DateTimeRule.java @@ -18,7 +18,6 @@ import java.io.Serializable; * * @hide Only a subset of ICU is exposed in Android */ -@libcore.api.IntraCoreApi public class DateTimeRule implements Serializable { private static final long serialVersionUID = 2183055795738051443L; @@ -27,14 +26,12 @@ public class DateTimeRule implements Serializable { * Date rule type defined by exact day of month. * For example, March 14. */ - @libcore.api.IntraCoreApi public static final int DOM = 0; /** * Date rule type defined by day of week in month. * For example, 2nd Sunday in March. */ - @libcore.api.IntraCoreApi public static final int DOW = 1; /** @@ -42,7 +39,6 @@ public class DateTimeRule implements Serializable { * after exact day of month. * For example, 1st Monday on or after March 15. */ - @libcore.api.IntraCoreApi public static final int DOW_GEQ_DOM = 2; /** @@ -50,25 +46,21 @@ public class DateTimeRule implements Serializable { * before exact day of month. * For example, last Saturday on or before March 15. */ - @libcore.api.IntraCoreApi public static final int DOW_LEQ_DOM = 3; /** * Time rule type for local wall time. */ - @libcore.api.IntraCoreApi public static final int WALL_TIME = 0; /** * Time rule type for local standard time. */ - @libcore.api.IntraCoreApi public static final int STANDARD_TIME = 1; /** * Time rule type for coordinated universal time. */ - @libcore.api.IntraCoreApi public static final int UTC_TIME = 2; // private stuff @@ -167,7 +159,6 @@ public class DateTimeRule implements Serializable { * * @return The date rule type. */ - @libcore.api.IntraCoreApi public int getDateRuleType() { return dateRuleType; } @@ -177,7 +168,6 @@ public class DateTimeRule implements Serializable { * * @return The rule month. */ - @libcore.api.IntraCoreApi public int getRuleMonth() { return month; } @@ -188,7 +178,6 @@ public class DateTimeRule implements Serializable { * * @return The rule day of month */ - @libcore.api.IntraCoreApi public int getRuleDayOfMonth() { return dayOfMonth; } @@ -199,7 +188,6 @@ public class DateTimeRule implements Serializable { * * @return The rule day of week. */ - @libcore.api.IntraCoreApi public int getRuleDayOfWeek() { return dayOfWeek; } @@ -221,7 +209,6 @@ public class DateTimeRule implements Serializable { * @return The time rule type, either <code>WALL_TIME</code> or <code>STANDARD_TIME</code> * or <code>UTC_TIME</code>. */ - @libcore.api.IntraCoreApi public int getTimeRuleType() { return timeRuleType; } @@ -231,7 +218,6 @@ public class DateTimeRule implements Serializable { * * @return The time in the rule day in milliseconds. */ - @libcore.api.IntraCoreApi public int getRuleMillisInDay() { return millisInDay; } diff --git a/android_icu4j/src/main/java/android/icu/util/InitialTimeZoneRule.java b/android_icu4j/src/main/java/android/icu/util/InitialTimeZoneRule.java index cafcfffac..3bdf2aaa4 100644 --- a/android_icu4j/src/main/java/android/icu/util/InitialTimeZoneRule.java +++ b/android_icu4j/src/main/java/android/icu/util/InitialTimeZoneRule.java @@ -18,7 +18,6 @@ import java.util.Date; * * @hide Only a subset of ICU is exposed in Android */ -@libcore.api.IntraCoreApi public class InitialTimeZoneRule extends TimeZoneRule { private static final long serialVersionUID = 1876594993064051206L; diff --git a/android_icu4j/src/main/java/android/icu/util/TimeZoneRule.java b/android_icu4j/src/main/java/android/icu/util/TimeZoneRule.java index c0834e9c4..2d4f63a1d 100644 --- a/android_icu4j/src/main/java/android/icu/util/TimeZoneRule.java +++ b/android_icu4j/src/main/java/android/icu/util/TimeZoneRule.java @@ -22,7 +22,6 @@ import java.util.Date; * * @hide Only a subset of ICU is exposed in Android */ -@libcore.api.IntraCoreApi @libcore.api.CorePlatformApi public abstract class TimeZoneRule implements Serializable { @@ -61,7 +60,6 @@ public abstract class TimeZoneRule implements Serializable { * * @return The standard time offset from UTC in milliseconds. */ - @libcore.api.IntraCoreApi public int getRawOffset() { return rawOffset; } @@ -72,7 +70,6 @@ public abstract class TimeZoneRule implements Serializable { * @return The amount of daylight saving offset used by this rule * in milliseconds. */ - @libcore.api.IntraCoreApi @libcore.api.CorePlatformApi public int getDSTSavings() { return dstSavings; diff --git a/android_icu4j/src/main/java/android/icu/util/TimeZoneTransition.java b/android_icu4j/src/main/java/android/icu/util/TimeZoneTransition.java index c035ed1b5..9cea34fc7 100644 --- a/android_icu4j/src/main/java/android/icu/util/TimeZoneTransition.java +++ b/android_icu4j/src/main/java/android/icu/util/TimeZoneTransition.java @@ -15,7 +15,6 @@ package android.icu.util; * * @hide Only a subset of ICU is exposed in Android */ -@libcore.api.IntraCoreApi @libcore.api.CorePlatformApi public class TimeZoneTransition { private final TimeZoneRule from; @@ -41,7 +40,6 @@ public class TimeZoneTransition { * * @return The time of the transition in milliseconds since the base time. */ - @libcore.api.IntraCoreApi @libcore.api.CorePlatformApi public long getTime() { return time; @@ -52,7 +50,6 @@ public class TimeZoneTransition { * * @return The time zone rule used after the transition. */ - @libcore.api.IntraCoreApi @libcore.api.CorePlatformApi public TimeZoneRule getTo() { return to; @@ -63,7 +60,6 @@ public class TimeZoneTransition { * * @return The time zone rule used after the transition. */ - @libcore.api.IntraCoreApi @libcore.api.CorePlatformApi public TimeZoneRule getFrom() { return from; diff --git a/android_icu4j/testing/src/com/android/icu/test/util/ExtendedTimeZoneParameterizedTest.java b/android_icu4j/testing/src/com/android/icu/test/util/ExtendedTimeZoneParameterizedTest.java new file mode 100644 index 000000000..ce540fa84 --- /dev/null +++ b/android_icu4j/testing/src/com/android/icu/test/util/ExtendedTimeZoneParameterizedTest.java @@ -0,0 +1,190 @@ +/* + * Copyright (C) 2016 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.android.icu.test.util; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; +import static org.junit.Assert.assertNull; + +import android.icu.testsharding.MainTestShard; +import android.icu.util.BasicTimeZone; +import android.icu.util.TimeZone; +import android.icu.util.TimeZoneRule; +import android.icu.util.TimeZoneTransition; + +import com.android.icu.util.ExtendedTimeZone; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import java.time.Duration; +import java.time.Instant; +import java.time.LocalDateTime; +import java.time.Month; +import java.time.ZoneOffset; +import java.time.zone.ZoneOffsetTransition; +import java.time.zone.ZoneRules; + +/** + * Test the {@link ExtendedTimeZone}. + * + * This test verifies that the rules returned by that provider behave + * equivalently to the ICU rules from which they are created. + */ +@MainTestShard +@RunWith(Parameterized.class) +public class ExtendedTimeZoneParameterizedTest { + + @Parameterized.Parameters(name = "{0}") + public static String[] getZoneIds() { + String[] zones = TimeZone.getAvailableIDs(); + assertNotEquals("no zones returned", 0, zones.length); + return zones; + } + + private final String zoneId; + + public ExtendedTimeZoneParameterizedTest(final String zoneId) { + this.zoneId = zoneId; + } + + /** + * Verifies that ICU and java.time return the same transitions before and after a pre-selected + * set of instants in time. + */ + @Test + public void testTransitionsNearInstants() { + // An arbitrary set of instants at which to test the offsets in both implementations. + Instant[] instants = new Instant[] { + LocalDateTime.of(1900, Month.DECEMBER, 24, 12, 0).toInstant(ZoneOffset.UTC), + LocalDateTime.of(1940, Month.JULY, 1, 0, 0).toInstant(ZoneOffset.UTC), + LocalDateTime.of(1970, Month.JANUARY, 1, 2, 3).toInstant(ZoneOffset.UTC), + LocalDateTime.of(1980, Month.FEBRUARY, 4, 5, 6).toInstant(ZoneOffset.UTC), + LocalDateTime.of(1990, Month.MARCH, 7, 8, 9).toInstant(ZoneOffset.UTC), + LocalDateTime.of(2000, Month.APRIL, 10, 11, 12).toInstant(ZoneOffset.UTC), + LocalDateTime.of(2016, Month.MAY, 13, 14, 15).toInstant(ZoneOffset.UTC), + LocalDateTime.of(2020, Month.JUNE, 16, 17, 18).toInstant(ZoneOffset.UTC), + LocalDateTime.of(2100, Month.JULY, 19, 20, 21).toInstant(ZoneOffset.UTC), + // yes, adding "now" makes the test time-dependent, but it also ensures that future + // updates don't break on the then-current date. + Instant.now() + }; + // Coincidentally this test verifies that all zones can be converted to ZoneRules and + // don't violate any of the assumptions. + ZoneRules rules = ExtendedTimeZone.getInstance(zoneId).createZoneRules(); + BasicTimeZone timeZone = (BasicTimeZone) TimeZone.getTimeZone(zoneId); + + int[] icuOffsets = new int[2]; + for (Instant instant : instants) { + ZoneOffset offset = rules.getOffset(instant); + Duration daylightSavings = rules.getDaylightSavings(instant); + timeZone.getOffset(instant.toEpochMilli(), false, icuOffsets); + + assertEquals("total offset for " + zoneId + " at " + instant, + icuOffsets[1] + icuOffsets[0], offset.getTotalSeconds() * 1000); + assertEquals("dst offset for " + zoneId + " at " + instant, + icuOffsets[1], daylightSavings.toMillis()); + + ZoneOffsetTransition jtTrans; + TimeZoneTransition icuTrans; + + jtTrans = rules.nextTransition(instant); + icuTrans = timeZone.getNextTransition(instant.toEpochMilli(), false); + while (isIcuOnlyTransition(icuTrans)) { + icuTrans = timeZone.getNextTransition(icuTrans.getTime(), false); + } + assertEquivalent(icuTrans, jtTrans); + + jtTrans = rules.previousTransition(instant); + icuTrans = timeZone.getPreviousTransition(instant.toEpochMilli(), false); + // Find previous "real" transition. + while (isIcuOnlyTransition(icuTrans)) { + icuTrans = timeZone.getPreviousTransition(icuTrans.getTime(), false); + } + assertEquivalent(icuTrans, jtTrans); + } + } + + /** + * Verifies that ICU and java.time rules return the same transitions between 1900 and 2100. + */ + @Test + public void testAllTransitions() { + final Instant start = LocalDateTime.of(1900, Month.JANUARY, 1, 12, 0) + .toInstant(ZoneOffset.UTC); + // Many timezones have ongoing DST changes, so they would generate transitions endlessly. + // Pick a far-future end date to stop comparing in that case. + final Instant end = LocalDateTime.of(2100, Month.DECEMBER, 31, 12, 0) + .toInstant(ZoneOffset.UTC); + + ZoneRules rules = ExtendedTimeZone.getInstance(zoneId).createZoneRules(); + BasicTimeZone timeZone = (BasicTimeZone) TimeZone.getTimeZone(zoneId); + + Instant instant = start; + while (instant.isBefore(end)) { + ZoneOffsetTransition jtTrans; + TimeZoneTransition icuTrans; + + jtTrans = rules.nextTransition(instant); + icuTrans = timeZone.getNextTransition(instant.toEpochMilli(), false); + while (isIcuOnlyTransition(icuTrans)) { + icuTrans = timeZone.getNextTransition(icuTrans.getTime(), false); + } + assertEquivalent(icuTrans, jtTrans); + if (jtTrans == null) { + break; + } + instant = jtTrans.getInstant(); + } + } + + /** + * Returns {@code true} iff this transition will only be returned by ICU code. + * ICU reports "no-op" transitions where the raw offset and the dst savings + * change by the same absolute value in opposite directions, java.time doesn't + * return them, so find the next "real" transition. + */ + private static boolean isIcuOnlyTransition(TimeZoneTransition transition) { + if (transition == null) { + return false; + } + return transition.getFrom().getRawOffset() + transition.getFrom().getDSTSavings() + == transition.getTo().getRawOffset() + transition.getTo().getDSTSavings(); + } + + /** + * Asserts that the ICU {@link TimeZoneTransition} is equivalent to the java.time {@link + * ZoneOffsetTransition}. + */ + private static void assertEquivalent( + TimeZoneTransition icuTransition, ZoneOffsetTransition jtTransition) { + if (icuTransition == null) { + assertNull(jtTransition); + return; + } + assertEquals("time of transition", + Instant.ofEpochMilli(icuTransition.getTime()), jtTransition.getInstant()); + TimeZoneRule from = icuTransition.getFrom(); + TimeZoneRule to = icuTransition.getTo(); + assertEquals("offset before", + (from.getDSTSavings() + from.getRawOffset()) / 1000, + jtTransition.getOffsetBefore().getTotalSeconds()); + assertEquals("offset after", + (to.getDSTSavings() + to.getRawOffset()) / 1000, + jtTransition.getOffsetAfter().getTotalSeconds()); + } +} |