Created
September 19, 2018 18:24
-
-
Save eutkin/f251a766591c052adfc74889b9ad1c1c to your computer and use it in GitHub Desktop.
bank id type
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import lombok.Data; | |
import org.hibernate.annotations.Type; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
/** | |
* @author eutkin | |
*/ | |
@Entity | |
@Data | |
public class Account { | |
@Id | |
@Type(type = "com.example.demo.BankIdType") | |
private BankId id; | |
public Account(BankId id) { | |
this.id = id; | |
} | |
public Account() { | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import java.io.Serializable; | |
/** | |
* @author eutkin | |
*/ | |
public class BankId implements Serializable { | |
private static final long SIZE = (long) Math.pow(10.0, 8.0); | |
public static final int BANK_ID = 57; | |
private final Integer bankId; | |
private final Long id; | |
private BankId(Integer bankId, Long id) { | |
this.bankId = bankId; | |
this.id = id; | |
} | |
public static BankId 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 && Math.log10(id.doubleValue()) + 1 == 8) { | |
throw new IllegalArgumentException("Incorrect format: " + id); | |
} | |
return new BankId(bankIdAsInt, id); | |
} | |
public static BankId fromIdWithBankId(Long idWithBankId) { | |
if (idWithBankId == null) { | |
throw new IllegalArgumentException("Required not null"); | |
} | |
long id = idWithBankId % SIZE; | |
long bankId = idWithBankId / SIZE; | |
return of(bankId, id); | |
} | |
public static BankId fromOnlyId(Long id) { | |
if (id == null) { | |
throw new IllegalArgumentException("Required not null"); | |
} | |
return of(BANK_ID, id); | |
} | |
public static BankId 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 * SIZE + id; | |
} | |
@Override | |
public String toString() { | |
return get().toString(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import org.hibernate.HibernateException; | |
import org.hibernate.engine.spi.SharedSessionContractImplementor; | |
import org.hibernate.usertype.UserType; | |
import java.io.Serializable; | |
import java.sql.PreparedStatement; | |
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.sql.Types; | |
import java.util.Objects; | |
/** | |
* @author eutkin | |
*/ | |
public class BankIdType implements UserType { | |
private static final BankIdValueMapper mapper = new BankIdValueMapperImpl(); | |
@Override | |
public int[] sqlTypes() { | |
return new int[]{Types.BIGINT}; | |
} | |
@Override | |
public Class returnedClass() { | |
return BankId.class; | |
} | |
@Override | |
public boolean equals(Object x, Object y) throws HibernateException { | |
return Objects.equals(x, y); | |
} | |
@Override | |
public int hashCode(Object x) throws HibernateException { | |
return Objects.hashCode(x); | |
} | |
@Override | |
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner) throws HibernateException, SQLException { | |
return mapper.getValue(rs, names); | |
} | |
@Override | |
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { | |
mapper.setValue(st, (BankId) value, index); | |
} | |
@Override | |
public Object deepCopy(Object value) throws HibernateException { | |
BankId bankId = (BankId) value; | |
return BankId.of(bankId.getBankId(), bankId.getId()); | |
} | |
@Override | |
public boolean isMutable() { | |
return false; | |
} | |
@Override | |
public Serializable disassemble(Object value) throws HibernateException { | |
return (Serializable) value; | |
} | |
@Override | |
public Object assemble(Serializable cached, Object owner) throws HibernateException { | |
return cached; | |
} | |
@Override | |
public Object replace(Object original, Object target, Object owner) throws HibernateException { | |
return original; | |
} | |
private interface BankIdValueMapper extends Serializable { | |
BankId getValue(ResultSet rs, String[] names) throws SQLException; | |
void setValue(PreparedStatement st, BankId value, int index) throws SQLException; | |
String objectToSQLString(BankId value); | |
String toXMLString(BankId value); | |
BankId fromXMLString(String xml); | |
} | |
private static class BankIdValueMapperImpl implements BankIdValueMapper { | |
@Override | |
public BankId getValue(ResultSet rs, String[] names) throws SQLException { | |
final Long value = rs.getLong(names[0]); | |
if (rs.wasNull()) { | |
return null; | |
} | |
return BankId.fromIdWithBankId(value); | |
} | |
@Override | |
public void setValue(PreparedStatement st, BankId value, int index) throws SQLException { | |
if (value == null) { | |
st.setNull(index, Types.BIGINT); | |
} else { | |
st.setLong(index, value.get()); | |
} | |
} | |
@Override | |
public String objectToSQLString(BankId value) { | |
return Objects.toString(value); | |
} | |
@Override | |
public String toXMLString(BankId value) { | |
return Objects.toString(value); | |
} | |
@Override | |
public BankId fromXMLString(String xml) { | |
return BankId.fromString(xml); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment