Skip to content

Instantly share code, notes, and snippets.

@eutkin
Created September 22, 2018 18:04
Show Gist options
  • Save eutkin/f5fe1567b6eb221c5e699e6339256b30 to your computer and use it in GitHub Desktop.
Save eutkin/f5fe1567b6eb221c5e699e6339256b30 to your computer and use it in GitHub Desktop.
package com.example.demo;
import lombok.Data;
import org.hibernate.annotations.GeneratorType;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
/**
* @author eutkin
*/
@Entity
@Data
public class Account {
@Id
@Type(type = "com.example.demo.AccountIdType")
@GeneratedValue(generator = "account-id-generator")
@GenericGenerator(name = "account-id-generator", strategy = "com.example.demo.AccountIdGenerator")
private AccountId id;
public Account() {
}
}
package com.example.demo;
import java.io.Serializable;
/**
* @author eutkin
*/
public class AccountId implements Serializable {
static final long SIZE = 8;
static final long FACTOR = (long) Math.pow(10.0, SIZE);
public static final int BANK_ID = 57;
private final Integer bankId;
private final Long id;
private AccountId(Integer bankId, Long id) {
this.bankId = bankId;
this.id = id;
}
public static AccountId of(Number bankId, Long id) {
if (bankId == null) {
throw new IllegalArgumentException("Required not null");
}
int bankIdAsInt = bankId.intValue();
if (bankIdAsInt != BANK_ID) {
throw new IllegalArgumentException("Incorrect format: " + bankIdAsInt);
}
if (id == null) {
throw new IllegalArgumentException("Required not null");
}
if (id > 0 && id.toString().length() != 8) {
throw new IllegalArgumentException("Incorrect format: " + id);
}
return new AccountId(bankIdAsInt, id);
}
public static AccountId fromIdWithBankId(Long idWithBankId) {
if (idWithBankId == null) {
throw new IllegalArgumentException("Required not null");
}
long id = idWithBankId % FACTOR;
long bankId = idWithBankId / FACTOR;
return of(bankId, id);
}
public static AccountId fromOnlyId(Long id) {
if (id == null) {
throw new IllegalArgumentException("Required not null");
}
return of(BANK_ID, id);
}
public static AccountId fromString(String raw) {
return raw == null || raw.isEmpty() ? null : of(Integer.parseInt(raw.substring(0,2)), Long.parseLong(raw.substring(2)));
}
public Integer getBankId() {
return bankId;
}
public Long getId() {
return id;
}
public Long get() {
return bankId * FACTOR + id;
}
public boolean isInternal(Long other) {
return other / FACTOR == BANK_ID;
}
@Override
public String toString() {
return get().toString();
}
}
package com.example.demo;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.id.IdentifierGenerator;
import java.io.Serializable;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.LongStream;
import java.util.stream.Stream;
import static com.example.demo.AccountId.SIZE;
/**
* @author eutkin
*/
public class AccountIdGenerator implements IdentifierGenerator {
@Override
public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
Long nextLong = ThreadLocalRandom.current().nextLong((long) Math.pow(10.0, SIZE - 1), (long) Math.pow(10, SIZE) - 1);
return AccountId.fromOnlyId(nextLong);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment