package com.livingworld.data.serialization;

import java.util.Properties;

/**
 * {@link PersistenceWriter} backed by a {@link Properties} object.
 *
 * <p>All keys are namespaced by a caller-supplied prefix so that multiple
 * modules can write to the same {@code Properties} without collision.
 * For example, a prefix of {@code "mod.pollution."} and a key of
 * {@code "airPollution"} stores the value under {@code "mod.pollution.airPollution"}.</p>
 */
public final class PropertiesPersistenceWriter implements PersistenceWriter {

    private final Properties props;
    private final String prefix;

    public PropertiesPersistenceWriter(Properties props, String prefix) {
        this.props = props;
        this.prefix = prefix;
    }

    @Override
    public void writeString(String key, String value) {
        props.setProperty(prefix + key, value);
    }

    @Override
    public void writeInt(String key, int value) {
        props.setProperty(prefix + key, Integer.toString(value));
    }

    @Override
    public void writeLong(String key, long value) {
        props.setProperty(prefix + key, Long.toString(value));
    }

    @Override
    public void writeDouble(String key, double value) {
        props.setProperty(prefix + key, Double.toString(value));
    }

    @Override
    public void writeBoolean(String key, boolean value) {
        props.setProperty(prefix + key, Boolean.toString(value));
    }
}
