Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c42363ad authored by Roozbeh Pournader's avatar Roozbeh Pournader
Browse files

Avoid creating unnecessary builder objects in BidiFormatter.

Previously, BidiFormatter.getInstance() created an unnecessary
pass-through builder object, which was used to return a static
already existing BidiFormatter.

Now, we just return the same static BidiFormatter, without going
through a builder, so we can save some extra allocations.

Bug: 10022222

Change-Id: Ibcb6aac1c1e16d9ec00e93824fa063f8dccdf8cb
parent 0f9f0189
Loading
Loading
Loading
Loading
+14 −8
Original line number Diff line number Diff line
@@ -196,17 +196,13 @@ public final class BidiFormatter {
            return this;
        }

        private static BidiFormatter getDefaultInstanceFromContext(boolean isRtlContext) {
            return isRtlContext ? DEFAULT_RTL_INSTANCE : DEFAULT_LTR_INSTANCE;
        }

        /**
         * @return A BidiFormatter with the specified options.
         */
        public BidiFormatter build() {
            if (mFlags == DEFAULT_FLAGS &&
                    mTextDirectionHeuristic == DEFAULT_TEXT_DIRECTION_HEURISTIC) {
                return getDefaultInstanceFromContext(mIsRtlContext);
                return BidiFormatter.getDefaultInstanceFromContext(mIsRtlContext);
            }
            return new BidiFormatter(mIsRtlContext, mFlags, mTextDirectionHeuristic);
        }
@@ -233,27 +229,33 @@ public final class BidiFormatter {
    /**
     * Factory for creating an instance of BidiFormatter for the default locale directionality.
     *
     * This does not create any new objects, and returns already existing static instances.
     *
     */
    public static BidiFormatter getInstance() {
        return new Builder().build();
        return getDefaultInstanceFromContext(isRtlLocale(Locale.getDefault()));
    }

    /**
     * Factory for creating an instance of BidiFormatter given the context directionality.
     *
     * This does not create any new objects, and returns already existing static instances.
     *
     * @param rtlContext Whether the context directionality is RTL.
     */
    public static BidiFormatter getInstance(boolean rtlContext) {
        return new Builder(rtlContext).build();
        return getDefaultInstanceFromContext(rtlContext);
    }

    /**
     * Factory for creating an instance of BidiFormatter given the context locale.
     *
     * This does not create any new objects, and returns already existing static instances.
     *
     * @param locale The context locale.
     */
    public static BidiFormatter getInstance(Locale locale) {
        return new Builder(locale).build();
        return getDefaultInstanceFromContext(isRtlLocale(locale));
    }

    /**
@@ -440,6 +442,10 @@ public final class BidiFormatter {
        return unicodeWrap(str, mDefaultTextDirectionHeuristic, true /* isolate */);
    }

    private static BidiFormatter getDefaultInstanceFromContext(boolean isRtlContext) {
        return isRtlContext ? DEFAULT_RTL_INSTANCE : DEFAULT_LTR_INSTANCE;
    }

    /**
     * Helper method to return true if the Locale directionality is RTL.
     *