001/*
002 * Configurate
003 * Copyright (C) zml and Configurate contributors
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package ninja.leaping.configurate.examples;
018
019import com.google.common.reflect.TypeToken;
020import ninja.leaping.configurate.ConfigurationNode;
021import ninja.leaping.configurate.commented.CommentedConfigurationNode;
022import ninja.leaping.configurate.hocon.HoconConfigurationLoader;
023import ninja.leaping.configurate.objectmapping.ObjectMappingException;
024
025import java.io.IOException;
026import java.nio.file.Paths;
027
028public class Tutorial {
029    public static void main(String[] args) throws ObjectMappingException {
030        HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
031                .setPath(Paths.get("myproject.conf")) // Set where we will load and save to
032                .build();
033
034        CommentedConfigurationNode root;
035        try {
036            root = loader.load();
037        } catch (IOException e) {
038            System.err.println("An error occurred while loading this configuration: " + e.getMessage());
039            if (e.getCause() != null) {
040                e.getCause().printStackTrace();
041            }
042            System.exit(1);
043            return;
044        }
045
046        ConfigurationNode countNode = root.getNode("messages", "count"),
047                moodNode = root.getNode("messages", "mood");
048
049        String name = root.getNode("name").getString();
050        int count = countNode.getInt(Integer.MIN_VALUE);
051        Mood mood = moodNode.getValue(Mood.TYPE);
052
053        if (name == null || count == Integer.MIN_VALUE || mood == null) {
054            System.err.println("Invalid configuration");
055            System.exit(2);
056            return;
057        }
058
059        System.out.println("Hello, " + name + "!");
060        System.out.println("You have " + count + " " + mood + " messages!");
061        System.out.println("Thanks for viewing your messages");
062
063        // Update values
064        countNode.setValue(0); // native type
065        moodNode.setValue(Mood.TYPE, Mood.NEUTRAL); // serialized type
066
067        CommentedConfigurationNode accesses = root.getNode("accesses");
068        accesses.setCommentIfAbsent("The times messages have been accessed, in milliseconds since the epoch");
069        accesses.appendListNode().setValue(System.currentTimeMillis());
070
071        // And save the node back to the file
072        try {
073            loader.save(root);
074        } catch (IOException e) {
075            System.err.println("Unable to save your messages configuration! Sorry! " + e.getMessage());
076                System.exit(1);
077        }
078    }
079}
080
081/**
082 * A mood that a message may have
083 */
084enum Mood {
085    HAPPY, SAD, CONFUSED, NEUTRAL;
086
087    public static final TypeToken<Mood> TYPE = TypeToken.of(Mood.class); // Keep track of our generic type, to avoid reinitialization
088}