Skip to content

Instantly share code, notes, and snippets.

Created April 15, 2014 23:37
Show Gist options
  • Save anonymous/10789250 to your computer and use it in GitHub Desktop.
Save anonymous/10789250 to your computer and use it in GitHub Desktop.
HasKey and AbstractHasKey
/*
*/
package st.voo.tick.entity.util;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
/**
* A simple way to remove some boilerplate from some entity.
*/
abstract public class AbstractHasKey<T extends HasKey<T>> implements HasKey<T>
{
/* (non-Javadoc)
* @see st.voo.tick.entity.util.HasKey#getRef()
*/
@Override
public Ref<T> getRef() {
return Ref.create(getKey());
}
/* (non-Javadoc)
* @see st.voo.tick.entity.util.HasKey#equivalent(com.googlecode.objectify.Key)
*/
@Override
public boolean equivalent(Key<T> key) {
return getKey().equals(key);
}
/* (non-Javadoc)
* @see st.voo.tick.entity.util.HasKey#equivalent(com.googlecode.objectify.Ref)
*/
@Override
public boolean equivalent(Ref<T> ref) {
return getKey().equivalent(ref);
}
}
/*
*/
package st.voo.tick.entity.util;
import com.googlecode.objectify.Key;
import com.googlecode.objectify.Ref;
/**
* Defines a common interface for entities to expose their key
*/
public interface HasKey<T extends HasKey<T>>
{
/** Gets the key for the entity, duh */
Key<T> getKey();
/** Gets the ref version. Might need to rename this to ref() to avoid javabeans signature. */
Ref<T> getRef();
/** @return true if this entity is key-equivalent */
boolean equivalent(Key<T> key);
/** @return true if this entity is key-equivalent */
boolean equivalent(Ref<T> ref);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment