/* GENERATED SOURCE. DO NOT MODIFY. */ // © 2016 and later: Unicode, Inc. and others. // License & terms of use: http://www.unicode.org/copyright.html /* ******************************************************************************* * Copyright (C) 2013-2014, International Business Machines * Corporation and others. All Rights Reserved. ******************************************************************************* * SharedObject.java, ported from sharedobject.h/.cpp * * C++ version created on: 2013dec19 * created by: Markus W. Scherer */ package android.icu.impl.coll; import java.util.concurrent.atomic.AtomicInteger; import android.icu.util.ICUCloneNotSupportedException; /** * Base class for shared, reference-counted, auto-deleted objects. * Java subclasses are mutable and must implement clone(). * *
In C++, the SharedObject base class is used for both memory and ownership management. * In Java, memory management (deletion after last reference is gone) * is up to the garbage collector, * but the reference counter is still used to see whether the referent is the sole owner. * *
Usage: *
* class S extends SharedObject { * public clone() { ... } * } * * // Either use the nest class Reference (which costs an extra allocation), * // or duplicate its code in the class that uses S * // (which duplicates code and is more error-prone). * class U { * // For read-only access, use s.readOnly(). * // For writable access, use S ownedS = s.copyOnWrite(); * private SharedObject.Reference<S> s; * // Returns a writable version of s. * // If there is exactly one owner, then s itself is returned. * // If there are multiple owners, then s is replaced with a clone, * // and that is returned. * private S getOwnedS() { * return s.copyOnWrite(); * } * public U clone() { * ... * c.s = s.clone(); * ... * } * } * * class V { * // For read-only access, use s directly. * // For writable access, use S ownedS = getOwnedS(); * private S s; * // Returns a writable version of s. * // If there is exactly one owner, then s itself is returned. * // If there are multiple owners, then s is replaced with a clone, * // and that is returned. * private S getOwnedS() { * if(s.getRefCount() > 1) { * S ownedS = s.clone(); * s.removeRef(); * s = ownedS; * ownedS.addRef(); * } * return s; * } * public U clone() { * ... * s.addRef(); * ... * } * protected void finalize() { * ... * if(s != null) { * s.removeRef(); * s = null; * } * ... * } * } ** * Either use only Java memory management, or use addRef()/removeRef(). * Sharing requires reference-counting. * * TODO: Consider making this more widely available inside ICU, * or else adopting a different model. * @hide Only a subset of ICU is exposed in Android */ public class SharedObject implements Cloneable { /** * Similar to a smart pointer, basically a port of the static methods of C++ SharedObject. * @hide Only a subset of ICU is exposed in Android */ public static final class Reference