com.aliyun.odps.udf.utils.CounterUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.aliyun.odps.udf.utils.CounterUtils.java

Source

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.
 */

package com.aliyun.odps.udf.utils;

import java.util.Map.Entry;

import com.aliyun.odps.counter.Counter;
import com.aliyun.odps.counter.CounterGroup;
import com.aliyun.odps.counter.Counters;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

/**
 *  Counter ???
 *
 * @see Counter
 * @see CounterGroup
 * @see Counters
 */
public class CounterUtils {

    /**
     *  {@link Counters} ?JSON
     *
     * @param counters
     *     ? {@link Counters} 
     * @return JSON
     */
    public static String toJsonString(Counters counters) {
        JsonObject js = toJson(counters);
        return js.toString();
    }

    /**
     * JSON{@link Counters}
     *  {@link RuntimeException}
     *
     * @param json
     *     JSON
     * @return {@link Counters}
     * @see #toJsonString(Counters)
     */
    public static Counters createFromJsonString(String json) {
        JsonParser parser = new JsonParser();
        JsonElement el = parser.parse(json);
        return createFromJson(el.getAsJsonObject());
    }

    private static Counters createFromJson(JsonObject obj) {
        Counters counters = new Counters();
        for (Entry<String, JsonElement> entry : obj.entrySet()) {
            String key = entry.getKey();
            CounterGroup group = counters.getGroup(key);
            fromJson(group, entry.getValue().getAsJsonObject());
        }
        return counters;
    }

    private static void fromJson(CounterGroup group, JsonObject obj) {
        JsonArray counterArray = obj.get("counters").getAsJsonArray();
        for (int i = 0; i < counterArray.size(); i++) {
            JsonObject subObj = counterArray.get(i).getAsJsonObject();
            String counterName = subObj.get("name").getAsString();
            Counter counter = group.findCounter(counterName);
            long value = subObj.get("value").getAsLong();
            counter.increment(value);
        }
    }

    private static JsonObject toJson(Counters counters) {
        JsonObject obj = new JsonObject();
        for (CounterGroup group : counters) {
            obj.add(group.getName(), toJson(group));
        }
        return obj;
    }

    private static JsonObject toJson(CounterGroup counterGroup) {
        JsonObject obj = new JsonObject();
        obj.addProperty("name", counterGroup.getName());
        JsonArray counterArray = new JsonArray();
        for (Counter entry : counterGroup) {
            counterArray.add(toJson(entry));
        }
        obj.add("counters", counterArray);
        return obj;
    }

    private static JsonObject toJson(Counter counter) {
        JsonObject js = new JsonObject();
        js.addProperty("name", counter.getName());
        js.addProperty("value", counter.getValue());
        return js;
    }

}