Skip to content

KopeMorta/UserPreferences

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UserPreferences

Small Utility for manage User Preferences

What for

Provides the functionality of two-way object update. There is an object in which user settings are stored, and we want these parameters to be automatically updated in the file if the object has been changed. Conversely, the object will be updated when the user edits the file.

Using

More in Example Package

import com.kopemorta.userpreferences.Config;
import com.kopemorta.userpreferences.UserPreferences;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;

import java.io.File;

public class Example {

    public static void main(String[] args) {
        final Config config = Config.builder() // if need
                .delay(100)
                .initialDelay(20)
                // etc
                .build();

        UserPreferences.init(config); // if don't use custom config - skip init

        final Settings settings = Settings.getInstance();
        UserPreferences.registerInstance(settings); // register instance

        /*
         * Done.
         * You can change settings object as you want.
         * Edit file, call setters, by reflection, etc.
         */
        settings.setConTimeout(231);
        settings.setMyCustomFile(new File("new file"));
        settings.setMySuperString("MY VERY SUPER STRING !!!");
    }
}


@EqualsAndHashCode(callSuper = false)
class Settings implements UserPreferences {

    private static final File PREF_FILE = new File("settings.cfg");

    private static volatile Settings instance;

    public static Settings getInstance() {
        Settings localInstance = instance;
        if (localInstance == null) {
            synchronized (Settings.class) {
                localInstance = instance;
                if (localInstance == null) {
                    instance = localInstance = new Settings();
                }
            }
        }
        return localInstance;
    }


    @Getter
    @Setter
    private File myCustomFile = new File("mysupefile.txt");

    @Getter
    @Setter
    private long timeout = 25;

    @Getter
    @Setter
    private long conTimeout = 10;

    @Getter
    @Setter
    private long readTimeout = 10;

    @Getter
    @Setter
    private String mySuperString = "aooaooo";


    private Settings() {
    }


    @Override
    public File userPreferencesFile() {
        return PREF_FILE;
    }
    
}

About

Utility for Management User Preferences

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages