134 changed files with 961 additions and 2032 deletions
@ -0,0 +1,49 @@ |
|||
/** |
|||
* Copyright © 2016-2017 The Thingsboard Authors |
|||
* |
|||
* 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 org.thingsboard.server.common.data; |
|||
|
|||
import java.util.List; |
|||
import java.util.UUID; |
|||
import java.util.stream.Collectors; |
|||
|
|||
/** |
|||
* Created by ashvayka on 13.07.17. |
|||
*/ |
|||
public class UUIDConverter { |
|||
|
|||
public static UUID fromString(String src) { |
|||
return UUID.fromString(src.substring(7, 15) + "-" + src.substring(3, 7) + "-1" |
|||
+ src.substring(0, 3) + "-" + src.substring(15, 19) + "-" + src.substring(19)); |
|||
} |
|||
|
|||
public static String fromTimeUUID(UUID src) { |
|||
if (src.version() != 1) { |
|||
throw new IllegalArgumentException("Not a time UUID!"); |
|||
} |
|||
String str = src.toString(); |
|||
// 58e0a7d7-eebc-11d8-9669-0800200c9a66 => 1d8eebc58e0a7d796690800200c9a66. Note that [11d8] -> [1d8]
|
|||
return str.substring(15, 18) + str.substring(9, 13) + str.substring(0, 8) + str.substring(19, 23) + str.substring(24); |
|||
} |
|||
|
|||
public static List<String> fromTimeUUIDs(List<UUID> uuids) { |
|||
if (uuids == null) { |
|||
return null; |
|||
} |
|||
return uuids.stream().map(UUIDConverter::fromTimeUUID).collect(Collectors.toList()); |
|||
} |
|||
|
|||
} |
|||
|
|||
@ -0,0 +1,76 @@ |
|||
/** |
|||
* Copyright © 2016-2017 The Thingsboard Authors |
|||
* |
|||
* 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 org.thingsboard.server.common.data; |
|||
|
|||
import com.datastax.driver.core.utils.UUIDs; |
|||
import org.junit.Assert; |
|||
import org.junit.Test; |
|||
import org.junit.runner.RunWith; |
|||
import org.mockito.runners.MockitoJUnitRunner; |
|||
|
|||
import java.util.Random; |
|||
import java.util.UUID; |
|||
|
|||
/** |
|||
* Created by ashvayka on 14.07.17. |
|||
*/ |
|||
@RunWith(MockitoJUnitRunner.class) |
|||
public class UUIDConverterTest { |
|||
|
|||
@Test |
|||
public void basicUuidToStringTest() { |
|||
UUID original = UUID.fromString("58e0a7d7-eebc-11d8-9669-0800200c9a66"); |
|||
String result = UUIDConverter.fromTimeUUID(original); |
|||
Assert.assertEquals("1d8eebc58e0a7d796690800200c9a66", result); |
|||
} |
|||
|
|||
@Test |
|||
public void basicStringToUUIDTest() { |
|||
UUID result = UUIDConverter.fromString("1d8eebc58e0a7d796690800200c9a66"); |
|||
Assert.assertEquals(UUID.fromString("58e0a7d7-eebc-11d8-9669-0800200c9a66"), result); |
|||
} |
|||
|
|||
@Test(expected = IllegalArgumentException.class) |
|||
public void nonV1UuidToStringTest() { |
|||
UUIDConverter.fromTimeUUID(UUID.fromString("58e0a7d7-eebc-01d8-9669-0800200c9a66")); |
|||
} |
|||
|
|||
@Test |
|||
public void basicUuidComperisonTest() { |
|||
Random r = new Random(System.currentTimeMillis()); |
|||
for (int i = 0; i < 100000; i++) { |
|||
long ts = System.currentTimeMillis() + 1000 * 60 * 60 * 24 * 365 * 10; |
|||
long before = (long) (Math.random() * ts); |
|||
long after = (long) (Math.random() * ts); |
|||
if (before > after) { |
|||
long tmp = after; |
|||
after = before; |
|||
before = tmp; |
|||
} |
|||
|
|||
String beforeStr = UUIDConverter.fromTimeUUID(UUIDs.startOf(before)); |
|||
String afterStr = UUIDConverter.fromTimeUUID(UUIDs.startOf(after)); |
|||
|
|||
if (afterStr.compareTo(beforeStr) < 0) { |
|||
System.out.println("Before: " + before + " | " + beforeStr); |
|||
System.out.println("After: " + after + " | " + afterStr); |
|||
} |
|||
Assert.assertTrue(afterStr.compareTo(beforeStr) >= 0); |
|||
} |
|||
} |
|||
|
|||
|
|||
} |
|||
@ -0,0 +1,57 @@ |
|||
/** |
|||
* Copyright © 2016-2017 The Thingsboard Authors |
|||
* |
|||
* 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 org.thingsboard.server.dao.model; |
|||
|
|||
import lombok.Data; |
|||
import org.thingsboard.server.common.data.UUIDConverter; |
|||
|
|||
import javax.persistence.Column; |
|||
import javax.persistence.Id; |
|||
import javax.persistence.MappedSuperclass; |
|||
import java.util.UUID; |
|||
|
|||
/** |
|||
* Created by ashvayka on 13.07.17. |
|||
*/ |
|||
@Data |
|||
@MappedSuperclass |
|||
public abstract class BaseSqlEntity<D> implements BaseEntity<D> { |
|||
|
|||
@Id |
|||
@Column(name = ModelConstants.ID_PROPERTY) |
|||
protected String id; |
|||
|
|||
@Override |
|||
public UUID getId() { |
|||
if (id == null) { |
|||
return null; |
|||
} |
|||
return UUIDConverter.fromString(id); |
|||
} |
|||
|
|||
public void setId(UUID id) { |
|||
this.id = UUIDConverter.fromTimeUUID(id); |
|||
} |
|||
|
|||
protected UUID toUUID(String src){ |
|||
return UUIDConverter.fromString(src); |
|||
} |
|||
|
|||
protected String toString(UUID timeUUID){ |
|||
return UUIDConverter.fromTimeUUID(timeUUID); |
|||
} |
|||
|
|||
} |
|||
Some files were not shown because too many files changed in this diff
Loading…
Reference in new issue