com.google.sitebricks.stat.StatsPublishers Maven / Gradle / Ivy
/**
* Copyright (C) 2011 Google Inc.
*
* 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.
*
*/
package com.google.sitebricks.stat;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;
/**
* This class contains a collection of {@link StatsPublisher} implementations.
*
* @author [email protected] (Fred Faber)
*/
final class StatsPublishers {
private StatsPublishers() { }
/** This {@link StatsPublisher} publishes snapshots as html. */
static class HtmlStatsPublisher extends StatsPublisher {
@Override protected String getContentType() {
return "text/html";
}
@Override protected void publish(
ImmutableMap snapshot, PrintWriter writer) {
writer.println("");
for (Entry entry : snapshot.entrySet()) {
StatDescriptor statDescriptor = entry.getKey();
writer.print("");
writer.print(statDescriptor.getName());
writer.print(": ");
writer.print(entry.getValue());
writer.println("
");
}
writer.println("");
}
}
/** This {@link StatsPublisher} publishes a snapshot as JSON. */
static class JsonStatsPublisher extends StatsPublisher {
@Override protected String getContentType() {
return "application/json";
}
@Override protected void publish(
ImmutableMap snapshot, PrintWriter writer) {
Gson gson = new Gson();
Map valuesByName = Maps.newLinkedHashMap();
for (Entry entry : snapshot.entrySet()) {
valuesByName.put(entry.getKey().getName(), entry.getValue());
}
String gsonString = gson.toJson(valuesByName);
writer.write(gsonString);
}
}
/** This {@link StatsPublisher} publishes snapshots as text. */
static class TextStatsPublisher extends StatsPublisher {
@Override protected String getContentType() {
return "text/plain";
}
@Override protected void publish(
ImmutableMap snapshot, PrintWriter writer) {
for (Map.Entry entry : snapshot.entrySet()) {
writer.println(entry.getKey().getName() + " " + entry.getValue());
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy