001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.runtime.userprofile;
021
022import java.util.Enumeration;
023import java.util.Iterator;
024import java.util.Properties;
025
026import org.apache.isis.core.commons.debug.DebugBuilder;
027import org.apache.isis.core.commons.debug.DebuggableWithTitle;
028
029public class Options implements DebuggableWithTitle {
030
031    private final Properties properties = new Properties();
032
033    public void addOption(final String name, final String value) {
034        properties.put(name, value);
035    }
036
037    public void addOptions(final String name, final Options options) {
038        properties.put(name, options);
039    }
040
041    public Iterator<String> names() {
042        final Enumeration<?> propertyNames = properties.propertyNames();
043        return new Iterator<String>() {
044            @Override
045            public boolean hasNext() {
046                return propertyNames.hasMoreElements();
047            }
048
049            @Override
050            public String next() {
051                return (String) propertyNames.nextElement();
052            }
053
054            @Override
055            public void remove() {
056                throw new UnsupportedOperationException();
057            }
058        };
059    }
060
061    public String getString(final String name) {
062        return properties.getProperty(name);
063    }
064
065    public String getString(final String name, final String defaultValue) {
066        return properties.getProperty(name, defaultValue);
067    }
068
069    public int getInteger(final String name, final int defaultValue) {
070        final String value = getString(name);
071        if (value == null) {
072            return defaultValue;
073        } else {
074            return Integer.valueOf(value).intValue();
075        }
076    }
077
078    public Options getOptions(final String name) {
079        Options options = (Options) properties.get(name);
080        if (options == null) {
081            options = new Options();
082            addOptions(name, options);
083        }
084        return options;
085    }
086
087    public boolean isOptions(final String name) {
088        return properties.get(name) instanceof Options;
089    }
090
091    public void copy(final Options options) {
092        properties.putAll(options.properties);
093    }
094
095    // ///////////////////////////////
096    // Debugging
097    // ///////////////////////////////
098
099    @Override
100    public String debugTitle() {
101        return "Options";
102    }
103
104    @Override
105    public void debugData(final DebugBuilder debug) {
106        final Enumeration<Object> keys = properties.keys();
107        while (keys.hasMoreElements()) {
108            final String name = (String) keys.nextElement();
109            debug.appendln(name, properties.get(name));
110        }
111    }
112
113}