-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
143 additions
and
5 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
vpro-shared-hibernate/src/main/java/nl/vpro/hibernate/DurationToLongType.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package nl.vpro.hibernate; | ||
|
||
import java.io.Serializable; | ||
import java.math.BigDecimal; | ||
import java.sql.*; | ||
import java.time.Duration; | ||
|
||
import org.hibernate.HibernateException; | ||
import org.hibernate.engine.spi.SharedSessionContractImplementor; | ||
import org.hibernate.usertype.UserType; | ||
|
||
/** | ||
* @author Michiel Meeuwissen | ||
* @since 0.36 | ||
*/ | ||
public class DurationToLongType implements UserType<Duration> { | ||
|
||
public static final DurationToLongType INSTANCE = new DurationToLongType(); | ||
|
||
|
||
public DurationToLongType() { | ||
super(); | ||
} | ||
|
||
|
||
@Override | ||
public int getSqlType() { | ||
return Types.BIGINT; | ||
} | ||
|
||
@Override | ||
public Class<Duration> returnedClass() { | ||
return Duration.class; | ||
} | ||
|
||
@Override | ||
public boolean equals(Duration x, Duration y) throws HibernateException { | ||
if (x == null) { | ||
return y == null; | ||
} | ||
return x.equals(y); | ||
|
||
} | ||
|
||
@Override | ||
public int hashCode(Duration x) throws HibernateException { | ||
if (x == null) { | ||
return 0; | ||
} | ||
return x.hashCode(); | ||
} | ||
|
||
@Override | ||
public Duration nullSafeGet(ResultSet rs, int position, SharedSessionContractImplementor session, Object owner) throws SQLException { | ||
BigDecimal ts = rs.getBigDecimal(position); | ||
if (ts == null) { | ||
return null; | ||
} | ||
return Duration.ofMillis(ts.toBigInteger().longValue()); | ||
} | ||
|
||
|
||
@Override | ||
public void nullSafeSet(PreparedStatement st, Duration value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException { | ||
if (value == null) { | ||
st.setNull(index, Types.TIMESTAMP); | ||
} else { | ||
st.setBigDecimal(index, BigDecimal.valueOf(value.toMillis())); | ||
} | ||
} | ||
|
||
@Override | ||
public Duration deepCopy(Duration value) throws HibernateException { | ||
return value; | ||
} | ||
|
||
@Override | ||
public boolean isMutable() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Serializable disassemble(Duration value) throws HibernateException { | ||
return value; | ||
} | ||
|
||
@Override | ||
public Duration assemble(Serializable cached, Object owner) throws HibernateException { | ||
return (Duration) cached; | ||
} | ||
|
||
@Override | ||
public Duration replace(Duration original, Duration target, Object owner) throws HibernateException { | ||
return original; | ||
} | ||
} | ||
|
25 changes: 25 additions & 0 deletions
25
vpro-shared-jcache/src/main/java/nl/vpro/javax/cache/CustomExpiryPolicy.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package nl.vpro.javax.cache; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import javax.cache.expiry.Duration; | ||
import javax.cache.expiry.ExpiryPolicy; | ||
|
||
public class CustomExpiryPolicy implements ExpiryPolicy { | ||
|
||
private java.time.Duration expiry = java.time.Duration.ofMinutes(5); | ||
@Override | ||
public Duration getExpiryForCreation() { | ||
return new Duration(TimeUnit.MILLISECONDS, expiry.toMillis()); | ||
} | ||
|
||
@Override | ||
public Duration getExpiryForAccess() { | ||
return new Duration(TimeUnit.MILLISECONDS, expiry.toMillis()); | ||
} | ||
|
||
@Override | ||
public Duration getExpiryForUpdate() { | ||
return new Duration(TimeUnit.MILLISECONDS, expiry.toMillis()); | ||
} | ||
} |
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
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