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 org.spongepowered.configurate.examples;
018
019import org.checkerframework.checker.nullness.qual.Nullable;
020import org.spongepowered.configurate.CommentedConfigurationNode;
021import org.spongepowered.configurate.ConfigurateException;
022import org.spongepowered.configurate.ConfigurationNode;
023import org.spongepowered.configurate.hocon.HoconConfigurationLoader;
024import org.spongepowered.configurate.serialize.SerializationException;
025
026import java.nio.file.Paths;
027
028public final class Tutorial {
029
030    private Tutorial() {}
031
032    public static void main(final String[] args) throws SerializationException {
033        final HoconConfigurationLoader loader = HoconConfigurationLoader.builder()
034                .path(Paths.get("myproject.conf")) // Set where we will load and save to
035                .build();
036
037        final CommentedConfigurationNode root;
038        try {
039            root = loader.load();
040        } catch (final ConfigurateException e) {
041            System.err.println("An error occurred while loading this configuration: " + e.getMessage());
042            if (e.getCause() != null) {
043                e.getCause().printStackTrace();
044            }
045            System.exit(1);
046            return;
047        }
048
049        final ConfigurationNode countNode = root.node("messages", "count");
050        final ConfigurationNode moodNode = root.node("messages", "mood");
051
052        final @Nullable String name = root.node("name").getString();
053        final int count = countNode.getInt(Integer.MIN_VALUE);
054        final @Nullable Mood mood = moodNode.get(Mood.class);
055
056        if (name == null || count == Integer.MIN_VALUE || mood == null) {
057            System.err.println("Invalid configuration");
058            System.exit(2);
059            return;
060        }
061
062        System.out.println("Hello, " + name + "!");
063        System.out.println("You have " + count + " " + mood + " messages!");
064        System.out.println("Thanks for viewing your messages");
065
066        // Update values
067        countNode.raw(0); // native type
068        moodNode.set(Mood.class, Mood.NEUTRAL); // serialized type
069
070        root.node("accesses").act(n -> {
071            n.commentIfAbsent("The times messages have been accessed, in milliseconds since the epoch");
072            n.appendListNode().set(System.currentTimeMillis());
073        });
074
075        // And save the node back to the file
076        try {
077            loader.save(root);
078        } catch (final ConfigurateException e) {
079            System.err.println("Unable to save your messages configuration! Sorry! " + e.getMessage());
080            System.exit(1);
081        }
082
083    }
084
085    /**
086     * A mood that a message may have.
087     */
088    enum Mood {
089
090        HAPPY, SAD, CONFUSED, NEUTRAL;
091
092    }
093
094}
095