Change album ID to be unique, add internal ID
[Sone.git] / src / main / java / net / pterodactylus / sone / data / IdBuilder.java
1 package net.pterodactylus.sone.data;
2
3 import javax.annotation.Nonnull;
4 import javax.annotation.concurrent.ThreadSafe;
5
6 import com.google.common.base.Charsets;
7 import com.google.common.hash.HashFunction;
8 import com.google.common.hash.Hashing;
9
10 /**
11  * Builds (practically) unique IDs by combining Sone and element IDs.
12  *
13  * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
14  */
15 @ThreadSafe
16 public class IdBuilder {
17
18         private static final HashFunction HASH_FUNCTION = Hashing.sha256();
19         public static final int ID_STRING_LENGTH = HASH_FUNCTION.bits() / 4;
20
21         private final HashFunction sha256 = HASH_FUNCTION;
22
23         @Nonnull
24         public String buildId(@Nonnull String soneId, @Nonnull String id) {
25                 return sha256.newHasher()
26                                 .putBytes(soneId.getBytes(Charsets.UTF_8))
27                                 .putBytes(id.getBytes(Charsets.UTF_8))
28                                 .hash().toString();
29         }
30
31 }