Skip to content

Instantly share code, notes, and snippets.

View noizu's full-sized avatar

Keith Brings noizu

View GitHub Profile
@noizu
noizu / gist:bebddcd44819ab373988
Last active November 24, 2015 10:01
All better. (Generic handling for serialization and encoding of nodes to Google Endpoints and Objectify). Allowing arbitrary tree structures to be encoded/decoded and presented via api.
/*
There we go, much cleaner than https://gist.github.com/noizu/579fe422e3d600b0eee4
encoding/decoding is handled in base class. We still require a recursive generic but we avoid the need to use a seperat helper class by forcing derived class to explicitly state the concrete class used for json encoding children.
getChildren/setChildren
Some boiler plate has to be added whenever including a Tree structure but its much more minimal than it was. No conditional logic, etc. Load to and from objectify appears to work. Load to and from memcache appears to work.
Important, any entity that embeds a tree should call OnSave, OnLoad on the nested entity as these may not called under certain conditions such as when using custom memcached encoding/decoding logic to work around poor built in cache granularity controls. ( https://github.com/noizu/fragmented-keys-4java )
@noizu
noizu / gist:579fe422e3d600b0eee4
Last active November 24, 2015 08:33
Nonsense to work around Objectify/Appengine restrictions against recursive structures and generic classes when saving/loading tree structures in a generic way for re-use.
/**
There has to be a more straight forward way. Not being able to use generics or persist recursions is painful. Maybe abstract base classes with type reflection and boxing/unboxing to prepare and populate nested structure and flat array structure. *muses*
Or moving flatten/expand logic into the Node class . . . so instantiating a single node and calling.expand(tree) onLoad, and m.tree = node.flatten() onSave. Which would let us keep that logic in a base class with out needing to work around specific node type (additional fields).
Sigh, but then we'd have a base class with a list of children of base class type or base class with a get children method of base class type or node interface type and extra boxing/unboxing cruft.
*/
public Long GetField(Integer from, Integer to) throws Exception {
if(from % 4 == 0 && (to + 1) % 4 == 0) {
return hexToLong(this.data, from/4, (to + 1)/4);
} else {
// 1. Grab Nibble bounding from, and to.
int firstChar = (int) Math.floor( (double) (from/4.0)) ;
int lastChar = (int) Math.ceil( (double) ((to + 1.0)/4.0));
int bitWidth = (to + 1) - from;