1 | /* |
2 | * Copyright 2004-2014 H2 Group. Multiple-Licensed under the MPL 2.0, |
3 | * and the EPL 1.0 (http://h2database.com/html/license.html). |
4 | * Initial Developer: H2 Group |
5 | */ |
6 | package org.h2.mvstore.rtree; |
7 | |
8 | import java.util.Arrays; |
9 | |
10 | /** |
11 | * A unique spatial key. |
12 | */ |
13 | public class SpatialKey { |
14 | |
15 | private final long id; |
16 | private final float[] minMax; |
17 | |
18 | /** |
19 | * Create a new key. |
20 | * |
21 | * @param id the id |
22 | * @param minMax min x, max x, min y, max y, and so on |
23 | */ |
24 | public SpatialKey(long id, float... minMax) { |
25 | this.id = id; |
26 | this.minMax = minMax; |
27 | } |
28 | |
29 | /** |
30 | * Get the minimum value for the given dimension. |
31 | * |
32 | * @param dim the dimension |
33 | * @return the value |
34 | */ |
35 | public float min(int dim) { |
36 | return minMax[dim + dim]; |
37 | } |
38 | |
39 | /** |
40 | * Set the minimum value for the given dimension. |
41 | * |
42 | * @param dim the dimension |
43 | * @param x the value |
44 | */ |
45 | public void setMin(int dim, float x) { |
46 | minMax[dim + dim] = x; |
47 | } |
48 | |
49 | /** |
50 | * Get the maximum value for the given dimension. |
51 | * |
52 | * @param dim the dimension |
53 | * @return the value |
54 | */ |
55 | public float max(int dim) { |
56 | return minMax[dim + dim + 1]; |
57 | } |
58 | |
59 | /** |
60 | * Set the maximum value for the given dimension. |
61 | * |
62 | * @param dim the dimension |
63 | * @param x the value |
64 | */ |
65 | public void setMax(int dim, float x) { |
66 | minMax[dim + dim + 1] = x; |
67 | } |
68 | |
69 | public long getId() { |
70 | return id; |
71 | } |
72 | |
73 | @Override |
74 | public String toString() { |
75 | StringBuilder buff = new StringBuilder(); |
76 | buff.append(id).append(": ("); |
77 | for (int i = 0; i < minMax.length; i += 2) { |
78 | if (i > 0) { |
79 | buff.append(", "); |
80 | } |
81 | buff.append(minMax[i]).append('/').append(minMax[i + 1]); |
82 | } |
83 | return buff.append(")").toString(); |
84 | } |
85 | |
86 | @Override |
87 | public int hashCode() { |
88 | return (int) ((id >>> 32) ^ id); |
89 | } |
90 | |
91 | @Override |
92 | public boolean equals(Object other) { |
93 | if (other == this) { |
94 | return true; |
95 | } else if (!(other instanceof SpatialKey)) { |
96 | return false; |
97 | } |
98 | SpatialKey o = (SpatialKey) other; |
99 | if (id != o.id) { |
100 | return false; |
101 | } |
102 | return equalsIgnoringId(o); |
103 | } |
104 | |
105 | /** |
106 | * Check whether two objects are equals, but do not compare the id fields. |
107 | * |
108 | * @param o the other key |
109 | * @return true if the contents are the same |
110 | */ |
111 | public boolean equalsIgnoringId(SpatialKey o) { |
112 | return Arrays.equals(minMax, o.minMax); |
113 | } |
114 | |
115 | } |