@Override protected Class<?> getKey(ISerializer<?> item) { return item.getType(); }
/** * Returns an object of the specified class. The class must have an associated context * serializer registered. * * @param <T> The item's class. * @param itemName Item name * @param clazz Class of item to be returned. * @return Deserialized item of specified class. * @throws ContextException If no context serializer found. */ @SuppressWarnings("unchecked") public <T> T getItem(String itemName, Class<T> clazz) throws ContextException { String item = getItem(itemName); if (item == null || item.isEmpty()) { return null; } ISerializer<?> contextSerializer = ContextSerializerRegistry.getInstance().get(clazz); if (contextSerializer == null) { throw new ContextException("No serializer found for type " + clazz.getName()); } return (T) contextSerializer.deserialize(item); }
/** * Sets a context item value. * * @param itemName Item name. * @param value The value to set. The value's class must have an associated context serializer * registered for it. */ public void setItem(String itemName, Object value) { if (value == null) { setItem(itemName, (String) null); } else { @SuppressWarnings("unchecked") ISerializer<Object> contextSerializer = (ISerializer<Object>) ContextSerializerRegistry.getInstance() .get(value.getClass()); if (contextSerializer == null) { throw new ContextException("No serializer found for type " + value.getClass().getName()); } setItem(itemName, contextSerializer.serialize(value)); } }
/** * Returns the item associated with the specified key, or null if not found. * * @param clazz The class whose serializer is sought. * @return The context serializer. */ @Override public ISerializer<?> get(Class<?> clazz) { ISerializer<?> contextSerializer = super.get(clazz); if (contextSerializer != null) { return contextSerializer; } for (ISerializer<?> item : this) { if (item.getType().isAssignableFrom(clazz)) { return item; } } return null; }