@Override @SuppressWarnings("unchecked") public <T> T getValue(@NonNull TypeToken<T> type, @NonNull Supplier<T> defSupplier) throws ObjectMappingException { Object value = getValue(); if (value == null) { return storeDefault(defSupplier.get()); } TypeSerializer serial = getOptions().getSerializers().get(type); if (serial == null) { if (type.getRawType().isInstance(value)) { return (T) type.getRawType().cast(value); } else { return storeDefault(defSupplier.get()); } } return (T) serial.deserialize(type, this); }
@Override @SuppressWarnings("unchecked") public <T> T getValue(@NonNull TypeToken<T> type, T def) throws ObjectMappingException { Object value = getValue(); if (value == null) { return storeDefault(def); } TypeSerializer serial = getOptions().getSerializers().get(type); if (serial == null) { if (type.getRawType().isInstance(value)) { return (T) type.getRawType().cast(value); } else { return storeDefault(def); } } return (T) serial.deserialize(type, this); }
private ConfigurationOptions getConfigurationOptions() { ConfigurationOptions defaults = ConfigurationOptions.defaults(); TypeSerializerCollection serializers = defaults.getSerializers().newChild(); serializers.registerType(TypeToken.of(Duration.class), new DurationSerializer()); //explicit set enum serializer because otherwise they will be interpreted as class with the requirement of //a public constructor TypeSerializer<Enum> enumSerializer = serializers.get(TypeToken.of(Enum.class)); serializers.registerType(TypeToken.of(StorageType.class), enumSerializer); serializers.registerType(TypeToken.of(HashingAlgorithm.class), enumSerializer); return defaults.setSerializers(serializers); }
@Override @SuppressWarnings("unchecked") public <T> T getValue(@NonNull TypeToken<T> type, @NonNull Supplier<T> defSupplier) throws ObjectMappingException { Object value = getValue(); if (value == null) { return storeDefault(type, defSupplier.get()); } TypeSerializer serial = getOptions().getSerializers().get(type); if (serial == null) { if (type.getRawType().isInstance(value)) { return (T) type.getRawType().cast(value); } else { return storeDefault(type, defSupplier.get()); } } return (T) serial.deserialize(type, this); }
@Override @SuppressWarnings("unchecked") public <T> T getValue(@NonNull TypeToken<T> type, T def) throws ObjectMappingException { Object value = getValue(); if (value == null) { return storeDefault(type, def); } TypeSerializer serial = getOptions().getSerializers().get(type); if (serial == null) { if (type.getRawType().isInstance(value)) { return (T) type.getRawType().cast(value); } else { return storeDefault(type, def); } } return (T) serial.deserialize(type, this); }
public void deserializeFrom(Object instance, ConfigurationNode node) throws ObjectMappingException { TypeSerializer<?> serial = node.getOptions().getSerializers().get(this.fieldType); if (serial == null) { throw new ObjectMappingException("No TypeSerializer found for field " + field.getName() + " of type " + this.fieldType); } Object newVal = node.isVirtual() ? null : serial.deserialize(this.fieldType, node); try { if (newVal == null) { Object existingVal = field.get(instance); if (existingVal != null) { serializeTo(instance, node); } } else { field.set(instance, newVal); } } catch (IllegalAccessException e) { throw new ObjectMappingException("Unable to deserialize field " + field.getName(), e); } }
public void deserializeFrom(Object instance, ConfigurationNode node) throws ObjectMappingException { TypeSerializer<?> serial = node.getOptions().getSerializers().get(this.fieldType); if (serial == null) { throw new ObjectMappingException("No TypeSerializer found for field " + field.getName() + " of type " + this.fieldType); } Object newVal = node.isVirtual() ? null : serial.deserialize(this.fieldType, node); try { if (newVal == null) { Object existingVal = field.get(instance); if (existingVal != null) { serializeTo(instance, node); } } else { field.set(instance, newVal); } } catch (IllegalAccessException e) { throw new ObjectMappingException("Unable to deserialize field " + field.getName(), e); } }
@Override public Set<?> deserialize(TypeToken<?> type, ConfigurationNode value) throws ObjectMappingException { TypeToken<?> entryType = type.resolveType(Set.class.getTypeParameters()[0]); TypeSerializer<?> entrySerial = value.getOptions().getSerializers().get(entryType); if (entrySerial == null) { throw new ObjectMappingException("No applicable type serializer for type " + entryType); } if (value.hasListChildren()) { List<? extends ConfigurationNode> values = value.getChildrenList(); Set<Object> ret = new HashSet<>(values.size()); for (ConfigurationNode ent : values) { ret.add(entrySerial.deserialize(entryType, ent)); } return ret; } else { Object unwrappedVal = value.getValue(); if (unwrappedVal != null) { return Sets.newHashSet(entrySerial.deserialize(entryType, value)); } } return new HashSet<>(); }
@Override public void serialize(TypeToken<?> type, Set<?> obj, ConfigurationNode value) throws ObjectMappingException { TypeToken<?> entryType = type.resolveType(Set.class.getTypeParameters()[0]); TypeSerializer entrySerial = value.getOptions().getSerializers().get(entryType); if (entrySerial == null) { throw new ObjectMappingException("No applicable type serializer for type " + entryType); } value.setValue(ImmutableSet.of()); for (Object ent : obj) { entrySerial.serialize(entryType, ent, value.getAppendedNode()); } } }
public void deserializeFrom(Object instance, ConfigurationNode node) throws ObjectMappingException { TypeSerializer<?> serial = node.getOptions().getSerializers().get(this.fieldType); if (serial == null) { throw new ObjectMappingException("No TypeSerializer found for field " + field.getName() + " of type " + this.fieldType); } Object newVal = node.isVirtual() ? null : serial.deserialize(this.fieldType, node); try { if (newVal == null) { Object existingVal = field.get(instance); if (existingVal != null) { serializeTo(instance, node); } } else { field.set(instance, newVal); } } catch (IllegalAccessException e) { throw new ObjectMappingException("Unable to deserialize field " + field.getName(), e); } }
@SuppressWarnings("rawtypes") public void serializeTo(Object instance, ConfigurationNode node) throws ObjectMappingException { try { Object fieldVal = this.field.get(instance); if (fieldVal == null) { node.setValue(null); } else { TypeSerializer serial = node.getOptions().getSerializers().get(this.fieldType); if (serial == null) { throw new ObjectMappingException("No TypeSerializer found for field " + field.getName() + " of type " + this.fieldType); } serial.serialize(this.fieldType, fieldVal, node); } if (node instanceof CommentedConfigurationNode && this.comment != null && !this.comment.isEmpty()) { CommentedConfigurationNode commentNode = ((CommentedConfigurationNode) node); if (!commentNode.getComment().isPresent()) { commentNode.setComment(this.comment); } } } catch (IllegalAccessException e) { throw new ObjectMappingException("Unable to serialize field " + field.getName(), e); } } }
@SuppressWarnings("rawtypes") public void serializeTo(Object instance, ConfigurationNode node) throws ObjectMappingException { try { Object fieldVal = this.field.get(instance); if (fieldVal == null) { node.setValue(null); } else { TypeSerializer serial = node.getOptions().getSerializers().get(this.fieldType); if (serial == null) { throw new ObjectMappingException("No TypeSerializer found for field " + field.getName() + " of type " + this.fieldType); } serial.serialize(this.fieldType, fieldVal, node); } if (node instanceof CommentedConfigurationNode && this.comment != null && !this.comment.isEmpty()) { CommentedConfigurationNode commentNode = ((CommentedConfigurationNode) node); if (!commentNode.getComment().isPresent()) { commentNode.setComment(this.comment); } } } catch (IllegalAccessException e) { throw new ObjectMappingException("Unable to serialize field " + field.getName(), e); } } }
@SuppressWarnings("rawtypes") public void serializeTo(Object instance, ConfigurationNode node) throws ObjectMappingException { try { Object fieldVal = this.field.get(instance); if (fieldVal == null) { node.setValue(null); } else { TypeSerializer serial = node.getOptions().getSerializers().get(this.fieldType); if (serial == null) { throw new ObjectMappingException("No TypeSerializer found for field " + field.getName() + " of type " + this.fieldType); } serial.serialize(this.fieldType, fieldVal, node); } if (node instanceof CommentedConfigurationNode && this.comment != null && !this.comment.isEmpty()) { CommentedConfigurationNode commentNode = ((CommentedConfigurationNode) node); if (!commentNode.getComment().isPresent()) { commentNode.setComment(this.comment); } } } catch (IllegalAccessException e) { throw new ObjectMappingException("Unable to serialize field " + field.getName(), e); } } }
@Override @SuppressWarnings("rawtypes") public void serialize(TypeToken<?> type, Map<?, ?> obj, ConfigurationNode node) throws ObjectMappingException { if (!(type.getType() instanceof ParameterizedType)) { throw new ObjectMappingException("Raw types are not supported for collections"); } TypeToken<?> key = type.resolveType(Map.class.getTypeParameters()[0]); TypeToken<?> value = type.resolveType(Map.class.getTypeParameters()[1]); TypeSerializer keySerial = node.getOptions().getSerializers().get(key); TypeSerializer valueSerial = node.getOptions().getSerializers().get(value); if (keySerial == null) { throw new ObjectMappingException("No type serializer available for type " + key); } if (valueSerial == null) { throw new ObjectMappingException("No type serializer available for type " + value); } node.setValue(ImmutableMap.of()); for (Map.Entry<?, ?> ent : obj.entrySet()) { SimpleConfigurationNode keyNode = SimpleConfigurationNode.root(); keySerial.serialize(key, ent.getKey(), keyNode); valueSerial.serialize(value, ent.getValue(), node.getNode(keyNode.getValue())); } } }
@Override public List<?> deserialize(@NonNull TypeToken<?> type, @NonNull ConfigurationNode value) throws ObjectMappingException { if (!(type.getType() instanceof ParameterizedType)) { throw new ObjectMappingException("Raw types are not supported for collections"); } TypeToken<?> entryType = type.resolveType(List.class.getTypeParameters()[0]); TypeSerializer entrySerial = value.getOptions().getSerializers().get(entryType); if (entrySerial == null) { throw new ObjectMappingException("No applicable type serializer for type " + entryType); } if (value.hasListChildren()) { List<? extends ConfigurationNode> values = value.getChildrenList(); List<Object> ret = new ArrayList<>(values.size()); for (ConfigurationNode ent : values) { ret.add(entrySerial.deserialize(entryType, ent)); } return ret; } else { Object unwrappedVal = value.getValue(); if (unwrappedVal != null) { return Lists.newArrayList(entrySerial.deserialize(entryType, value)); } } return new ArrayList<>(); }
@Override public void serialize(TypeToken<?> type, List<?> obj, ConfigurationNode value) throws ObjectMappingException { if (!(type.getType() instanceof ParameterizedType)) { throw new ObjectMappingException("Raw types are not supported for collections"); } TypeToken<?> entryType = type.resolveType(List.class.getTypeParameters()[0]); TypeSerializer entrySerial = value.getOptions().getSerializers().get(entryType); if (entrySerial == null) { throw new ObjectMappingException("No applicable type serializer for type " + entryType); } value.setValue(ImmutableList.of()); for (Object ent : obj) { entrySerial.serialize(entryType, ent, value.getAppendedNode()); } } }
@Override public void serialize(@NonNull TypeToken<?> type, @Nullable List<?> obj, @NonNull ConfigurationNode value) throws ObjectMappingException { if (!(type.getType() instanceof ParameterizedType)) { throw new ObjectMappingException("Raw types are not supported for collections"); } TypeToken<?> entryType = type.resolveType(List.class.getTypeParameters()[0]); TypeSerializer entrySerial = value.getOptions().getSerializers().get(entryType); if (entrySerial == null) { throw new ObjectMappingException("No applicable type serializer for type " + entryType); } value.setValue(ImmutableList.of()); for (Object ent : obj) { entrySerial.serialize(entryType, ent, value.getAppendedNode()); } } }
/** * Set this node's value to the given value. * If the provided value is a {@link java.util.Collection} or a {@link java.util.Map}, it will be unwrapped into * the appropriate configuration node structure. * This method will also perform serialization using the appropriate TypeSerializer for the given type, or casting if no type serializer is found. * * @param type The type to use for serialization type information * @param value The value to set * @param <T> The type to serialize to * @return this */ default <T> ConfigurationNode setValue(TypeToken<T> type, T value) throws ObjectMappingException { if (value == null) { setValue(null); return this; } TypeSerializer serial = getOptions().getSerializers().get(type); if (serial != null) { serial.serialize(type, value, this); } else if (getOptions().acceptsType(value.getClass())) { setValue(value); // Just write if no applicable serializer exists? } else { throw new ObjectMappingException("No serializer available for type " + type); } return this; }
@Override public void serialize(@NonNull TypeToken<?> type, @Nullable List<?> obj, @NonNull ConfigurationNode value) throws ObjectMappingException { if (!(type.getType() instanceof ParameterizedType)) { throw new ObjectMappingException("Raw types are not supported for collections"); } TypeToken<?> entryType = type.resolveType(List.class.getTypeParameters()[0]); TypeSerializer entrySerial = value.getOptions().getSerializers().get(entryType); if (entrySerial == null) { throw new ObjectMappingException("No applicable type serializer for type " + entryType); } value.setValue(ImmutableList.of()); for (Object ent : obj) { entrySerial.serialize(entryType, ent, value.getAppendedNode()); } } }
/** * Set this node's value to the given value. * * <p>If the provided value is a {@link Collection} or a {@link Map}, it will be unwrapped into * the appropriate configuration node structure.</p> * * <p>This method will also perform serialization using the appropriate TypeSerializer for the * given type, or casting if no type serializer is found.</p> * * @param type The type to use for serialization type information * @param value The value to set * @param <T> The type to serialize to * @return this */ @NonNull default <T> ConfigurationNode setValue(@NonNull TypeToken<T> type, @Nullable T value) throws ObjectMappingException { if (value == null) { setValue(null); return this; } TypeSerializer<T> serial = getOptions().getSerializers().get(type); if (serial != null) { serial.serialize(type, value, this); } else if (getOptions().acceptsType(value.getClass())) { setValue(value); // Just write if no applicable serializer exists? } else { throw new ObjectMappingException("No serializer available for type " + type); } return this; }