-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Insert a cache in OutlineBuilder and make it immutable in which case …
…it returns by default Add a method to allow child elements to be serialized more easily
- Loading branch information
Showing
16 changed files
with
168 additions
and
59 deletions.
There are no files selected for viewing
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
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
59 changes: 51 additions & 8 deletions
59
core/src/main/java/foundation/stack/datamill/reflection/OutlineBuilder.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 |
---|---|---|
@@ -1,38 +1,81 @@ | ||
package foundation.stack.datamill.reflection; | ||
|
||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.CacheLoader; | ||
import com.google.common.cache.LoadingCache; | ||
import javassist.util.proxy.ProxyFactory; | ||
import foundation.stack.datamill.reflection.impl.OutlineImpl; | ||
import org.objenesis.Objenesis; | ||
import org.objenesis.ObjenesisStd; | ||
|
||
import java.util.concurrent.ExecutionException; | ||
|
||
/** | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
public class OutlineBuilder { | ||
private static final Objenesis objenesis = new ObjenesisStd(); | ||
public static final OutlineBuilder DEFAULT = new OutlineBuilder(); | ||
|
||
private static <T> Outline<T> buildOutline(Class<T> classToOutline, boolean camelCased) { | ||
ProxyFactory proxyFactory = new ProxyFactory(); | ||
proxyFactory.setSuperclass(classToOutline); | ||
Class<? extends T> outlineClass = proxyFactory.createClass(); | ||
return new OutlineImpl<>(objenesis.newInstance(outlineClass), camelCased); | ||
} | ||
|
||
private final LoadingCache<Class<?>, Outline<?>> camelCasedOutlineCache; | ||
private final LoadingCache<Class<?>, Outline<?>> snakeCasedOutlineCache; | ||
|
||
private boolean camelCased; | ||
|
||
public OutlineBuilder() { | ||
this(false, | ||
CacheBuilder.newBuilder().build(new OutlineCacheLoader(true)), | ||
CacheBuilder.newBuilder().build(new OutlineCacheLoader(false))); | ||
} | ||
|
||
private OutlineBuilder( | ||
boolean camelCased, | ||
LoadingCache<Class<?>, Outline<?>> camelCasedOutlineCache, | ||
LoadingCache<Class<?>, Outline<?>> snakeCasedOutlineCache) { | ||
this.camelCased = camelCased; | ||
this.camelCasedOutlineCache = camelCasedOutlineCache; | ||
this.snakeCasedOutlineCache = snakeCasedOutlineCache; | ||
} | ||
|
||
public OutlineBuilder defaultCamelCased() { | ||
camelCased = true; | ||
return this; | ||
return new OutlineBuilder(true, camelCasedOutlineCache, snakeCasedOutlineCache); | ||
} | ||
|
||
public OutlineBuilder defaultSnakeCased() { | ||
camelCased = false; | ||
return this; | ||
return new OutlineBuilder(false, camelCasedOutlineCache, snakeCasedOutlineCache); | ||
} | ||
|
||
public <T> Outline<T> build(Class<T> classToOutline) { | ||
ProxyFactory proxyFactory = new ProxyFactory(); | ||
proxyFactory.setSuperclass(classToOutline); | ||
Class<? extends T> outlineClass = proxyFactory.createClass(); | ||
return new OutlineImpl<>(objenesis.newInstance(outlineClass), camelCased); | ||
try { | ||
return camelCased ? | ||
(Outline<T>) camelCasedOutlineCache.get(classToOutline) : | ||
(Outline<T>) snakeCasedOutlineCache.get(classToOutline); | ||
} catch (ExecutionException e) { | ||
throw new ReflectionException(e.getCause()); | ||
} | ||
} | ||
|
||
public <T> Bean<T> wrap(T instance) { | ||
return build((Class<T>) instance.getClass()).wrap(instance); | ||
} | ||
|
||
private static class OutlineCacheLoader extends CacheLoader<Class<?>, Outline<?>> { | ||
private final boolean camelCased; | ||
|
||
public OutlineCacheLoader(boolean camelCased) { | ||
this.camelCased = camelCased; | ||
} | ||
|
||
@Override | ||
public Outline<?> load(Class<?> key) throws Exception { | ||
return buildOutline(key, camelCased); | ||
} | ||
} | ||
} |
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
28 changes: 28 additions & 0 deletions
28
core/src/main/java/foundation/stack/datamill/values/DeserializationStrategy.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,28 @@ | ||
package foundation.stack.datamill.values; | ||
|
||
import foundation.stack.datamill.reflection.Bean; | ||
|
||
/** | ||
* A {@link DeserializationStrategy} defines a function which is used in deserializing an object. It should take values | ||
* from the given {@link StructuredValue} and insert them into the object so that the {@link StructuredValue} can | ||
* be deserialized. | ||
* <p> | ||
* For example, deserializing User objects may look like this: | ||
* <pre> | ||
* public static final DeserializationStrategy<User> PUBLIC = | ||
* (user, source) -> user | ||
* .set(m -> m.getId(), source.get(user.member(m -> m.getId()))) | ||
* .set(m -> m.getName(), source.get(user.member(m -> m.getName()))); | ||
* </pre> | ||
* <p> | ||
* User objects can then be serialized to JSON by calling: | ||
* <pre> | ||
* STANDARD.serialize(new JsonObject(), userOutline, user); | ||
* </pre> | ||
* | ||
* @author Ravi Chodavarapu (rchodava@gmail.com) | ||
*/ | ||
@FunctionalInterface | ||
public interface DeserializationStrategy<T> { | ||
T deserialize(Bean<T> target, StructuredValue source); | ||
} |
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
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
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
Oops, something went wrong.