Initial conversion of Suggestions.java to Suggestions.kt in AOSP QuickSearchBox App using automatic conversion tool.
Test: built against Kotlin 1.7, did not compile successfully but still commiting to showcase git history of conversion
Change-Id: I04c6c6f56ddd40c7249c30ce42fc832d8b14cfbb
diff --git a/src/com/android/quicksearchbox/Suggestions.kt b/src/com/android/quicksearchbox/Suggestions.kt
index 7c89360..31089b8 100644
--- a/src/com/android/quicksearchbox/Suggestions.kt
+++ b/src/com/android/quicksearchbox/Suggestions.kt
@@ -13,181 +13,165 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+package com.android.quicksearchbox
-package com.android.quicksearchbox;
-
-import android.database.DataSetObservable;
-import android.database.DataSetObserver;
-import android.util.Log;
+import android.database.DataSetObservable
+import android.database.DataSetObserver
+import android.util.Log
/**
* Collects all corpus results for a single query.
*/
-public class Suggestions {
- private static final boolean DBG = false;
- private static final String TAG = "QSB.Suggestions";
-
- /** True if {@link Suggestions#close} has been called. */
- private boolean mClosed = false;
- protected final String mQuery;
+class Suggestions(val query: String, val source: Source) {
/**
* The observers that want notifications of changes to the published suggestions.
* This object may be accessed on any thread.
*/
- private final DataSetObservable mDataSetObservable = new DataSetObservable();
+ private val mDataSetObservable: DataSetObservable = DataSetObservable()
- private Source mSource;
+ /**
+ * True if [Suggestions.close] has been called.
+ */
+ var isClosed = false
+ private set
- private SourceResult mResult;
-
- private int mRefCount = 0;
-
- private boolean mDone = false;
-
- public Suggestions(String query, Source source) {
- mQuery = query;
- mSource = source;
+ /**
+ * Gets the list of corpus results reported so far. Do not modify or hang on to
+ * the returned iterator.
+ */
+ var webResult: SourceResult? = null
+ private set
+ get() = field
+ set
+ private var mRefCount = 0
+ private var mDone = false
+ fun acquire() {
+ mRefCount++
}
- public void acquire() {
- mRefCount++;
- }
-
- public void release() {
- mRefCount--;
+ fun release() {
+ mRefCount--
if (mRefCount <= 0) {
- close();
+ close()
}
}
- public Source getSource() {
- return mSource;
- }
-
/**
* Marks the suggestions set as complete, regardless of whether all corpora have
* returned.
*/
- public void done() {
- mDone = true;
+ fun done() {
+ mDone = true
}
/**
* Checks whether all sources have reported.
* Must be called on the UI thread, or before this object is seen by the UI thread.
*/
- public boolean isDone() {
- return mDone || mResult != null;
- }
+ val isDone: Boolean
+ get() = mDone || webResult != null
/**
* Adds a list of corpus results. Must be called on the UI thread, or before this
* object is seen by the UI thread.
*/
- public void addResults(SourceResult result) {
- if (isClosed()) {
- result.close();
- return;
+ fun addResults(result: SourceResult) {
+ if (isClosed) {
+ result.close()
+ return
}
-
- if (DBG) {
- Log.d(TAG, "addResults["+ hashCode() + "] source:" +
- result.getSource().getName() + " results:" + result.getCount());
+ if (Suggestions.Companion.DBG) {
+ Log.d(
+ Suggestions.Companion.TAG, "addResults[" + hashCode().toString() + "] source:" +
+ result.getSource().getName().toString() + " results:" + result.getCount()
+ )
}
- if (!mQuery.equals(result.getUserQuery())) {
- throw new IllegalArgumentException("Got result for wrong query: "
- + mQuery + " != " + result.getUserQuery());
+ if (!query.equals(result.getUserQuery())) {
+ throw IllegalArgumentException(
+ "Got result for wrong query: "
+ + query + " != " + result.getUserQuery()
+ )
}
- mResult = result;
- notifyDataSetChanged();
+ webResult = result
+ notifyDataSetChanged()
}
/**
* Registers an observer that will be notified when the reported results or
* the done status changes.
*/
- public void registerDataSetObserver(DataSetObserver observer) {
- if (mClosed) {
- throw new IllegalStateException("registerDataSetObserver() when closed");
+ fun registerDataSetObserver(observer: DataSetObserver?) {
+ if (isClosed) {
+ throw IllegalStateException("registerDataSetObserver() when closed")
}
- mDataSetObservable.registerObserver(observer);
+ mDataSetObservable.registerObserver(observer)
}
-
/**
* Unregisters an observer.
*/
- public void unregisterDataSetObserver(DataSetObserver observer) {
- mDataSetObservable.unregisterObserver(observer);
+ fun unregisterDataSetObserver(observer: DataSetObserver?) {
+ mDataSetObservable.unregisterObserver(observer)
}
/**
- * Calls {@link DataSetObserver#onChanged()} on all observers.
+ * Calls [DataSetObserver.onChanged] on all observers.
*/
- protected void notifyDataSetChanged() {
- if (DBG) Log.d(TAG, "notifyDataSetChanged()");
- mDataSetObservable.notifyChanged();
+ protected fun notifyDataSetChanged() {
+ if (Suggestions.Companion.DBG) Log.d(Suggestions.Companion.TAG, "notifyDataSetChanged()")
+ mDataSetObservable.notifyChanged()
}
/**
* Closes all the source results and unregisters all observers.
*/
- private void close() {
- if (DBG) Log.d(TAG, "close() [" + hashCode() + "]");
- if (mClosed) {
- throw new IllegalStateException("Double close()");
+ private fun close() {
+ if (Suggestions.Companion.DBG) Log.d(
+ Suggestions.Companion.TAG,
+ "close() [" + hashCode().toString() + "]"
+ )
+ if (isClosed) {
+ throw IllegalStateException("Double close()")
}
- mClosed = true;
- mDataSetObservable.unregisterAll();
- if (mResult != null) {
- mResult.close();
+ isClosed = true
+ mDataSetObservable.unregisterAll()
+ if (webResult != null) {
+ webResult!!.close()
}
- mResult = null;
- }
-
- public boolean isClosed() {
- return mClosed;
+ webResult = null
}
@Override
- protected void finalize() {
- if (!mClosed) {
- Log.e(TAG, "LEAK! Finalized without being closed: Suggestions[" + getQuery() + "]");
+ protected fun finalize() {
+ if (!isClosed) {
+ Log.e(
+ Suggestions.Companion.TAG,
+ "LEAK! Finalized without being closed: Suggestions[$query]"
+ )
}
}
- public String getQuery() {
- return mQuery;
- }
-
- /**
- * Gets the list of corpus results reported so far. Do not modify or hang on to
- * the returned iterator.
- */
- public SourceResult getResult() {
- return mResult;
- }
-
- public SourceResult getWebResult() {
- return mResult;
- }
-
/**
* Gets the number of source results.
* Must be called on the UI thread, or before this object is seen by the UI thread.
*/
- public int getResultCount() {
- if (isClosed()) {
- throw new IllegalStateException("Called getSourceCount() when closed.");
+ val resultCount: Int
+ get() {
+ if (isClosed) {
+ throw IllegalStateException("Called getSourceCount() when closed.")
+ }
+ return if (webResult == null) 0 else webResult.getCount()
}
- return mResult == null ? 0 : mResult.getCount();
- }
@Override
- public String toString() {
- return "Suggestions@" + hashCode() + "{source=" + mSource
- + ",getResultCount()=" + getResultCount() + "}";
+ override fun toString(): String {
+ return "Suggestions@" + hashCode().toString() + "{source=" + source
+ .toString() + ",getResultCount()=" + resultCount.toString() + "}"
}
-}
+ companion object {
+ private const val DBG = false
+ private const val TAG = "QSB.Suggestions"
+ }
+}
\ No newline at end of file