Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright 2012-2014 One Platform Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.content.Intent;

import android.os.Bundle;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class Main {
    private static final String ITEM_DIVIDER = ", ";

    /**
     * Converts an {@link android.content.Intent} object to a {@code String}.
     *
     * @param intent The converted intent.
     * @return The string representation of the intent.
     */
    @SuppressWarnings("PMD.ConsecutiveLiteralAppends")
    @NonNull
    public static String toString(@Nullable final Intent intent) {
        if (intent == null) {
            return "null";
        }

        final StringBuilder stringBuilder = new StringBuilder("Intent{action=\"").append(intent.getAction())
                .append('"').append(ITEM_DIVIDER).append("data=\"").append(intent.getDataString()).append('"')
                .append(ITEM_DIVIDER).append("component=\"").append(intent.getComponent()).append('"')
                .append(ITEM_DIVIDER);

        final Bundle extras = intent.getExtras();
        stringBuilder.append("extras=").append(extras == null ? null : toString(extras)).append('}');
        return stringBuilder.toString();
    }

    /**
     * Converts a {@link android.os.Bundle} object to a {@code String}.
     *
     * @param bundle The converted bundle.
     * @return The string representation of the bundle.
     */
    @NonNull
    public static String toString(@Nullable final Bundle bundle) {
        if (bundle == null) {
            return "null";
        }

        if (bundle.isEmpty()) {
            return "";
        }

        final StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append('[');
        for (String key : bundle.keySet()) {
            stringBuilder.append('"').append(key).append("\":\"").append(bundle.get(key)).append('"')
                    .append(ITEM_DIVIDER);
        }
        stringBuilder.setLength(stringBuilder.length() - ITEM_DIVIDER.length());
        stringBuilder.append(']');
        return stringBuilder.toString();
    }
}