| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.mvstore.db; |
| 7 |
|
| 8 |
import java.util.Arrays; |
| 9 |
import org.h2.engine.CastDataProvider; |
| 10 |
import org.h2.mvstore.rtree.Spatial; |
| 11 |
import org.h2.value.CompareMode; |
| 12 |
import org.h2.value.TypeInfo; |
| 13 |
import org.h2.value.Value; |
| 14 |
|
| 15 |
/** |
| 16 |
* A unique spatial key. |
| 17 |
*/ |
| 18 |
public class SpatialKey extends Value implements Spatial { |
| 19 |
|
| 20 |
private final long id; |
| 21 |
private final float[] minMax; |
| 22 |
|
| 23 |
/** |
| 24 |
* Create a new key. |
| 25 |
* |
| 26 |
* @param id the id |
| 27 |
* @param minMax min x, max x, min y, max y, and so on |
| 28 |
*/ |
| 29 |
public SpatialKey(long id, float... minMax) { |
| 30 |
this.id = id; |
| 31 |
this.minMax = minMax; |
| 32 |
} |
| 33 |
|
| 34 |
public SpatialKey(long id, SpatialKey other) { |
| 35 |
this.id = id; |
| 36 |
this.minMax = other.minMax.clone(); |
| 37 |
} |
| 38 |
|
| 39 |
@Override |
| 40 |
public float min(int dim) { |
| 41 |
return minMax[dim + dim]; |
| 42 |
} |
| 43 |
|
| 44 |
@Override |
| 45 |
public void setMin(int dim, float x) { |
| 46 |
minMax[dim + dim] = x; |
| 47 |
} |
| 48 |
|
| 49 |
@Override |
| 50 |
public float max(int dim) { |
| 51 |
return minMax[dim + dim + 1]; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public void setMax(int dim, float x) { |
| 56 |
minMax[dim + dim + 1] = x; |
| 57 |
} |
| 58 |
|
| 59 |
@Override |
| 60 |
public Spatial clone(long id) { |
| 61 |
return new SpatialKey(id, this); |
| 62 |
} |
| 63 |
|
| 64 |
@Override |
| 65 |
public long getId() { |
| 66 |
return id; |
| 67 |
} |
| 68 |
|
| 69 |
@Override |
| 70 |
public boolean isNull() { |
| 71 |
return minMax.length == 0; |
| 72 |
} |
| 73 |
|
| 74 |
@Override |
| 75 |
public String toString() { |
| 76 |
return getString(); |
| 77 |
} |
| 78 |
|
| 79 |
@Override |
| 80 |
public int hashCode() { |
| 81 |
return (int) ((id >>> 32) ^ id); |
| 82 |
} |
| 83 |
|
| 84 |
@Override |
| 85 |
public boolean equals(Object other) { |
| 86 |
if (other == this) { |
| 87 |
return true; |
| 88 |
} else if (!(other instanceof SpatialKey)) { |
| 89 |
return false; |
| 90 |
} |
| 91 |
SpatialKey o = (SpatialKey) other; |
| 92 |
if (id != o.id) { |
| 93 |
return false; |
| 94 |
} |
| 95 |
return equalsIgnoringId(o); |
| 96 |
} |
| 97 |
|
| 98 |
@Override |
| 99 |
public int compareTypeSafe(Value v, CompareMode mode, CastDataProvider provider) { |
| 100 |
throw new UnsupportedOperationException(); |
| 101 |
// return 0; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Check whether two objects are equals, but do not compare the id fields. |
| 106 |
* |
| 107 |
* @param o the other key |
| 108 |
* @return true if the contents are the same |
| 109 |
*/ |
| 110 |
@Override |
| 111 |
public boolean equalsIgnoringId(Spatial o) { |
| 112 |
return Arrays.equals(minMax, ((SpatialKey)o).minMax); |
| 113 |
} |
| 114 |
|
| 115 |
@Override |
| 116 |
public StringBuilder getSQL(StringBuilder builder, int sqlFlags) { |
| 117 |
builder.append(id).append(": ("); |
| 118 |
for (int i = 0; i < minMax.length; i += 2) { |
| 119 |
if (i > 0) { |
| 120 |
builder.append(", "); |
| 121 |
} |
| 122 |
builder.append(minMax[i]).append('/').append(minMax[i + 1]); |
| 123 |
} |
| 124 |
builder.append(")"); |
| 125 |
return builder; |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public TypeInfo getType() { |
| 130 |
return TypeInfo.TYPE_GEOMETRY; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public int getValueType() { |
| 135 |
return Value.GEOMETRY; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public String getString() { |
| 140 |
return getTraceSQL(); |
| 141 |
} |
| 142 |
|
| 143 |
} |