提交 d5d927d2 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov

Add ValueCollectionBase as base class for ValueArray and ValueRow

上级 245aa770
......@@ -10,14 +10,12 @@ import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import org.h2.engine.Constants;
import org.h2.engine.SysProperties;
import org.h2.util.MathUtils;
/**
* Implementation of the ARRAY data type.
*/
public class ValueArray extends Value {
public class ValueArray extends ValueCollectionBase {
/**
* Empty array.
......@@ -25,12 +23,10 @@ public class ValueArray extends Value {
private static final Object EMPTY = get(new Value[0]);
private final Class<?> componentType;
private final Value[] values;
private int hash;
private ValueArray(Class<?> componentType, Value[] list) {
super(list);
this.componentType = componentType;
this.values = list;
}
/**
......@@ -65,19 +61,6 @@ public class ValueArray extends Value {
return (ValueArray) EMPTY;
}
@Override
public int hashCode() {
if (hash != 0) {
return hash;
}
int h = 1;
for (Value v : values) {
h = h * 31 + v.hashCode();
}
hash = h;
return h;
}
public Value[] getList() {
return values;
}
......@@ -91,15 +74,6 @@ public class ValueArray extends Value {
return componentType;
}
@Override
public long getPrecision() {
long p = 0;
for (Value v : values) {
p += v.getPrecision();
}
return p;
}
@Override
public String getString() {
StringBuilder builder = new StringBuilder().append('[');
......@@ -181,15 +155,6 @@ public class ValueArray extends Value {
return builder.append(']').toString();
}
@Override
public int getDisplaySize() {
long size = 0;
for (Value v : values) {
size += v.getDisplaySize();
}
return MathUtils.convertLongToInt(size);
}
@Override
public boolean equals(Object other) {
if (!(other instanceof ValueArray)) {
......@@ -211,15 +176,6 @@ public class ValueArray extends Value {
return true;
}
@Override
public int getMemory() {
int memory = 32;
for (Value v : values) {
memory += v.getMemory() + Constants.MEMORY_POINTER;
}
return memory;
}
@Override
public Value convertPrecision(long precision, boolean force) {
if (!force) {
......
/*
* Copyright 2004-2018 H2 Group. Multiple-Licensed under the MPL 2.0,
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.value;
import org.h2.engine.Constants;
import org.h2.util.MathUtils;
/**
* Base class for collection values.
*/
abstract class ValueCollectionBase extends Value {
final Value[] values;
private int hash;
ValueCollectionBase(Value[] values) {
this.values = values;
}
@Override
public int hashCode() {
if (hash != 0) {
return hash;
}
int h = getType();
for (Value v : values) {
h = h * 31 + v.hashCode();
}
hash = h;
return h;
}
@Override
public long getPrecision() {
long p = 0;
for (Value v : values) {
p += v.getPrecision();
}
return p;
}
@Override
public int getDisplaySize() {
long size = 0;
for (Value v : values) {
size += v.getDisplaySize();
}
return MathUtils.convertLongToInt(size);
}
@Override
public int getMemory() {
int memory = 32;
for (Value v : values) {
memory += v.getMemory() + Constants.MEMORY_POINTER;
}
return memory;
}
}
......@@ -10,26 +10,21 @@ import java.sql.SQLException;
import java.util.Arrays;
import org.h2.api.ErrorCode;
import org.h2.engine.Constants;
import org.h2.engine.SysProperties;
import org.h2.message.DbException;
import org.h2.util.MathUtils;
/**
* Row value.
*/
public class ValueRow extends Value {
public class ValueRow extends ValueCollectionBase {
/**
* Empty row.
*/
private static final Object EMPTY = get(new Value[0]);
private final Value[] values;
private int hash;
private ValueRow(Value[] list) {
this.values = list;
super(list);
}
/**
......@@ -52,19 +47,6 @@ public class ValueRow extends Value {
return (ValueRow) EMPTY;
}
@Override
public int hashCode() {
if (hash != 0) {
return hash;
}
int h = 1;
for (Value v : values) {
h = h * 31 + v.hashCode();
}
hash = h;
return h;
}
public Value[] getList() {
return values;
}
......@@ -74,15 +56,6 @@ public class ValueRow extends Value {
return Value.ROW;
}
@Override
public long getPrecision() {
long p = 0;
for (Value v : values) {
p += v.getPrecision();
}
return p;
}
@Override
public String getString() {
StringBuilder builder = new StringBuilder("ROW (");
......@@ -165,15 +138,6 @@ public class ValueRow extends Value {
return builder.append(')').toString();
}
@Override
public int getDisplaySize() {
long size = 0;
for (Value v : values) {
size += v.getDisplaySize();
}
return MathUtils.convertLongToInt(size);
}
@Override
public boolean equals(Object other) {
if (!(other instanceof ValueRow)) {
......@@ -195,15 +159,6 @@ public class ValueRow extends Value {
return true;
}
@Override
public int getMemory() {
int memory = 32;
for (Value v : values) {
memory += v.getMemory() + Constants.MEMORY_POINTER;
}
return memory;
}
@Override
public Value convertPrecision(long precision, boolean force) {
if (!force) {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论