All Downloads are FREE. Search and download functionalities are using the official Maven repository.

prompto.intrinsic.PromptoAny Maven / Gradle / Ivy

The newest version!
package prompto.intrinsic;

@SuppressWarnings("unchecked")
public class PromptoAny {

	public static void setMember(Object instance, Object key, Object value) {
		if(instance instanceof PromptoDocument)
			((PromptoDocument)instance).put(key, value);
		else
			throw new UnsupportedOperationException("Cannot call setMember for " + instance.getClass().getName());
	}
	
	public static Object getMember(Object instance, Object key) {
		if(instance instanceof PromptoDocument)
			return ((PromptoDocument)instance).getOrCreate(key, PromptoDocument.class);
		else if("text".equals(key))
			return instance.toString();
		else
			throw new UnsupportedOperationException("Cannot call getMember for " + instance.getClass().getName());
	}
	
	public static void setItem(Object instance, Object index, Object value) {
		if(instance instanceof PromptoList && index instanceof Long)
			((PromptoList)instance).set(((Long)index).intValue() - 1, value);
		else if(instance instanceof PromptoTuple && index instanceof Long)
			((PromptoTuple)instance).set(((Long)index).intValue() - 1, value);
		else if(instance instanceof PromptoDocument && index instanceof String)
			((PromptoDocument)instance).put(index, value);
		else
			throw new UnsupportedOperationException("Cannot call setItem for " + instance.getClass().getName());
	}

	public static Object getItem(Object instance, Object index) {
		if(instance instanceof PromptoList && index instanceof Long)
			return ((PromptoList)instance).get(((Long)index).intValue() - 1);
		else if(instance instanceof PromptoTuple && index instanceof Long)
			return ((PromptoTuple)instance).get(((Long)index).intValue() - 1);
		else if(instance instanceof PromptoDocument && index instanceof String)
			return ((PromptoDocument)instance).get(index);
		else
			throw new UnsupportedOperationException("Cannot call getItem for " + instance.getClass().getName());
	}
}