Skip to content

Instantly share code, notes, and snippets.

Created April 15, 2014 23:37

Revisions

  1. @invalid-email-address Anonymous created this gist Apr 15, 2014.
    38 changes: 38 additions & 0 deletions AbstractHasKey.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    /*
    */

    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);
    }

    }
    26 changes: 26 additions & 0 deletions HasKey.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    /*
    */

    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);
    }