org.parceler.Parcels Maven / Gradle / Ivy
/**
* Copyright 2011-2015 John Ericksen
*
* 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 org.parceler;
import android.os.Parcelable;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
/**
* Static utility class used to wrap an `@Parcel` annotated class with the generated `Parcelable` wrapper.
*
* @author John Ericksen
*/
public final class Parcels {
public static final String PARCELS_NAME = "Parcels";
public static final String PARCELS_REPOSITORY_NAME = "Parceler$$Parcels";
public static final String PARCELS_PACKAGE = "org.parceler";
public static final String IMPL_EXT = "Parcelable";
private static final ParcelCodeRepository REPOSITORY = new ParcelCodeRepository();
private static final NullParcelable NULL_PARCELABLE = new NullParcelable();
static{
REPOSITORY.loadRepository(NonParcelRepository.getInstance());
}
private Parcels(){
// private utility class constructor
}
/**
* Testing method for replacing the Parceler$Parcels class with one referenced in the given classloader.
*
* @param classLoader ClassLoader to use when loading repository.
*/
protected static void update(ClassLoader classLoader){
REPOSITORY.loadRepository(classLoader);
}
/**
* Wraps the input `@Parcel` annotated class with a `Parcelable` wrapper.
*
* @throws ParcelerRuntimeException if there was an error looking up the wrapped Parceler$Parcels class.
* @param input Parcel
* @return Parcelable wrapper
*/
@SuppressWarnings("unchecked")
public static Parcelable wrap(T input) {
if(input == null){
return NULL_PARCELABLE;
}
return wrap(input.getClass(), input);
}
/**
* Wraps the input `@Parcel` annotated class with a `Parcelable` wrapper.
*
* @throws ParcelerRuntimeException if there was an error looking up the wrapped Parceler$Parcels class.
* @param inputType specific type to parcel
* @param input Parcel
* @return Parcelable wrapper
*/
@SuppressWarnings("unchecked")
public static Parcelable wrap(Class extends T> inputType, T input) {
if(input == null){
return NULL_PARCELABLE;
}
ParcelableFactory parcelableFactory = REPOSITORY.get(inputType);
return parcelableFactory.buildParcelable(input);
}
/**
* Unwraps the input wrapped `@Parcel` `Parcelable`
*
* @throws ClassCastException if the input Parcelable does not implement ParcelWrapper with the correct parameter type.
* @param input Parcelable implementing ParcelWrapper
* @param type of unwrapped `@Parcel`
* @return Unwrapped `@Parcel`
*/
@SuppressWarnings("unchecked")
public static T unwrap(Parcelable input) {
if(input == null){
return null;
}
ParcelWrapper wrapper = (ParcelWrapper) input;
return wrapper.getParcel();
}
public static class NullParcelable implements Parcelable, ParcelWrapper