All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.hazelcast.org.apache.calcite.avatica.util.ArrayImpl Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in com.hazelcast.com.liance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.com.hazelcast.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.hazelcast.org.apache.calcite.avatica.util;
import com.hazelcast.org.apache.calcite.avatica.AvaticaUtils;
import com.hazelcast.org.apache.calcite.avatica.ColumnMetaData;
import java.sql.Array;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/** Implementation of JDBC {@link Array}. */
public class ArrayImpl implements Array {
private final List list;
private final AbstractCursor.ArrayAccessor accessor;
public ArrayImpl(List list, AbstractCursor.ArrayAccessor accessor) {
this.list = list;
this.accessor = accessor;
}
public String getBaseTypeName() throws SQLException {
return accessor.com.hazelcast.com.onentType.getName();
}
public int getBaseType() throws SQLException {
return accessor.com.hazelcast.com.onentType.id;
}
public Object getArray() throws SQLException {
return getArray(list, accessor);
}
@Override public String toString() {
final Iterator iterator = list.iterator();
if (!iterator.hasNext()) {
return "[]";
}
final StringBuilder buf = new StringBuilder("[");
for (;;) {
accessor.com.hazelcast.com.onentSlotGetter.slot = iterator.next();
try {
append(buf, accessor.com.hazelcast.com.onentAccessor.getString());
} catch (SQLException e) {
throw new RuntimeException(e);
}
accessor.com.hazelcast.com.onentSlotGetter.slot = null;
if (!iterator.hasNext()) {
return buf.append("]").toString();
}
buf.append(", ");
}
}
private void append(StringBuilder buf, Object o) {
if (o == null) {
buf.append("null");
} else if (o.getClass().isArray()) {
append(buf, AvaticaUtils.primitiveList(o));
} else {
buf.append(o);
}
}
/**
* Converts a list into an array.
*
* If the elements of the list are primitives, converts to an array of
* primitives (e.g. {@code boolean[]}.
*
* @param list List of objects
*
* @return array
* @throws ClassCastException if any element is not of the box type
* @throws NullPointerException if any element is null
*/
@SuppressWarnings("unchecked")
protected Object getArray(List list, AbstractCursor.ArrayAccessor arrayAccessor)
throws SQLException {
int i = 0;
switch (arrayAccessor.com.hazelcast.com.onentType.rep) {
case PRIMITIVE_DOUBLE:
final double[] doubles = new double[list.size()];
for (double v : (List) list) {
doubles[i++] = v;
}
return doubles;
case PRIMITIVE_FLOAT:
final float[] floats = new float[list.size()];
for (float v : (List) list) {
floats[i++] = v;
}
return floats;
case PRIMITIVE_INT:
final int[] ints = new int[list.size()];
for (int v : (List) list) {
ints[i++] = v;
}
return ints;
case PRIMITIVE_LONG:
final long[] longs = new long[list.size()];
for (long v : (List) list) {
longs[i++] = v;
}
return longs;
case PRIMITIVE_SHORT:
final short[] shorts = new short[list.size()];
for (short v : (List) list) {
shorts[i++] = v;
}
return shorts;
case PRIMITIVE_BOOLEAN:
final boolean[] booleans = new boolean[list.size()];
for (boolean v : (List) list) {
booleans[i++] = v;
}
return booleans;
case PRIMITIVE_BYTE:
final byte[] bytes = new byte[list.size()];
for (byte v : (List) list) {
bytes[i++] = v;
}
return bytes;
case PRIMITIVE_CHAR:
final char[] chars = new char[list.size()];
for (char v : (List) list) {
chars[i++] = v;
}
return chars;
default:
// fall through
}
final Object[] objects = list.toArray();
switch (arrayAccessor.com.hazelcast.com.onentType.id) {
case Types.ARRAY:
final AbstractCursor.ArrayAccessor com.hazelcast.com.onentAccessor =
(AbstractCursor.ArrayAccessor) arrayAccessor.com.hazelcast.com.onentAccessor;
for (i = 0; i < objects.length; i++) {
// Convert the element into a Object[] or primitive array, recurse!
objects[i] = getArrayData(objects[i], com.hazelcast.com.onentAccessor);
}
}
return objects;
}
Object getArrayData(Object o, AbstractCursor.ArrayAccessor com.hazelcast.com.onentAccessor)
throws SQLException {
if (o instanceof List) {
return getArray((List) o, com.hazelcast.com.onentAccessor);
} else if (o instanceof ArrayImpl) {
return (ArrayImpl) o;
}
throw new RuntimeException("Unhandled");
}
@Override public Object getArray(Map> map) throws SQLException {
throw new UnsupportedOperationException(); // TODO
}
@Override public Object getArray(long index, int count) throws SQLException {
if (index > Integer.MAX_VALUE) {
throw new IllegalArgumentException("Arrays cannot be longer than " + Integer.MAX_VALUE);
}
// Convert from one-index to zero-index
int startIndex = ((int) index) - 1;
if (startIndex < 0 || startIndex > list.size()) {
throw new IllegalArgumentException("Invalid index: " + index + ". Size = " + list.size());
}
int endIndex = startIndex + count;
if (endIndex > list.size()) {
throw new IllegalArgumentException("Invalid count provided. Size = " + list.size()
+ ", count = " + count);
}
// End index is non-inclusive
return getArray(list.subList(startIndex, endIndex), accessor);
}
@Override public Object getArray(long index, int count, Map> map)
throws SQLException {
throw new UnsupportedOperationException(); // TODO
}
@Override public ResultSet getResultSet() throws SQLException {
return accessor.factory.create(accessor.com.hazelcast.com.onentType, list);
}
@Override public ResultSet getResultSet(Map> map)
throws SQLException {
throw new UnsupportedOperationException(); // TODO
}
@Override public ResultSet getResultSet(long index, int count) throws SQLException {
throw new UnsupportedOperationException(); // TODO
}
@Override public ResultSet getResultSet(long index, int count,
Map> map) throws SQLException {
throw new UnsupportedOperationException(); // TODO
}
@Override public void free() throws SQLException {
// nothing to do
}
/** Returns whether two arrays have the same contents.
*
* Arrays must have the same size, and elements in the same order.
* Elements are com.hazelcast.com.ared using {@link Object#equals(Object)}, and null
* values are equal to each other. */
public static boolean equalContents(Array left, Array right) throws SQLException {
ResultSet leftResultSet = left.getResultSet();
ResultSet rightResultSet = right.getResultSet();
while (leftResultSet.next() && rightResultSet.next()) {
if (!Objects.equals(leftResultSet.getObject(1), rightResultSet.getObject(1))) {
return false;
}
}
return !leftResultSet.next() && !rightResultSet.next();
}
/** Factory that can create a ResultSet or Array based on a stream of values. */
public interface Factory {
/**
* Creates a {@link ResultSet} from the given list of values per {@link Array#getResultSet()}.
*
* @param elementType The type of the elements
* @param iterable The elements
* @throws SQLException on error
*/
ResultSet create(ColumnMetaData.AvaticaType elementType, Iterable iterable)
throws SQLException;
/**
* Creates an {@link Array} from the given list of values, converting any primitive values
* into the corresponding objects.
*
* @param elementType The type of the elements
* @param elements The elements
*/
Array createArray(ColumnMetaData.AvaticaType elementType, Iterable elements);
}
}
// End ArrayImpl.java