Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2009, 2010 Cloudsmith Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Cloudsmith Inc. - initial API and implementation
 *******************************************************************************/

import java.io.*;

import java.util.*;

public class Main {
    /**
     * Reads a property list using the {@link Properties#load(InputStream)} method. The
     * properties are stored in a map.
     * @param stream The stream to read from
     * @return The resulting map
     * @throws IOException propagated from the load method.
     */
    public static Map<String, String> loadProperties(InputStream stream) throws IOException {
        Properties properties = new Properties();
        properties.load(stream);
        return toMap(properties);
    }

    /**
     * Copies all elements from <code>properties</code> into a Map. The returned map might be unmodifiable
     * @param properties
     * @return The map containing all elements
     */
    public static Map<String, String> toMap(Properties properties) {
        if (properties == null || properties.isEmpty())
            return Collections.<String, String>emptyMap();

        Map<String, String> props = new HashMap<String, String>(properties.size());
        putAll(properties, props);
        return props;
    }

    /**
     * Copies all elements from <code>properties</code> into the given <code>result</code>.
     * @param properties
     * @param result
     */
    public static void putAll(Properties properties, Map<String, String> result) {
        for (Enumeration<Object> keys = properties.keys(); keys.hasMoreElements();) {
            String key = (String) keys.nextElement();
            result.put(key, properties.getProperty(key));
        }
    }
}