InitializerIsomorphism.java
/***************************************************************************
Copyright 2013 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.util.reflect;
import net.metanotion.util.Dictionary;
/** Given two "representations" of a structures that have the same set of fields, this class will use
GetInitializer's to provide a two way conversion between the representations.
@param <A> The type that will be produced by calling {@link #a} with an instance of B.
@param <B> The type that will be produced by calling {@link #b} with an instance of A.
*/
public final class InitializerIsomorphism<A,B> {
private final GetInitializer<A> giA;
private final GetInitializer<B> giB;
/** Create an instance of the converter from the two GetInitializer's.
@param a The GetInitializer for representation A.
@param b The GetInitializer for representation B.
*/
public InitializerIsomorphism(GetInitializer<A> a, GetInitializer<B> b) {
this.giA = a;
this.giB = b;
}
/** This is the conversion algorithm, since both representation's used by InitializerIsomorphism are
based on GetInitializer's, the algorithm is the same in either direction, we just swap which
GetInitializer to use.
@param <C> The type of structure this class produces.
@param <D> The type of structure this class consumes.
@param out The GetInitializer for the output/return type.
@param in The GetInitializer for the input type.
@param val An instance of the input type to convert.
@return An instance of the output type created from val.
*/
private static <C,D> C xfrm(final GetInitializer<C> out, final GetInitializer<D> in, final D val) {
/* Since null is an instance of every type, pass it through unaffected to avoid null pointer exceptions.
This may not actually be the "best" way to do this overall(perhaps a structure has an interpretation of null
that isn't null...), but... for now it's here... this line of code should probably just be removed. */
if(val == null) { return null; }
try {
// Get an instance of the intializer for the output type so we can construct an instance.
final Initializer<C> outObj = out.initializer();
// Get a dictionary version of the input instance to make field retrieval easy.
final Dictionary<String,Object> inObj = in.struct(val);
// Copy the fields.
for(String field: out) { outObj.put(field, inObj.get(field)); }
// Create the output instance and return it.
return outObj.instance();
} catch (InstantiationException | IllegalAccessException e) {
throw new RuntimeException(e);
}
}
/** Convert an instance of representation B to representation A.
@param bVal an instance of type B.
@return an instance of type A equivalent to bVal.
*/
public A a(final B bVal) { return InitializerIsomorphism.xfrm(giA, giB, bVal); }
/** Convert an instance of representation A to representation B.
@param aVal an instance of type A.
@return an instance of type B equivalent to aVal.
*/
public B b(final A aVal) { return InitializerIsomorphism.xfrm(giB, giA, aVal); }
}