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

com.flowpowered.commons.store.FlatFileStore Maven / Gradle / Ivy

Go to download

A library for Java that provides re-usable components commonly used by Flow libraries.

The newest version!
/*
 * This file is part of Flow Commons, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2013 Flow Powered 
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.flowpowered.commons.store;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map.Entry;

/**
 * This implements a SimpleStore that is stored in memory. The save and load methods can be used to write the map to a File.
 */
public class FlatFileStore extends MemoryStore {
    private final Path path;
    private boolean dirty = false;
    private final Class clazz; // Preserve class, so parser knows what to do

    public FlatFileStore(Path path, Class clazz) {
        super();
        this.clazz = clazz;
        this.path = path;
        if (path != null) {
            if (!Files.exists(path)) {
                try {
                    Files.createFile(path);
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
            }
        }
    }

    @Override
    public synchronized boolean clear() {
        dirty = true;
        return super.clear();
    }

    @Override
    public synchronized boolean save() {
        if (!dirty) {
            return true;
        }

        Collection strings = getStrings();
        try {
            Files.write(path, strings, Charset.defaultCharset());
            dirty = false;
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    @Override
    public synchronized boolean load() {
        Collection strings;
        try {
            strings = Files.readAllLines(path, Charset.defaultCharset());
        } catch (IOException e) {
            return false;
        }
        boolean loaded = processStrings(strings);
        if (loaded) {
            dirty = false;
        }
        return loaded;
    }

    @Override
    public synchronized T remove(String key) {
        T value = super.remove(key);
        if (value != null) {
            dirty = true;
        }
        return value;
    }

    @Override
    public synchronized T set(String key, T value) {
        dirty = true;
        return super.set(key, value);
    }

    private synchronized Collection getStrings() {
        Iterator> itr = super.getEntrySet().iterator();
        ArrayList strings = new ArrayList<>(super.getSize());
        while (itr.hasNext()) {
            Entry entry = itr.next();
            String encodedKey = encode(entry.getKey());
            T value = entry.getValue();
            strings.add(value + ":" + encodedKey);
        }
        return strings;
    }

    private boolean processStrings(Collection strings) {
        super.clear();
        for (String string : strings) {
            String[] split = string.trim().split(":");
            if (split.length != 2) {
                return false;
            }
            T value;
            try {
                value = parse(split[0]);
            } catch (NumberFormatException nfe) {
                return false;
            }
            String key = decode(split[1]);
            set(key, value);
        }
        return true;
    }

    private static String encode(String key) {
        String encoded = key;
        encoded = encoded.replace("\\", "\\\\");
        encoded = encoded.replace("\n", "\\n");
        encoded = encoded.replace(":", "\\:");
        return encoded;
    }

    private static String decode(String encoded) {
        String key = encoded;
        key = key.replace("\\:", ":");
        key = key.replace("\\n", "\n");
        key = key.replace("\\\\", "\\");
        return encoded;
    }

    @SuppressWarnings ("unchecked")
    private T parse(String string) {
        if (clazz.equals(Integer.class)) {
            return (T) (Object) Integer.parseInt(string);
        } else if (clazz.equals(String.class)) {
            return (T) string;
        } else {
            throw new IllegalArgumentException("Unable to parse clazzes of type " + clazz.getName());
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy