Skip to content

Instantly share code, notes, and snippets.

@darrend
Last active August 29, 2015 14:26
Show Gist options
  • Save darrend/994252b083a968c73038 to your computer and use it in GitHub Desktop.
Save darrend/994252b083a968c73038 to your computer and use it in GitHub Desktop.
playing around with java 8 and application properties
package us.copperdays.snippets.applicationproperty.demo;
import javax.persistence.*;
@Entity
@Table(name = "APP_PROPS", schema = "DEMO")
public class ApplicationPropertyEntity {
@Id
@Column(name = "KEY")
private String key;
@Column(name="VALUE")
private String value;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
TestApplicationProperty that = (TestApplicationProperty) o;
if (!key.equals(that.key)) return false;
return !(value != null ? !value.equals(that.value) : that.value != null);
}
@Override
public int hashCode() {
int result = key.hashCode();
result = 31 * result + (value != null ? value.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "ApplicationProperty{" +
"key='" + key + '\'' +
", value='" + value + '\'' +
'}';
}
}
package us.copperdays.snippets.applicationproperty.demo;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TestApplicationPropertyRepository extends JpaRepository<ApplicationProperty,String> {
}
package us.copperdays.snippets.applicationproperty;
import java.util.function.Consumer;
/**
* Define the basics of an application property source
*
* @param <K> type for keys
*/
public interface ApplicationPropertySource<K> {
/**
* Given the key, return the value
*
* @param key assocated with value
* @param <T> type of the value
* @return value assigned or null if not found
*/
<T> T get(K key);
/**
* Is there a value for this key?
* @param key is this associated with a value?
* @return true if there is a value associated with this key
*/
default boolean hasValue(K key) {
return get(key)!=null;
}
/**
* Given the key return associated value or fallback if not found
*
* @param key associated with value
* @param fallback will be returned if
* @param <T> value type
* @return the value or the fallback if the key is not found
*/
default <T> T get(K key, T fallback) {
return hasValue(key) ? get(key) : fallback;
}
/**
* Require a value for this key or throw an exception
*
* @param key associated with the value
* @param <T> type of value
* @return value for the given key
* @throws PropertyNotFoundException if key is not found
*/
default <T> T mustGet(K key) throws PropertyNotFoundException {
if(!hasValue(key)) { throw new PropertyNotFoundException(key); }
return get(key);
}
/**
*
* @param key
* @param valueConsumer
* @param <T>
* @see #get(Object)
*/
default <T> void visit(K key, Consumer<T> valueConsumer) {
if(hasValue(key)) { valueConsumer.accept(get(key)); }
}
/**
*
* @param key
* @param fallback
* @param valueConsumer
* @param <T>
* @see #get(Object, Object)
*/
default <T> void visit(K key, T fallback, Consumer<T> valueConsumer) {
valueConsumer.accept(get(key, fallback));
}
/**
*
* @param key
* @param valueConsumer
* @param <T>
* @see #mustGet(Object)
*/
default <T> void mustVisit(K key, Consumer<T> valueConsumer) {
valueConsumer.accept(mustGet(key));
}
/**
* Check for new values in sources for properties
*/
default void refresh() {};
class PropertyNotFoundException extends RuntimeException {
private Object key;
public PropertyNotFoundException(Object key) {
super("Could not find property with key ["+key+"]");
this.key = key;
}
public Object getKey() {
return key;
}
}
}
package us.copperdays.snippets.applicationproperty.demo;
public enum DemoPropertyKey {
DISPATCHER_SCHEDULE,
DISPATCHER_ENABLED,
DISPATCHER_REPEAT_DELAY_MS
}
package us.copperdays.snippets.applicationproperty.demo;
import us.copperdays.snippets.applicationproperty.*;
public class DemoPropertySourceImpl implements ApplicationPropertySource<DemoPropertyKey> {
private ApplicationPropertyRepository repository;
public DemoPropertySourceImpl(ApplicationPropertyRepository repository) {
this.repository = repository;
}
@Override
public <T> T get(MessageListenerPropertyKey key) {
TestApplicationProperty one = repository.findOne(key.name());
return one==null?null: (T) one.getValue();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment