
com.couchbase.lite.Result Maven / Gradle / Ivy
//
// Copyright (c) 2020, 2017 Couchbase, Inc All rights reserved.
//
// 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.couchbase.lite;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.couchbase.lite.internal.DbContext;
import com.couchbase.lite.internal.core.C4QueryEnumerator;
import com.couchbase.lite.internal.fleece.FLArrayIterator;
import com.couchbase.lite.internal.fleece.FLValue;
import com.couchbase.lite.internal.fleece.MRoot;
import com.couchbase.lite.internal.utils.DateUtils;
import com.couchbase.lite.internal.utils.Preconditions;
/**
* Result represents a row of result set returned by a Query.
*/
@SuppressWarnings("PMD.GodClass")
public final class Result implements ArrayInterface, DictionaryInterface, Iterable {
//---------------------------------------------
// member variables
//---------------------------------------------
private final ResultSet rs;
private final List values;
private final long missingColumns;
private final DbContext context;
//---------------------------------------------
// constructors
//---------------------------------------------
Result(ResultSet rs, C4QueryEnumerator c4enum, DbContext context) {
this.rs = rs;
this.values = extractColumns(c4enum.getColumns());
this.missingColumns = c4enum.getMissingColumns();
this.context = context;
}
//---------------------------------------------
// API - public methods
//---------------------------------------------
//---------------------------------------------
// implementation of common between ReadOnlyArrayInterface and ReadOnlyDictionaryInterface
//--------------------------------------------
/**
* Return A number of the projecting values in the result.
*/
@Override
public int count() { return rs.getColumnCount(); }
//---------------------------------------------
// implementation of ReadOnlyArrayInterface
//---------------------------------------------
/**
* The projecting result value at the given index.
*
* @param index The select result index as a Object.
* @return The value.
*/
@Override
public Object getValue(int index) {
checkBounds(index);
return fleeceValueToObject(index);
}
/**
* The projecting result value at the given index as a String object
*
* @param index The select result index.
* @return The String object.
*/
@Override
public String getString(int index) {
checkBounds(index);
final Object obj = fleeceValueToObject(index);
return obj instanceof String ? (String) obj : null;
}
/**
* The projecting result value at the given index as a Number object
*
* @param index The select result index.
* @return The Number object.
*/
@Override
public Number getNumber(int index) {
checkBounds(index);
return CBLConverter.asNumber(fleeceValueToObject(index));
}
/**
* The projecting result value at the given index as a integer value
*
* @param index The select result index.
* @return The integer value.
*/
@Override
public int getInt(int index) {
checkBounds(index);
final FLValue flValue = values.get(index);
return flValue != null ? (int) flValue.asInt() : 0;
}
/**
* The projecting result value at the given index as a long value
*
* @param index The select result index.
* @return The long value.
*/
@Override
public long getLong(int index) {
checkBounds(index);
final FLValue flValue = values.get(index);
return flValue != null ? flValue.asInt() : 0L;
}
/**
* The projecting result value at the given index as a float value
*
* @param index The select result index.
* @return The float value.
*/
@Override
public float getFloat(int index) {
checkBounds(index);
final FLValue flValue = values.get(index);
return flValue != null ? flValue.asFloat() : 0.0F;
}
/**
* The projecting result value at the given index as a double value
*
* @param index The select result index.
* @return The double value.
*/
@Override
public double getDouble(int index) {
checkBounds(index);
final FLValue flValue = values.get(index);
return flValue != null ? flValue.asDouble() : 0.0;
}
/**
* The projecting result value at the given index as a boolean value
*
* @param index The select result index.
* @return The boolean value.
*/
@Override
public boolean getBoolean(int index) {
checkBounds(index);
final FLValue flValue = values.get(index);
return flValue != null && flValue.asBool();
}
/**
* The projecting result value at the given index as a Blob object
*
* @param index The select result index.
* @return The Blob object.
*/
@Override
public Blob getBlob(int index) {
checkBounds(index);
final Object obj = fleeceValueToObject(index);
return obj instanceof Blob ? (Blob) obj : null;
}
/**
* The projecting result value at the given index as an Array object
*
* @param index The select result index.
* @return The object.
*/
@Override
public Date getDate(int index) {
checkBounds(index);
return DateUtils.fromJson(getString(index));
}
/**
* The projecting result value at the given index as a Array object
*
* @param index The select result index.
* @return The object.
*/
@Override
public Array getArray(int index) {
checkBounds(index);
final Object obj = fleeceValueToObject(index);
return obj instanceof Array ? (Array) obj : null;
}
/**
* The projecting result value at the given index as a Dictionary object
*
* @param index The select result index.
* @return The object.
*/
@Override
public Dictionary getDictionary(int index) {
checkBounds(index);
final Object obj = fleeceValueToObject(index);
return obj instanceof Dictionary ? (Dictionary) obj : null;
}
/**
* Gets all values as an List. The value types of the values contained
* in the returned List object are Array, Blob, Dictionary, Number types, String, and null.
*
* @return The List representing all values.
*/
@NonNull
@Override
public List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy