Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import java.util.Collection;

import java.util.Map;

public class Main {
    static String replaceProperties(final CharSequence string, final Map<String, Object> properties) {
        if (string == null) {
            return null;
        } else {
            final StringBuilder buffer = new StringBuilder();
            for (int i = 0; i < string.length(); ++i) {
                char c = string.charAt(i);
                switch (c) {
                case '$':
                    ++i;
                    if (i < string.length()) {
                        c = string.charAt(i);
                        if (c == '{') {
                            ++i;
                            final StringBuilder propertyName = new StringBuilder();
                            for (; i < string.length() && c != '}'; ++i) {
                                c = string.charAt(i);
                                if (c != '}') {
                                    propertyName.append(c);
                                }
                            }
                            Object value = null;
                            if (propertyName.length() > 0) {
                                value = properties.get(propertyName.toString());

                            }
                            if (value == null) {
                                buffer.append("${");
                                buffer.append(propertyName);
                                buffer.append("}");
                            } else {
                                buffer.append(value);
                            }
                        }
                    }
                    break;

                default:
                    buffer.append(c);
                    break;
                }
            }
            return buffer.toString();
        }
    }

    static <T> T get(final Collection<T> collection, final int index) {
        if (collection != null) {
            int i = 0;
            for (final T object : collection) {
                if (i == index) {
                    return object;
                } else {
                    i++;
                }
            }
        }
        return null;
    }
}